-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.tsx
289 lines (255 loc) · 8.77 KB
/
index.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
/**
* External dependencies
*/
import React, { useState, useEffect } from 'react';
import { Button, ExternalLink, TextControl, Modal, Notice } from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { createInterpolateElement } from '@wordpress/element';
import { useI18n } from '@automattic/react-i18n';
import { useLocalizeUrl } from '@automattic/i18n-utils';
/**
* Internal dependencies
*/
import { USER_STORE } from '../../stores/user';
import { STORE_KEY as ONBOARD_STORE } from '../../stores/onboard';
import {
useLangRouteParam,
usePath,
Step,
useCurrentStep,
useAnchorFmParams,
useIsAnchorFm,
} from '../../path';
import ModalSubmitButton from '../modal-submit-button';
import './style.scss';
import SignupFormHeader from './header';
import {
initGoogleRecaptcha,
recordGoogleRecaptchaAction,
recordOnboardingError,
} from '../../lib/analytics';
import { useTrackModal } from '../../hooks/use-track-modal';
import config from '@automattic/calypso-config';
interface Props {
onRequestClose: () => void;
}
const SignupForm = ( { onRequestClose }: Props ) => {
const { __ } = useI18n();
const [ emailVal, setEmailVal ] = useState( '' );
const [ passwordVal, setPasswordVal ] = useState( '' );
const [ recaptchaClientId, setRecaptchaClientId ] = useState< number >();
const { createAccount, clearErrors } = useDispatch( USER_STORE );
const isFetchingNewUser = useSelect( ( select ) => select( USER_STORE ).isFetchingNewUser() );
const newUserError = useSelect( ( select ) => select( USER_STORE ).getNewUserError() );
const { siteTitle, siteVertical } = useSelect( ( select ) => select( ONBOARD_STORE ) ).getState();
const langParam = useLangRouteParam();
const makePath = usePath();
const currentStep = useCurrentStep();
const isMobile = useViewportMatch( 'small', '<' );
const { anchorFmPodcastId, anchorFmEpisodeId, anchorFmSpotifyUrl } = useAnchorFmParams();
const isAnchorFmSignup = useIsAnchorFm();
const closeModal = () => {
clearErrors();
onRequestClose();
};
useTrackModal( 'Signup' );
const lang = useLangRouteParam();
const localizeUrl = useLocalizeUrl();
useEffect( () => {
initGoogleRecaptcha(
'g-recaptcha',
'calypso/signup/pageLoad',
config( 'google_recaptcha_site_key' )
).then( ( result ) => {
if ( ! result ) {
return;
}
setRecaptchaClientId( result.clientId );
} );
}, [ setRecaptchaClientId ] );
const handleSignUp = async ( event: React.FormEvent< HTMLFormElement > ) => {
event.preventDefault();
const username_hint = siteTitle || siteVertical?.label || null;
let recaptchaToken;
let recaptchaError;
if ( typeof recaptchaClientId === 'number' ) {
recaptchaToken = await recordGoogleRecaptchaAction(
recaptchaClientId,
'calypso/signup/formSubmit'
);
if ( ! recaptchaToken ) {
recaptchaError = 'recaptcha_failed';
}
} else {
recaptchaError = 'recaptcha_didnt_load';
}
const result = await createAccount( {
email: emailVal,
'g-recaptcha-error': recaptchaError,
'g-recaptcha-response': recaptchaToken || undefined,
password: passwordVal,
signup_flow_name: 'gutenboarding',
locale: langParam,
extra: { username_hint, is_anchor_fm_signup: isAnchorFmSignup },
is_passwordless: false,
} );
if ( result.ok ) {
closeModal();
} else {
recordOnboardingError( {
step: 'account_creation',
error: result.newUserError.error || 'signup_form_new_user_error',
} );
}
};
const localizedTosLink = localizeUrl( 'https://wordpress.com/tos/' );
const tos = createInterpolateElement(
__( 'By creating an account you agree to our <link_to_tos>Terms of Service</link_to_tos>.' ),
{
link_to_tos: <ExternalLink href={ localizedTosLink } />,
}
);
// translators: English wording comes from Google: https://developers.google.com/recaptcha/docs/faq#id-like-to-hide-the-recaptcha-badge.-what-is-allowed
const recaptcha_text = __(
'This site is protected by reCAPTCHA and the Google <link_to_policy>Privacy Policy</link_to_policy> and <link_to_tos>Terms of Service</link_to_tos> apply.'
);
const recaptcha_tos = createInterpolateElement( recaptcha_text, {
link_to_policy: <ExternalLink href="https://policies.google.com/privacy" />,
link_to_tos: <ExternalLink href="https://policies.google.com/terms" />,
} );
let errorMessage: string | undefined;
if ( newUserError ) {
switch ( newUserError.error ) {
case 'already_taken':
case 'already_active':
case 'email_exists':
case 'email_reserved':
errorMessage = __( 'An account with this email address already exists.' );
break;
case 'email_invalid':
errorMessage = __( 'Please enter a valid email address.' );
break;
case 'email_cant_be_used_to_signup':
errorMessage = __(
'You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider.'
);
break;
case 'email_not_allowed':
errorMessage = __( 'Sorry, that email address is not allowed!' );
break;
case 'password_invalid':
errorMessage = newUserError.message;
break;
default:
errorMessage = __(
'Sorry, something went wrong when trying to create your account. Please try again.'
);
break;
}
}
const langFragment = lang ? `/${ lang }` : '';
const addAnchorQueryParts = ( url: string ): string => {
const queryParts = {
anchor_podcast: anchorFmPodcastId,
anchor_episode: anchorFmEpisodeId,
spotify_url: anchorFmSpotifyUrl,
};
for ( const [ k, v ] of Object.entries( queryParts ) ) {
if ( v ) {
url += `&${ k }=${ encodeURIComponent( v ) }`;
}
}
return url;
};
let loginRedirectUrl = window.location.origin + '/new';
if ( isAnchorFmSignup ) {
loginRedirectUrl += `${ makePath( Step.IntentGathering ) }?new`;
loginRedirectUrl = addAnchorQueryParts( loginRedirectUrl );
} else {
loginRedirectUrl += `${ makePath( Step.CreateSite ) }?new`;
}
loginRedirectUrl = encodeURIComponent( loginRedirectUrl );
let signupUrl = `/new${ makePath( Step[ currentStep ] ) }?signup`;
if ( isAnchorFmSignup ) {
signupUrl = addAnchorQueryParts( signupUrl );
}
signupUrl = encodeURIComponent( signupUrl );
const loginUrl = `/log-in/new${ langFragment }?redirect_to=${ loginRedirectUrl }&signup_url=${ signupUrl }`;
return (
<Modal
className={ 'signup-form' }
title={
isAnchorFmSignup
? __( 'Create your podcast site with WordPress.com' )
: __( 'Save your progress' )
}
onRequestClose={ closeModal }
focusOnMount={ false }
isDismissible={ false }
overlayClassName={ 'signup-form__overlay' }
// set to false so that 1password's autofill doesn't automatically close the modal
shouldCloseOnClickOutside={ false }
>
<SignupFormHeader onRequestClose={ closeModal } />
<div className="signup-form__body">
<h1 className="signup-form__title">
{ isAnchorFmSignup
? __( 'Create your podcast site with WordPress.com' )
: __( 'Save your progress' ) }
</h1>
<form onSubmit={ handleSignUp }>
<fieldset className="signup-form__fieldset">
<legend className="signup-form__legend">
<p>
{ isAnchorFmSignup
? __( 'Create a WordPress.com account and start creating your free site.' )
: __( 'Enter an email and password to save your progress and continue.' ) }
</p>
</legend>
<TextControl
value={ emailVal }
disabled={ isFetchingNewUser }
type="email"
onChange={ setEmailVal }
placeholder={ __( 'Email address' ) }
required
autoFocus={ ! isMobile } // eslint-disable-line jsx-a11y/no-autofocus
/>
<TextControl
value={ passwordVal }
disabled={ isFetchingNewUser }
type="password"
autoComplete="new-password"
onChange={ setPasswordVal }
placeholder={ __( 'Password' ) }
required
/>
{ errorMessage && (
<Notice className="signup-form__error-notice" status="error" isDismissible={ false }>
{ errorMessage }
</Notice>
) }
<div className="signup-form__footer">
<p className="signup-form__login-link">
<span>{ __( 'Already have an account?' ) }</span>{ ' ' }
<Button className="signup-form__link" isLink href={ loginUrl }>
{ __( 'Log in' ) }
</Button>
</p>
<p className="signup-form__link signup-form__terms-of-service-link">{ tos }</p>
<ModalSubmitButton disabled={ isFetchingNewUser } isBusy={ isFetchingNewUser }>
{ __( 'Create account' ) }
</ModalSubmitButton>
<p className="signup-form__link signup-form__terms-of-service-link signup-form__recaptcha_tos">
{ recaptcha_tos }
</p>
</div>
</fieldset>
</form>
</div>
<div id="g-recaptcha"></div>
</Modal>
);
};
export default SignupForm;