-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathClaimWebsite.js
305 lines (275 loc) · 8.12 KB
/
ClaimWebsite.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
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
/**
* External dependencies
*/
import { recordEvent } from '@woocommerce/tracks';
import { __, sprintf } from '@wordpress/i18n';
import {
useEffect,
useState,
createInterpolateElement,
} from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import {
Button,
Card,
CardBody,
Flex,
Notice,
__experimentalText as Text, // eslint-disable-line @wordpress/no-unsafe-wp-apis --- _experimentalText unlikely to change/disappear and also used by WC Core
} from '@wordpress/components';
/**
* Internal dependencies
*/
import AdsCreditsPromo from './components/AdsCreditsPromo';
import { PROCESS_STATUS as STATUS, LABEL_STATUS } from '../constants';
import StepHeader from '../components/StepHeader';
import StepOverview from '../components/StepOverview';
import UrlInputControl from '../components/UrlInputControl';
import StatusLabel from '../components/StatusLabel';
import { useSettingsSelect, useCreateNotice } from '../helpers/effects';
import documentationLinkProps from '../helpers/documentation-link-props';
import { getNewPath } from '@woocommerce/navigation'; // eslint-disable-line
const StaticError = ( { reqError } ) => {
if ( reqError?.data?.pinterest_code === undefined ) {
return null;
}
const staticErrors = [ 71, 75 ]; // See https://developers.pinterest.com/docs/redoc/#tag/API-Response-Codes
if ( ! staticErrors.includes( reqError.data.pinterest_code ) ) {
return null;
}
const message = createInterpolateElement(
sprintf(
// translators: %s: error reason returned by Pinterest when verifying website claim fail.
__(
'<strong>We were unable to verify this domain.</strong> %s',
'pinterest-for-woocommerce'
),
reqError.message
),
{
strong: <strong />,
}
);
return (
<Notice status="error" isDismissible={ false }>
{ message }
</Notice>
);
};
/**
* Triggered when domain verification fails.
*
* @event wcadmin_pfw_domain_verify_failure
*
* @property {string} step Identifier of the step when verification failed.
*/
/**
* Triggered when a site is successfully verified.
*
* @event wcadmin_pfw_domain_verify_success
*/
/**
* Claim Website step component.
* Renders a UI with section block and <Card> to claim website (if not yet completed) and display its status.
*
* To be used in onboarding setup stepper.
*
* @fires wcadmin_pfw_domain_verify_failure
* @fires wcadmin_pfw_domain_verify_success
* @fires wcadmin_pfw_documentation_link_click with `{ link_id: 'claim-website', context: props.view }`
* @param {Object} props React props.
* @param {'wizard'|'settings'} props.view Indicate which view this component is rendered on.
* When the website claim is complete, called when clicking the "Continue" button.
* The "Continue" button is only displayed when `props.view` is 'wizard'.
* @return {JSX.Element} Rendered component.
*/
const ClaimWebsite = ( { view } ) => {
const [ status, setStatus ] = useState( STATUS.IDLE );
const [ reqError, setReqError ] = useState();
const isDomainVerified = useSettingsSelect( 'isDomainVerified' );
const createNotice = useCreateNotice();
const pfwSettings = wcSettings.pinterest_for_woocommerce;
useEffect( () => {
// If domain is not verified and verification status is not pending, error nor success - start verification.
if (
! Object.values( {
...LABEL_STATUS,
ERROR: STATUS.ERROR,
} ).includes( status ) &&
! isDomainVerified
) {
handleClaimWebsite();
}
if ( status !== STATUS.PENDING && isDomainVerified ) {
setStatus( STATUS.SUCCESS );
}
}, [ status, isDomainVerified ] ); // eslint-disable-line
const handleClaimWebsite = async () => {
setStatus( STATUS.PENDING );
setReqError();
try {
await apiFetch( {
path: pfwSettings.apiRoute + '/domain_verification',
method: 'POST',
} );
recordEvent( 'pfw_domain_verify_success' );
setStatus( STATUS.SUCCESS );
} catch ( error ) {
setStatus( STATUS.ERROR );
setReqError( error );
createNotice(
'error',
error.message ||
__(
'Couldn’t verify your domain.',
'pinterest-for-woocommerce'
)
);
recordEvent( 'pfw_domain_verify_failure', {
step:
wcSettings.pinterest_for_woocommerce
.claimWebsiteErrorStatus[ error?.data?.status ] ||
'unknown',
} );
}
};
const VerifyButton = () => {
const buttonLabels = {
[ STATUS.IDLE ]: __(
'Start verification',
'pinterest-for-woocommerce'
),
[ STATUS.PENDING ]: __( 'Verifying…', 'pinterest-for-woocommerce' ),
[ STATUS.ERROR ]: __( 'Try again', 'pinterest-for-woocommerce' ),
[ STATUS.SUCCESS ]: __( 'Verified', 'pinterest-for-woocommerce' ),
};
const text = buttonLabels[ status ];
if (
Object.values( {
...LABEL_STATUS,
IDLE: STATUS.IDLE,
} ).includes( status )
) {
return <StatusLabel status={ status } text={ text } />;
}
return (
<Button isSecondary text={ text } onClick={ handleClaimWebsite } />
);
};
const CompleteSetupButton = () => {
const buttonLabels = {
error: __( 'Try Again', 'pinterest-for-woocommerce' ),
success: __( 'Complete Setup', 'pinterest-for-woocommerce' ),
};
return (
<Button
isPrimary
disabled={
status !== STATUS.SUCCESS && status !== STATUS.ERROR
}
onClick={
status === STATUS.SUCCESS
? handleCompleteSetup
: handleClaimWebsite
}
>
{ buttonLabels[ status ] ??
__( 'Verifying…', 'pinterest-for-woocommerce' ) }
</Button>
);
};
const handleCompleteSetup = async () => {
try {
createNotice(
'success',
__( 'Connected successfully.', 'pinterest-for-woocommerce' )
);
recordEvent( 'pfw_setup', {
target: 'complete',
trigger: 'setup-complete',
} );
// Force reload WC admin page to initiate the relevant dependencies of the Dashboard page.
const path = getNewPath( {}, '/pinterest/settings', {} );
window.location = new URL( wcSettings.adminUrl + path );
} catch ( error ) {
createNotice(
'error',
__(
'There was a problem connecting the advertiser.',
'pinterest-for-woocommerce'
)
);
}
};
return (
<div className="woocommerce-setup-guide__claim-website">
{ view === 'wizard' && (
<StepHeader
title={ __(
'Claim your website',
'pinterest-for-woocommerce'
) }
subtitle={ __( 'Step Two', 'pinterest-for-woocommerce' ) }
/>
) }
<div className="woocommerce-setup-guide__step-columns">
<div className="woocommerce-setup-guide__step-column">
<StepOverview
title={
view === 'wizard'
? __(
'Claim your website',
'pinterest-for-woocommerce'
)
: __(
'Verified domain',
'pinterest-for-woocommerce'
)
}
description={ __(
'Claim your website to get access to analytics for the Pins you publish from your site, the analytics on Pins that other people create from your site and let people know where they can find more of your content.'
) }
readMore={ documentationLinkProps( {
href: pfwSettings.pinterestLinks.claimWebsite,
linkId: 'claim-website',
context: view,
} ) }
/>
{ view === 'wizard' && <AdsCreditsPromo /> }
</div>
<div className="woocommerce-setup-guide__step-column">
<Card>
<CardBody size="large">
<Text variant="subtitle">
{ __(
'Verify your domain to claim your website',
'pinterest-for-woocommerce'
) }
</Text>
<Text variant="body">
{ __(
'This will allow access to analytics for the Pins you publish from your site, the analytics on Pins that other people create from your site, and let people know where they can find more of your content.',
'pinterest-for-woocommerce'
) }
</Text>
<Flex gap={ 6 }>
<UrlInputControl
disabled
value={ pfwSettings.homeUrlToVerify }
/>
<VerifyButton />
</Flex>
<StaticError reqError={ reqError } />
</CardBody>
</Card>
{ view === 'wizard' && (
<div className="woocommerce-setup-guide__footer-button">
<CompleteSetupButton />
</div>
) }
</div>
</div>
</div>
);
};
export default ClaimWebsite;