aboutsummaryrefslogtreecommitdiffstats
path: root/src/containers/settings/EditUserScreen.tsx
blob: 62df170fca2f8a326f013cf34a96d5e5d8ffca43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Component, ReactElement } from 'react';
import { inject, observer } from 'mobx-react';
import { IntlShape, defineMessages, injectIntl } from 'react-intl';

import { StoresProps } from '../../@types/ferdium-components.types';
import { FormFields } from '../../@types/mobx-form.types';
import Form from '../../lib/Form';
import EditUserForm from '../../components/settings/user/EditUserForm';
import ErrorBoundary from '../../components/util/ErrorBoundary';

import { required, email, minLength } from '../../helpers/validation-helpers';

const messages = defineMessages({
  firstname: {
    id: 'settings.user.form.firstname',
    defaultMessage: 'First Name',
  },
  lastname: {
    id: 'settings.user.form.lastname',
    defaultMessage: 'Last Name',
  },
  email: {
    id: 'settings.user.form.email',
    defaultMessage: 'Email',
  },
  accountTypeLabel: {
    id: 'settings.user.form.accountType.label',
    defaultMessage: 'Account type',
  },
  accountTypeIndividual: {
    id: 'settings.user.form.accountType.individual',
    defaultMessage: 'Individual',
  },
  accountTypeNonProfit: {
    id: 'settings.user.form.accountType.non-profit',
    defaultMessage: 'Non-Profit',
  },
  accountTypeCompany: {
    id: 'settings.user.form.accountType.company',
    defaultMessage: 'Company',
  },
  currentPassword: {
    id: 'settings.user.form.currentPassword',
    defaultMessage: 'Current password',
  },
  newPassword: {
    id: 'settings.user.form.newPassword',
    defaultMessage: 'New password',
  },
});

interface EditUserScreenProps extends StoresProps {
  intl: IntlShape;
}

class EditUserScreen extends Component<EditUserScreenProps> {
  componentWillUnmount() {
    this.props.actions.user.resetStatus();
  }

  onSubmit(userData) {
    const { update } = this.props.actions.user;

    update({ userData });

    document.querySelector('#form')?.scrollIntoView({ behavior: 'smooth' });
  }

  prepareForm(user) {
    const { intl } = this.props;

    const config: FormFields = {
      fields: {
        firstname: {
          label: intl.formatMessage(messages.firstname),
          placeholder: intl.formatMessage(messages.firstname),
          value: user.firstname,
          validators: [required],
        },
        lastname: {
          label: intl.formatMessage(messages.lastname),
          placeholder: intl.formatMessage(messages.lastname),
          value: user.lastname,
          validators: [required],
        },
        email: {
          label: intl.formatMessage(messages.email),
          placeholder: intl.formatMessage(messages.email),
          value: user.email,
          validators: [required, email],
        },
        accountType: {
          value: user.accountType,
          validators: [required],
          label: intl.formatMessage(messages.accountTypeLabel),
          options: [
            {
              value: 'individual',
              label: intl.formatMessage(messages.accountTypeIndividual),
            },
            {
              value: 'non-profit',
              label: intl.formatMessage(messages.accountTypeNonProfit),
            },
            {
              value: 'company',
              label: intl.formatMessage(messages.accountTypeCompany),
            },
          ],
        },
        organization: {
          label: intl.formatMessage(messages.accountTypeCompany),
          placeholder: intl.formatMessage(messages.accountTypeCompany),
          value: user.organization,
        },
        oldPassword: {
          label: intl.formatMessage(messages.currentPassword),
          type: 'password',
          validators: [minLength(6)],
        },
        newPassword: {
          label: intl.formatMessage(messages.newPassword),
          type: 'password',
          validators: [minLength(6)],
        },
      },
    };

    return new Form(config);
  }

  render(): ReactElement {
    const { user } = this.props.stores;

    if (user.getUserInfoRequest.isExecuting) {
      return <div>Loading...</div>;
    }

    const form = this.prepareForm(user.data);

    return (
      <ErrorBoundary>
        <EditUserForm
          status={user.actionStatus}
          form={form}
          isSaving={user.updateUserInfoRequest.isExecuting}
          onSubmit={d => this.onSubmit(d)}
        />
      </ErrorBoundary>
    );
  }
}

export default injectIntl<'intl', EditUserScreenProps>(
  inject('stores', 'actions')(observer(EditUserScreen)),
);