-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathProfilePage.tsx
133 lines (120 loc) · 4.41 KB
/
ProfilePage.tsx
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
import React from 'react';
import { isDefaultImage } from '../../../utils';
import { useWizard, Wizard } from '../../common';
import { useCoreUser, useEnvironment } from '../../contexts';
import { localizationKeys } from '../../customizables';
import {
ContentPage,
Form,
FormButtons,
InformationBox,
SuccessPage,
useCardState,
withCardStateProvider,
} from '../../elements';
import { handleError, useFormControl } from '../../utils';
import { UserProfileAvatarUploader } from './UserProfileAvatarUploader';
import { UserProfileBreadcrumbs } from './UserProfileNavbar';
export const ProfilePage = withCardStateProvider(() => {
const title = localizationKeys('userProfile.profilePage.title');
const card = useCardState();
const user = useCoreUser();
const [avatarChanged, setAvatarChanged] = React.useState(false);
const { first_name, last_name } = useEnvironment().userSettings.attributes;
const showFirstName = first_name.enabled;
const showLastName = last_name.enabled;
const userFirstName = user.firstName || '';
const userLastName = user.lastName || '';
const wizard = useWizard({ onNextStep: () => card.setError(undefined) });
const firstNameField = useFormControl('firstName', user.firstName || '', {
type: 'text',
label: localizationKeys('formFieldLabel__firstName'),
placeholder: localizationKeys('formFieldInputPlaceholder__firstName'),
});
const lastNameField = useFormControl('lastName', user.lastName || '', {
type: 'text',
label: localizationKeys('formFieldLabel__lastName'),
placeholder: localizationKeys('formFieldInputPlaceholder__lastName'),
});
const userInfoChanged =
(showFirstName && firstNameField.value !== userFirstName) || (showLastName && lastNameField.value !== userLastName);
const optionalFieldsChanged = userInfoChanged || avatarChanged;
const hasRequiredFields = (showFirstName && first_name.required) || (showLastName && last_name.required);
const requiredFieldsFilled =
hasRequiredFields && !!lastNameField.value && !!firstNameField.value && optionalFieldsChanged;
const nameEditDisabled = user.samlAccounts.some(sa => sa.active);
const onSubmit = async (e: React.FormEvent) => {
e.preventDefault();
return (
userInfoChanged
? user.update({ firstName: firstNameField.value, lastName: lastNameField.value })
: Promise.resolve()
)
.then(() => {
wizard.nextStep();
})
.catch(err => {
handleError(err, [firstNameField, lastNameField], card.setError);
});
};
const uploadAvatar = (file: File) => {
return user
.setProfileImage({ file })
.then(() => {
setAvatarChanged(true);
card.setIdle();
})
.catch(err => handleError(err, [], card.setError));
};
const onAvatarRemove = () => {
void user
.setProfileImage({ file: null })
.then(() => {
setAvatarChanged(true);
card.setIdle();
})
.catch(err => handleError(err, [], card.setError));
};
return (
<Wizard {...wizard.props}>
<ContentPage
headerTitle={title}
Breadcrumbs={UserProfileBreadcrumbs}
>
{nameEditDisabled && <InformationBox message={localizationKeys('userProfile.profilePage.readonly')} />}
<Form.Root onSubmit={onSubmit}>
<UserProfileAvatarUploader
user={user}
onAvatarChange={uploadAvatar}
onAvatarRemove={isDefaultImage(user.imageUrl) ? null : onAvatarRemove}
/>
{showFirstName && (
<Form.ControlRow elementId={firstNameField.id}>
<Form.Control
autoFocus
{...firstNameField.props}
required={first_name.required}
isDisabled={nameEditDisabled}
/>
</Form.ControlRow>
)}
{showLastName && (
<Form.ControlRow elementId={lastNameField.id}>
<Form.Control
{...lastNameField.props}
required={last_name.required}
isDisabled={nameEditDisabled}
/>
</Form.ControlRow>
)}
<FormButtons isDisabled={hasRequiredFields ? !requiredFieldsFilled : !optionalFieldsChanged} />
</Form.Root>
</ContentPage>
<SuccessPage
title={title}
text={localizationKeys('userProfile.profilePage.successMessage')}
Breadcrumbs={UserProfileBreadcrumbs}
/>
</Wizard>
);
});