-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
210 lines (189 loc) · 5.8 KB
/
index.js
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
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState, useEffect } from '@wordpress/element';
import { isEqual } from 'lodash';
/**
* Internal dependencies
*/
import { useAppDispatch } from '.~/data';
import useStoreAddress from '.~/hooks/useStoreAddress';
import useSettings from '.~/components/free-listings/configure-product-listings/useSettings';
import useDispatchCoreNotices from '.~/hooks/useDispatchCoreNotices';
import StepContent from '.~/components/stepper/step-content';
import StepContentHeader from '.~/components/stepper/step-content-header';
import StepContentFooter from '.~/components/stepper/step-content-footer';
import AdaptiveForm from '.~/components/adaptive-form';
import ValidationErrors from '.~/components/validation-errors';
import ContactInformation from '.~/components/contact-information';
import AppButton from '.~/components/app-button';
import AppSpinner from '.~/components/app-spinner';
import PreLaunchChecklist from './pre-launch-checklist';
import usePolicyCheck from '.~/hooks/usePolicyCheck';
import checkErrors from './pre-launch-checklist/checkErrors';
/**
* Step for the store requirements in the onboarding flow.
*
* @param {Object} props React props.
* @param {() => void} props.onContinue Callback called once continue button is clicked.
*/
export default function StoreRequirements( { onContinue } ) {
const { updateGoogleMCContactInformation, saveSettings } = useAppDispatch();
const { createNotice } = useDispatchCoreNotices();
const { data: address } = useStoreAddress();
const { settings } = useSettings();
const { data: policyCheckData } = usePolicyCheck();
/**
* Since it still lacking the phone verification state,
* all onboarding accounts are considered unverified phone numbers.
*/
const [ isPhoneNumberReady, setPhoneNumberReady ] = useState( false );
const [ settingsSaved, setSettingsSaved ] = useState( true );
const [ preprocessed, setPreprocessed ] = useState( false );
const handleChangeCallback = async ( _, values ) => {
try {
await saveSettings( values );
setSettingsSaved( true );
} catch ( error ) {
//Create the notice only once
if ( settingsSaved ) {
createNotice(
'error',
__(
'There was an error trying to save settings. Please try again later.',
'google-listings-and-ads'
)
);
}
setSettingsSaved( false );
}
};
const handleSubmitCallback = async () => {
try {
await updateGoogleMCContactInformation();
onContinue();
} catch ( error ) {
createNotice(
'error',
__(
'Unable to update your contact information. Please try again later.',
'google-listings-and-ads'
)
);
}
};
// Preprocess the auto-checked state and data saving.
useEffect( () => {
if ( preprocessed || ! settings || ! policyCheckData ) {
return;
}
const newSettings = { ...settings };
const websiteLive =
policyCheckData.allowed_countries &&
! policyCheckData.robots_restriction &&
! policyCheckData.page_not_found_error &&
! policyCheckData.page_redirects;
if ( websiteLive !== settings.website_live ) {
newSettings.website_live = websiteLive;
}
if ( policyCheckData.store_ssl !== settings.checkout_process_secure ) {
newSettings.checkout_process_secure = policyCheckData.store_ssl;
}
if ( policyCheckData.refund_returns !== settings.refund_tos_visible ) {
newSettings.refund_tos_visible = policyCheckData.refund_returns;
}
if (
policyCheckData.payment_gateways !==
newSettings.payment_methods_visible
) {
newSettings.payment_methods_visible =
policyCheckData.payment_gateways;
}
const promise = isEqual( newSettings, settings )
? Promise.resolve()
: saveSettings( newSettings );
promise.finally( () => setPreprocessed( true ) );
}, [ preprocessed, policyCheckData, settings, saveSettings ] );
if ( ! preprocessed ) {
return <AppSpinner />;
}
const extendAdapter = ( formContext ) => {
return {
renderRequestedValidation( key ) {
if ( formContext.adapter.requestedShowValidation ) {
return (
<ValidationErrors
messages={ formContext.errors[ key ] }
/>
);
}
return null;
},
};
};
return (
<StepContent>
<StepContentHeader
title={ __(
'Confirm store requirements',
'google-listings-and-ads'
) }
description={ __(
'Review and confirm that your store meets Google Merchant Center requirements.',
'google-listings-and-ads'
) }
/>
<AdaptiveForm
initialValues={ {
website_live: settings.website_live,
checkout_process_secure: settings.checkout_process_secure,
payment_methods_visible: settings.payment_methods_visible,
refund_tos_visible: settings.refund_tos_visible,
contact_info_visible: settings.contact_info_visible,
} }
extendAdapter={ extendAdapter }
validate={ checkErrors }
onChange={ handleChangeCallback }
onSubmit={ handleSubmitCallback }
>
{ ( formContext ) => {
const { handleSubmit, isValidForm, adapter } = formContext;
const handleSubmitClick = ( event ) => {
const isReadyToComplete =
isValidForm &&
isPhoneNumberReady &&
address.isAddressFilled;
if ( isReadyToComplete ) {
return handleSubmit( event );
}
adapter.showValidation();
};
return (
<>
<ContactInformation
onPhoneNumberVerified={ () =>
setPhoneNumberReady( true )
}
/>
<PreLaunchChecklist />
<StepContentFooter>
<AppButton
isPrimary
loading={ adapter.isSubmitting }
disabled={ ! settingsSaved }
onClick={ handleSubmitClick }
>
{ __(
'Continue',
'google-listings-and-ads'
) }
</AppButton>
</StepContentFooter>
</>
);
} }
</AdaptiveForm>
</StepContent>
);
}