-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathSocialRegistrationForm.tsx
More file actions
275 lines (257 loc) · 8.39 KB
/
SocialRegistrationForm.tsx
File metadata and controls
275 lines (257 loc) · 8.39 KB
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
import classNames from 'classnames';
import type { MutableRefObject, ReactElement } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import type {
AuthTriggersType,
SocialRegistrationParameters,
} from '../../lib/auth';
import { AuthEventNames, AuthTriggers } from '../../lib/auth';
import { formToJson } from '../../lib/form';
import { Button, ButtonVariant } from '../buttons/Button';
import ImageInput from '../fields/ImageInput';
import { TextField } from '../fields/TextField';
import { MailIcon, UserIcon, LockIcon, AtIcon } from '../icons';
import AuthHeader from './AuthHeader';
import type { AuthFormProps } from './common';
import { providerMap } from './common';
import AuthContext from '../../contexts/AuthContext';
import type { ProfileFormHint } from '../../hooks/useProfileForm';
import { Checkbox } from '../fields/Checkbox';
import { useLogContext } from '../../contexts/LogContext';
import AuthForm from './AuthForm';
import { Modal } from '../modals/common/Modal';
import { IconSize } from '../Icon';
import { useGenerateUsername } from '../../hooks';
import AuthContainer from './AuthContainer';
import ConditionalWrapper from '../ConditionalWrapper';
import type { SignBackProvider } from '../../hooks/auth/useSignBack';
import { useSignBack } from '../../hooks/auth/useSignBack';
import ExperienceLevelDropdown from '../profile/ExperienceLevelDropdown';
import { Loader } from '../Loader';
import { labels } from '../../lib';
export interface SocialRegistrationFormProps extends AuthFormProps {
className?: string;
provider?: string;
formRef?: MutableRefObject<HTMLFormElement>;
title?: string;
trigger: AuthTriggersType;
hints?: ProfileFormHint;
onUpdateHints?: (errors: ProfileFormHint) => void;
onSignup?: (params: SocialRegistrationParameters) => void;
isLoading?: boolean;
}
export type SocialRegistrationFormValues = Omit<
SocialRegistrationParameters,
'method' | 'provider'
>;
export const SocialRegistrationForm = ({
className,
provider,
formRef,
title = 'Sign up',
hints,
onUpdateHints,
onSignup,
isLoading,
simplified,
trigger,
}: SocialRegistrationFormProps): ReactElement => {
const { logEvent } = useLogContext();
const { user } = useContext(AuthContext);
const hideExperienceLevel = trigger === AuthTriggers.Onboarding;
const [nameHint, setNameHint] = useState<string>(null);
const [usernameHint, setUsernameHint] = useState<string>(null);
const [experienceLevelHint, setExperienceLevelHint] = useState<string>(null);
const [name, setName] = useState(user?.name);
const {
username,
setUsername,
isLoading: isLoadingUsername,
} = useGenerateUsername(name);
const { onUpdateSignBack } = useSignBack();
useEffect(() => {
logEvent({
event_name: AuthEventNames.StartSignUpForm,
});
// @NOTE see https://dailydotdev.atlassian.net/l/cp/dK9h1zoM
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const logError = (error: unknown) => {
logEvent({
event_name: AuthEventNames.SubmitSignUpFormError,
extra: JSON.stringify({ error }),
});
};
useEffect(() => {
if (Object.keys(hints).length) {
logError(hints);
}
// @NOTE see https://dailydotdev.atlassian.net/l/cp/dK9h1zoM
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hints]);
const onSubmit = (e: React.FormEvent) => {
e.preventDefault();
logEvent({
event_name: AuthEventNames.SubmitSignUpForm,
});
const form = e.target as HTMLFormElement;
const values = formToJson<SocialRegistrationFormValues>(
formRef?.current ?? form,
);
if (!values.name) {
logError('Name not provided');
setNameHint('Please prove your name');
return;
}
if (!values.username) {
logError('Username not provided');
setUsernameHint('Please choose a username');
return;
}
if (!hideExperienceLevel && !values.experienceLevel?.length) {
logError('Experience level not provided');
setExperienceLevelHint('Please select your experience level');
return;
}
logEvent({
event_name: AuthEventNames.SubmitSignupFormExtra,
extra: JSON.stringify({
username: values?.username,
acceptedMarketing: !values?.optOutMarketing,
experienceLevel: values?.experienceLevel,
language: values?.language,
}),
});
onUpdateSignBack(
{ name: values.name, email: user.email, image: user.image },
provider as SignBackProvider,
);
const { file, optOutMarketing, ...rest } = values;
onSignup({ ...rest, acceptedMarketing: !optOutMarketing });
};
const emailFieldIcon = (providerI: string) => {
if (providerMap[providerI]) {
return React.cloneElement(providerMap[providerI].icon, {
secondary: false,
size: 'medium',
});
}
return <MailIcon size={IconSize.Small} />;
};
if (!user?.email) {
return <></>;
}
return (
<>
<AuthHeader simplified={simplified} title={title} />
<AuthForm
className={classNames(
'mt-6 w-full flex-1 place-items-center gap-2 self-center overflow-y-auto px-6 pb-2 tablet:px-[3.75rem]',
className,
)}
ref={formRef}
onSubmit={onSubmit}
id="auth-form"
data-testid="registration_form"
>
<ImageInput
className={{ container: 'mb-4' }}
initialValue={user?.image}
size="medium"
viewOnly
/>
<TextField
saveHintSpace
className={{ container: 'w-full' }}
leftIcon={emailFieldIcon(provider)}
name="email"
inputId="email"
label="Email"
type="email"
value={user?.email}
readOnly
rightIcon={<LockIcon />}
/>
<TextField
saveHintSpace
className={{ container: 'w-full' }}
leftIcon={<UserIcon size={IconSize.Small} />}
name="name"
inputId="name"
label="Name"
value={name}
valid={!nameHint && !hints?.name}
hint={hints?.name || nameHint}
onBlur={(e) => setName(e.target.value)}
valueChanged={() => {
if (hints?.name) {
onUpdateHints?.({ ...hints, name: '' });
}
if (nameHint) {
setNameHint('');
}
}}
/>
<TextField
saveHintSpace
className={{ container: 'w-full' }}
leftIcon={<AtIcon size={IconSize.Small} secondary />}
name="username"
inputId="username"
label="Enter a username"
value={username}
minLength={1}
valid={isLoadingUsername || (!usernameHint && !hints?.username)}
hint={
isLoadingUsername
? labels.generatingUsername
: hints?.username || usernameHint
}
onBlur={(e) => setUsername(e.target.value)}
valueChanged={() =>
hints?.[username] && onUpdateHints({ ...hints, username: '' })
}
rightIcon={isLoadingUsername ? <Loader /> : null}
/>
{!hideExperienceLevel && (
<ExperienceLevelDropdown
className={{ container: 'w-full' }}
name="experienceLevel"
onChange={() => {
if (experienceLevelHint) {
setExperienceLevelHint(null);
}
}}
valid={experienceLevelHint === null}
hint={experienceLevelHint}
saveHintSpace
/>
)}
<span className="border-b border-border-subtlest-tertiary pb-4 text-text-secondary typo-subhead">
Your email will be used to send you product and community updates
</span>
<Checkbox name="optOutMarketing" className="font-normal">
I don’t want to receive updates and promotions via email
</Checkbox>
</AuthForm>
<ConditionalWrapper
condition={simplified ?? false}
wrapper={(component) => (
<AuthContainer className="!mt-0">{component}</AuthContainer>
)}
>
<Modal.Footer>
<Button
form="auth-form"
type="submit"
className="w-full"
variant={ButtonVariant.Primary}
disabled={isLoading}
>
{user?.isPlus ? 'Continue' : 'Sign up'}
</Button>
</Modal.Footer>
</ConditionalWrapper>
</>
);
};