forked from woocommerce/google-listings-and-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
138 lines (129 loc) · 3.71 KB
/
index.js
File metadata and controls
138 lines (129 loc) · 3.71 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
/**
* External dependencies
*/
import { CardDivider } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import MerchantCenterSelectControl from '.~/components/merchant-center-select-control';
import AppButton from '.~/components/app-button';
import Section from '.~/wcdl/section';
import Subsection from '.~/wcdl/subsection';
import ContentButtonLayout from '.~/components/content-button-layout';
import SwitchUrlCard from '../switch-url-card';
import ReclaimUrlCard from '../reclaim-url-card';
import AccountCard, { APPEARANCE } from '.~/components/account-card';
import CreateAccountButton from '../create-account-button';
import useConnectMCAccount from '../useConnectMCAccount';
import useCreateMCAccount from '../useCreateMCAccount';
import CreatingCard from '../creating-card';
import './index.scss';
/**
* Clicking on the button to connect an existing Google Merchant Center account.
*
* @event gla_mc_account_connect_button_click
* @property {number} id The account ID to be connected.
*/
/**
* Clicking on the "Switch account" button to select a different Google Merchant Center account to connect.
*
* @event gla_mc_account_switch_account_button_click
* @property {string} context (`switch-url`|`reclaim-url`) - indicate the button is clicked from which step.
*/
/**
* @fires gla_mc_account_connect_button_click
*/
const ConnectMC = () => {
const [ value, setValue ] = useState();
const [ handleConnectMC, resultConnectMC ] = useConnectMCAccount( value );
const [ handleCreateAccount, resultCreateAccount ] = useCreateMCAccount();
if ( resultConnectMC.response?.status === 409 ) {
return (
<SwitchUrlCard
id={ resultConnectMC.error.id }
message={ resultConnectMC.error.message }
claimedUrl={ resultConnectMC.error.claimed_url }
newUrl={ resultConnectMC.error.new_url }
onSelectAnotherAccount={ resultConnectMC.reset }
/>
);
}
if (
resultConnectMC.response?.status === 403 ||
resultCreateAccount.response?.status === 403
) {
return (
<ReclaimUrlCard
id={
resultConnectMC.error?.id || resultCreateAccount.error?.id
}
websiteUrl={
resultConnectMC.error?.website_url ||
resultCreateAccount.error?.website_url
}
onSwitchAccount={ () => {
resultConnectMC.reset();
resultCreateAccount.reset();
} }
/>
);
}
if (
resultCreateAccount.loading ||
resultCreateAccount.response?.status === 503
) {
return (
<CreatingCard
retryAfter={ resultCreateAccount.error?.retry_after }
onRetry={ handleCreateAccount }
/>
);
}
return (
<AccountCard
className="gla-connect-mc-card"
appearance={ APPEARANCE.GOOGLE_MERCHANT_CENTER }
>
<CardDivider />
<Section.Card.Body>
<Subsection.Title>
{ __(
'Select an existing account',
'google-listings-and-ads'
) }
</Subsection.Title>
<ContentButtonLayout>
<MerchantCenterSelectControl
value={ value }
onChange={ setValue }
/>
<AppButton
isSecondary
loading={ resultConnectMC.loading }
disabled={ ! value }
eventName="gla_mc_account_connect_button_click"
eventProps={ { id: Number( value ) } }
onClick={ handleConnectMC }
>
{ __( 'Connect', 'google-listings-and-ads' ) }
</AppButton>
</ContentButtonLayout>
</Section.Card.Body>
<Section.Card.Footer>
<CreateAccountButton
isLink
disabled={ resultConnectMC.loading }
onCreateAccount={ handleCreateAccount }
>
{ __(
'Or, create a new Merchant Center account',
'google-listings-and-ads'
) }
</CreateAccountButton>
</Section.Card.Footer>
</AccountCard>
);
};
export default ConnectMC;