-
Notifications
You must be signed in to change notification settings - Fork 2k
/
domain-details-form.jsx
328 lines (296 loc) · 9.94 KB
/
domain-details-form.jsx
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
/**
* External dependencies
*/
import React, { PureComponent } from 'react';
import { connect } from 'react-redux';
import { localize } from 'i18n-calypso';
import classNames from 'classnames';
import debugFactory from 'debug';
import { first, includes, indexOf, intersection, isEqual, last, map, merge } from 'lodash';
import emailValidator from 'email-validator';
/**
* Internal dependencies
*/
import QueryContactDetailsCache from 'components/data/query-contact-details-cache';
import QueryTldValidationSchemas from 'components/data/query-tld-validation-schemas';
import PaymentBox from './payment-box';
import FormButton from 'components/forms/form-button';
import SecurePaymentFormPlaceholder from './secure-payment-form-placeholder.jsx';
import wp from 'lib/wp';
import config from 'config';
import ContactDetailsFormFields from 'components/domains/contact-details-form-fields';
import ExtraInfoForm, {
tldsWithAdditionalDetailsForms,
} from 'components/domains/registrant-extra-info';
import { setDomainDetails } from 'lib/transaction/actions';
import { addGoogleAppsRegistrationData } from 'lib/cart/actions';
import {
getDomainRegistrations,
getDomainTransfers,
hasGoogleApps,
hasDomainRegistration,
hasInvalidAlternateEmailDomain,
needsExplicitAlternateEmailForGSuite,
hasTransferProduct,
getTlds,
hasTld,
hasSomeDomainProductsWithPrivacySupport,
hasAllDomainProductsWithPrivacySupport,
} from 'lib/cart-values/cart-items';
import getContactDetailsCache from 'state/selectors/get-contact-details-cache';
import { updateContactDetailsCache } from 'state/domains/management/actions';
import { recordTracksEvent } from 'state/analytics/actions';
import { PUBLIC_VS_PRIVATE } from 'lib/url/support';
const debug = debugFactory( 'calypso:my-sites:upgrades:checkout:domain-details' );
const wpcom = wp.undocumented();
export class DomainDetailsForm extends PureComponent {
constructor( props ) {
super( props );
const steps = [ 'mainForm', ...this.getTldsWithAdditionalForm() ];
debug( 'steps:', steps );
this.state = {
steps,
currentStep: first( steps ),
};
}
componentDidMount() {
if ( this.props.recordTracksEvent ) {
this.props.recordTracksEvent( 'calypso_checkout_domain_contact_information_view' );
}
}
componentDidUpdate( prevProps ) {
if ( ! isEqual( prevProps.cart, this.props.cart ) ) {
this.validateSteps();
}
}
validateSteps() {
const updatedSteps = [ 'mainForm', ...this.getTldsWithAdditionalForm() ];
const newState = {
steps: updatedSteps,
};
if ( updatedSteps.indexOf( this.state.currentStep ) < 0 ) {
debug( 'Switching to step: mainForm' );
newState.currentStep = 'mainForm';
}
this.setState( newState );
}
addAlternateEmailToValidationHandler = ( fieldValues, validationHandler ) => ( error, data ) => {
if ( this.needsAlternateEmailForGSuite() ) {
let message = null;
if ( ! emailValidator.validate( fieldValues.alternateEmail ) ) {
message = this.props.translate( 'Please provide a valid email address.' );
} else if ( hasInvalidAlternateEmailDomain( this.props.cart, this.props.contactDetails ) ) {
message = this.props.translate(
'Please provide an email address that does not use the same domain than the G Suite account being purchased.'
);
}
if ( null !== message ) {
data = merge( data, { success: false, messages: { alternateEmail: [ message ] } } );
}
}
validationHandler( error, data );
};
validate = ( fieldValues, onComplete ) => {
const validationHandler = ( error, data ) => {
const messages = ( data && data.messages ) || {};
onComplete( error, messages );
};
if ( this.needsOnlyGoogleAppsDetails() ) {
wpcom.validateGoogleAppsContactInformation(
fieldValues,
this.addAlternateEmailToValidationHandler( fieldValues, validationHandler )
);
return;
}
wpcom.validateDomainContactInformation( fieldValues, this.getDomainNames(), validationHandler );
};
hasAnotherStep() {
return this.state.currentStep !== last( this.state.steps );
}
switchToNextStep() {
const newStep = this.state.steps[ indexOf( this.state.steps, this.state.currentStep ) + 1 ];
debug( 'Switching to step: ' + newStep );
this.setState( { currentStep: newStep } );
}
getDomainNames = () =>
map(
[ ...getDomainRegistrations( this.props.cart ), ...getDomainTransfers( this.props.cart ) ],
'meta'
);
needsAlternateEmailForGSuite() {
return (
this.needsOnlyGoogleAppsDetails() &&
needsExplicitAlternateEmailForGSuite( this.props.cart, this.props.contactDetails )
);
}
needsOnlyGoogleAppsDetails() {
return (
hasGoogleApps( this.props.cart ) &&
! hasDomainRegistration( this.props.cart ) &&
! hasTransferProduct( this.props.cart )
);
}
getNumberOfDomainRegistrations() {
return getDomainRegistrations( this.props.cart ).length;
}
getTldsWithAdditionalForm() {
if ( ! config.isEnabled( 'domains/cctlds' ) ) {
// All we need to do to disable everything is not show the .FR form
return [];
}
return intersection( getTlds( this.props.cart ), tldsWithAdditionalDetailsForms );
}
needsFax() {
return this.props.contactDetails.countryCode === 'NL' && hasTld( this.props.cart, 'nl' );
}
renderSubmitButton() {
return (
<FormButton
className="checkout__domain-details-form-submit-button"
onClick={ this.handleSubmitButtonClick }
>
{ this.props.translate( 'Continue' ) }
</FormButton>
);
}
handleContactDetailsChange = newContactDetailsValues => {
this.props.updateContactDetailsCache( newContactDetailsValues );
};
renderDomainContactDetailsFields() {
const { contactDetails, translate, userCountryCode } = this.props;
const labelTexts = {
submitButton: translate( 'Continue' ),
organization: translate(
'Registering this domain for a company? + Add Organization Name',
'Registering these domains for a company? + Add Organization Name',
{
count: this.getNumberOfDomainRegistrations(),
}
),
};
return (
<ContactDetailsFormFields
userCountryCode={ userCountryCode }
contactDetails={ contactDetails }
needsFax={ this.needsFax() }
needsOnlyGoogleAppsDetails={ this.needsOnlyGoogleAppsDetails() }
needsAlternateEmailForGSuite={ this.needsAlternateEmailForGSuite() }
onContactDetailsChange={ this.handleContactDetailsChange }
onSubmit={ this.handleSubmitButtonClick }
eventFormName="Checkout Form"
onValidate={ this.validate }
labelTexts={ labelTexts }
/>
);
}
renderDetailsForm() {
return <form>{ this.renderDomainContactDetailsFields() }</form>;
}
renderExtraDetailsForm( tld ) {
return (
<ExtraInfoForm tld={ tld } getDomainNames={ this.getDomainNames }>
{ this.renderSubmitButton() }
</ExtraInfoForm>
);
}
handleSubmitButtonClick = event => {
if ( event && event.preventDefault ) {
event.preventDefault();
}
if ( this.hasAnotherStep() ) {
return this.switchToNextStep();
}
this.finish();
};
finish() {
const allFieldValues = this.props.contactDetails;
debug( 'finish: allFieldValues:', allFieldValues );
setDomainDetails( allFieldValues );
addGoogleAppsRegistrationData( allFieldValues );
}
renderCurrentForm() {
const { currentStep } = this.state;
return includes( tldsWithAdditionalDetailsForms, currentStep )
? this.renderExtraDetailsForm( this.state.currentStep )
: this.renderDetailsForm();
}
render() {
const classSet = classNames( {
'domain-details': true,
selected: true,
} );
const hasDomainProduct =
hasDomainRegistration( this.props.cart ) || hasTransferProduct( this.props.cart );
const hasSomeDomainsWithPrivacy =
hasDomainProduct && hasSomeDomainProductsWithPrivacySupport( this.props.cart );
const hasAllDomainsWithPrivacy =
hasDomainProduct && hasAllDomainProductsWithPrivacySupport( this.props.cart );
let title;
let message;
// TODO: gather up tld specific stuff
if ( this.state.currentStep === 'fr' ) {
title = this.props.translate( '.FR Registration' );
} else if ( this.needsOnlyGoogleAppsDetails() ) {
title = this.props.translate( 'G Suite Account Information' );
} else {
title = this.props.translate( 'Domain Contact Information' );
if ( hasDomainProduct ) {
if ( hasSomeDomainsWithPrivacy ) {
if ( hasAllDomainsWithPrivacy ) {
message = this.props.translate(
'We have pre-filled the required contact information from your WordPress.com account. Privacy ' +
'Protection is included to help protect your personal information. {{a}}Learn more{{/a}}',
{
components: {
a: <a href={ PUBLIC_VS_PRIVATE } target="_blank" rel="noopener noreferrer" />,
},
}
);
} else {
message = this.props.translate(
'We have pre-filled the required contact information from your WordPress.com account. Privacy ' +
'Protection is included for all eligible domains to help protect your personal information. {{a}}Learn more{{/a}}',
{
components: {
a: <a href={ PUBLIC_VS_PRIVATE } target="_blank" rel="noopener noreferrer" />,
},
}
);
}
} else {
message = this.props.translate(
'We have pre-filled the required contact information from your WordPress.com account. Privacy ' +
'Protection is unavailable for domains in your cart.'
);
}
}
}
return (
<div>
<QueryTldValidationSchemas tlds={ this.getTldsWithAdditionalForm() } />
<PaymentBox currentPage={ this.state.currentStep } classSet={ classSet } title={ title }>
{ message && <p>{ message }</p> }
{ this.renderCurrentForm() }
</PaymentBox>
</div>
);
}
}
export class DomainDetailsFormContainer extends PureComponent {
render() {
return (
<div>
<QueryContactDetailsCache />
{ this.props.contactDetails ? (
<DomainDetailsForm { ...this.props } />
) : (
<SecurePaymentFormPlaceholder />
) }
</div>
);
}
}
export default connect( state => ( { contactDetails: getContactDetailsCache( state ) } ), {
recordTracksEvent,
updateContactDetailsCache,
} )( localize( DomainDetailsFormContainer ) );