-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUserSettings.tsx
335 lines (308 loc) · 11.6 KB
/
UserSettings.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { Email, HelpOutline, Phone } from "@mui/icons-material";
import {
Button,
Card,
CardContent,
Grid,
MenuItem,
Select,
TextField,
Tooltip,
Typography,
} from "@mui/material";
import { enqueueSnackbar } from "notistack";
import { FormEvent, Fragment, ReactElement, useState } from "react";
import { useTranslation } from "react-i18next";
import { OffOnSetting, User } from "api/models";
import { isEmailTaken, updateUser } from "backend";
import { getAvatar, getCurrentUser } from "backend/localStorage";
import AnalyticsConsent from "components/AnalyticsConsent";
import { asyncLoadSemanticDomains } from "components/Project/ProjectActions";
import ClickableAvatar from "components/UserSettings/ClickableAvatar";
import { updateLangFromUser } from "i18n";
import { useAppDispatch } from "rootRedux/hooks";
import theme from "types/theme";
import { uiWritingSystems } from "types/writingSystem";
// Chrome silently converts non-ASCII characters in a Textfield of type="email".
// Use punycode.toUnicode() to convert them from punycode back to Unicode.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const punycode = require("punycode/");
export enum UserSettingsIds {
ButtonChangeConsent = "user-settings-change-consent",
ButtonSubmit = "user-settings-submit",
FieldEmail = "user-settings-email",
FieldName = "user-settings-name",
FieldPhone = "user-settings-phone",
FieldUsername = "user-settings-username",
SelectGlossSuggestion = "user-settings-gloss-suggestion",
SelectUiLang = "user-settings-ui-lang",
}
export default function UserSettingsGetUser(): ReactElement {
const [potentialUser, setPotentialUser] = useState(getCurrentUser());
return potentialUser ? (
<UserSettings user={potentialUser} setUser={setPotentialUser} />
) : (
<Fragment />
);
}
export function UserSettings(props: {
user: User;
setUser: (user?: User) => void;
}): ReactElement {
const dispatch = useAppDispatch();
const [name, setName] = useState(props.user.name);
const [phone, setPhone] = useState(props.user.phone);
const [email, setEmail] = useState(props.user.email);
const [displayConsent, setDisplayConsent] = useState(false);
const [otelConsent, setOtelConsent] = useState(props.user.otelConsent);
const [uiLang, setUiLang] = useState(props.user.uiLang ?? "");
const [glossSuggestion, setGlossSuggestion] = useState(
props.user.glossSuggestion
);
const [emailTaken, setEmailTaken] = useState(false);
const [avatar, setAvatar] = useState(getAvatar());
const { t } = useTranslation();
async function isEmailOkay(): Promise<boolean> {
const unicodeEmail = punycode.toUnicode(email.toLowerCase());
const unchanged = unicodeEmail === props.user.email.toLowerCase();
return unchanged || !(await isEmailTaken(unicodeEmail));
}
const handleConsentChange = (consentVal?: boolean): void => {
setOtelConsent(consentVal ?? otelConsent);
setDisplayConsent(false);
};
const disabled =
name === props.user.name &&
phone === props.user.phone &&
punycode.toUnicode(email) === props.user.email &&
otelConsent === props.user.otelConsent &&
uiLang === (props.user.uiLang ?? "") &&
glossSuggestion === props.user.glossSuggestion;
async function onSubmit(e: FormEvent<HTMLFormElement>): Promise<void> {
e.preventDefault();
if (await isEmailOkay()) {
await updateUser({
...props.user,
name,
phone,
email: punycode.toUnicode(email),
otelConsent,
uiLang,
glossSuggestion,
hasAvatar: !!avatar,
});
// Update the i18n language and in-state semantic domains as needed.
if (await updateLangFromUser()) {
await dispatch(asyncLoadSemanticDomains());
}
enqueueSnackbar(t("userSettings.updateSuccess"));
props.setUser(getCurrentUser());
} else {
setEmailTaken(true);
}
}
return (
<Grid container justifyContent="center">
<Card style={{ width: 450 }}>
<form onSubmit={(e) => onSubmit(e)}>
<CardContent>
<Grid item container spacing={6}>
<Grid item container spacing={2} alignItems="center">
<Grid item>
<ClickableAvatar avatar={avatar} setAvatar={setAvatar} />
</Grid>
<Grid item xs>
<TextField
id={UserSettingsIds.FieldName}
fullWidth
variant="outlined"
value={name}
label={t("login.name")}
onChange={(e) => setName(e.target.value)}
inputProps={{
"data-testid": UserSettingsIds.FieldName,
maxLength: 100,
}}
style={{ margin: theme.spacing(1), marginLeft: 0 }}
/>
<Typography
data-testid={UserSettingsIds.FieldUsername}
id={UserSettingsIds.FieldUsername}
style={{ color: "grey" }}
variant="subtitle2"
>
{t("login.username")}
{": "}
{props.user.username}
</Typography>
</Grid>
</Grid>
<Grid item container spacing={2}>
<Grid item>
<Typography variant="h6">
{t("userSettings.contact")}
</Typography>
</Grid>
<Grid item container spacing={1} alignItems="center">
<Grid item>
<Phone />
</Grid>
<Grid item xs>
<TextField
id={UserSettingsIds.FieldPhone}
inputProps={{
"data-testid": UserSettingsIds.FieldPhone,
}}
fullWidth
variant="outlined"
value={phone}
label={t("userSettings.phone")}
onChange={(e) => setPhone(e.target.value)}
type="tel"
/>
</Grid>
</Grid>
<Grid item container spacing={1} alignItems="center">
<Grid item>
<Email />
</Grid>
<Grid item xs>
<TextField
id={UserSettingsIds.FieldEmail}
inputProps={{
"data-testid": UserSettingsIds.FieldEmail,
}}
required
fullWidth
variant="outlined"
value={email}
label={t("login.email")}
onChange={(e) => {
setEmail(e.target.value);
setEmailTaken(false);
}}
error={emailTaken}
helperText={
emailTaken ? t("login.emailTaken") : undefined
}
type="email"
/>
</Grid>
</Grid>
</Grid>
<Grid item container spacing={2}>
<Grid item xs={12}>
<Typography variant="h6">
{t("userSettings.uiLanguage")}
</Typography>
</Grid>
<Grid item>
<Select
variant="standard"
data-testid={UserSettingsIds.SelectUiLang}
id={UserSettingsIds.SelectUiLang}
value={uiLang}
onChange={(e) => setUiLang(e.target.value ?? "")}
/* Use `displayEmpty` and a conditional `renderValue` function to force
* something to appear when the menu is closed and its value is "" */
displayEmpty
renderValue={
uiLang
? undefined
: () => t("userSettings.uiLanguageDefault")
}
>
<MenuItem value={""}>
{t("userSettings.uiLanguageDefault")}
</MenuItem>
{uiWritingSystems.map((ws) => (
<MenuItem key={ws.bcp47} value={ws.bcp47}>
{`${ws.bcp47} (${ws.name})`}
</MenuItem>
))}
</Select>
</Grid>
</Grid>
<Grid item container spacing={2}>
<Grid item xs={12}>
<Typography variant="h6">
{t("userSettings.glossSuggestion")}
</Typography>
</Grid>
<Grid item>
<Select
data-testid={UserSettingsIds.SelectGlossSuggestion}
id={UserSettingsIds.SelectGlossSuggestion}
onChange={(e) =>
setGlossSuggestion(e.target.value as OffOnSetting)
}
value={glossSuggestion}
variant="standard"
>
<MenuItem value={OffOnSetting.Off}>
{t("projectSettings.autocomplete.off")}
</MenuItem>
<MenuItem value={OffOnSetting.On}>
{t("projectSettings.autocomplete.on")}
</MenuItem>
</Select>
</Grid>
<Grid item>
<Tooltip
title={t("userSettings.glossSuggestionHint")}
placement={document.body.dir === "rtl" ? "left" : "right"}
>
<HelpOutline fontSize="small" />
</Tooltip>
</Grid>
</Grid>
<Grid item container spacing={2}>
<Grid item xs={12}>
<Typography variant="h6">
{t("userSettings.analyticsConsent.title")}
</Typography>
</Grid>
<Grid item>
<Typography>
{t(
otelConsent
? "userSettings.analyticsConsent.consentYes"
: "userSettings.analyticsConsent.consentNo"
)}
</Typography>
</Grid>
<Grid item>
<Button
data-testid={UserSettingsIds.ButtonChangeConsent}
id={UserSettingsIds.ButtonChangeConsent}
onClick={() => setDisplayConsent(true)}
variant="outlined"
>
{t("userSettings.analyticsConsent.button")}
</Button>
</Grid>
{displayConsent ? (
<AnalyticsConsent
onChangeConsent={handleConsentChange}
required={false}
/>
) : null}
</Grid>
<Grid item container justifyContent="flex-end">
<Button
data-testid={UserSettingsIds.ButtonSubmit}
disabled={disabled}
id={UserSettingsIds.ButtonSubmit}
type="submit"
variant="contained"
>
{t("buttons.save")}
</Button>
</Grid>
</Grid>
</CardContent>
</form>
</Card>
</Grid>
);
}