-
Notifications
You must be signed in to change notification settings - Fork 21
/
connected-google-mc-account-card.js
238 lines (217 loc) · 7.36 KB
/
connected-google-mc-account-card.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
/**
* External dependencies
*/
import { sprintf, __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { getSetting } from '@woocommerce/settings'; // eslint-disable-line import/no-unresolved
// The above is an unpublished package, delivered with WC, we use Dependency Extraction Webpack Plugin to import it.
// See https://github.com/woocommerce/woocommerce-admin/issues/7781
/**
* Internal dependencies
*/
import AccountCard, { APPEARANCE } from '.~/components/account-card';
import AppButton from '.~/components/app-button';
import ConnectedIconLabel from '.~/components/connected-icon-label';
import Section from '.~/wcdl/section';
import { GOOGLE_WPCOM_APP_CONNECTED_STATUS } from '.~/constants';
import { API_NAMESPACE } from '.~/data/constants';
import useDispatchCoreNotices from '.~/hooks/useDispatchCoreNotices';
import useApiFetchCallback from '.~/hooks/useApiFetchCallback';
import { useAppDispatch } from '.~/data';
import EnableNewProductSyncButton from '.~/components/enable-new-product-sync-button';
import AppNotice from '.~/components/app-notice';
import DisconnectModal, {
API_DATA_FETCH_FEATURE,
} from '.~/settings/disconnect-modal';
import { getSettingsUrl } from '.~/utils/urls';
/**
* Clicking on the "connect to a different Google Merchant Center account" button.
*
* @event gla_mc_account_connect_different_account_button_click
*/
/**
* Renders a Google Merchant Center account card UI with connected account information.
* It also provides a switch button that lets user connect with another account.
*
* @fires gla_mc_account_connect_different_account_button_click
* @param {Object} props React props.
* @param {{ id: number }} props.googleMCAccount A data payload object containing the user's Google Merchant Center account ID.
* @param {boolean} [props.hideAccountSwitch=false] Indicate whether hide the account switch block at the card footer.
* @param {boolean} [props.hideNotificationService=true] Indicate whether hide the enable Notification service block at the card footer.
*/
const ConnectedGoogleMCAccountCard = ( {
googleMCAccount,
hideAccountSwitch = false,
hideNotificationService = false,
} ) => {
const { createNotice, removeNotice } = useDispatchCoreNotices();
const { invalidateResolution } = useAppDispatch();
const [ fetchGoogleMCDisconnect, { loading: loadingGoogleMCDisconnect } ] =
useApiFetchCallback( {
path: `${ API_NAMESPACE }/mc/connection`,
method: 'DELETE',
} );
const [
fetchDisableNotifications,
{ loading: loadingDisableNotifications },
] = useApiFetchCallback( {
path: `${ API_NAMESPACE }/rest-api/authorize`,
method: 'DELETE',
} );
/**
* Temporary code for disabling the API PULL Beta Feature from the GMC Card
*/
const [ openedModal, setOpenedModal ] = useState( null );
const dismissModal = () => setOpenedModal( null );
const openDisableDataFetchModal = () =>
setOpenedModal( API_DATA_FETCH_FEATURE );
const domain = new URL( getSetting( 'homeUrl' ) ).host;
/**
* Event handler to switch GMC account. Upon click, it will:
*
* 1. Display a notice to indicate disconnection in progress, and advise users to wait.
* 2. Call API to disconnect the current connected GMC account.
* 3. Call API to refetch list of GMC accounts.
* Users may have just created a new account,
* and we want that new account to show up in the list.
* 4. Call API to refetch GMC account connection status.
* 5. If there is an error in the above API calls, display an error notice.
*/
const handleSwitch = async () => {
const { notice } = await createNotice(
'info',
__(
'Disconnecting your Google Merchant Center account, please wait…',
'google-listings-and-ads'
)
);
try {
await fetchGoogleMCDisconnect();
invalidateResolution( 'getExistingGoogleMCAccounts', [] );
invalidateResolution( 'getGoogleMCAccount', [] );
} catch ( error ) {
createNotice(
'error',
__(
'Unable to disconnect your Google Merchant Center account. Please try again later.',
'google-listings-and-ads'
)
);
}
removeNotice( notice.id );
};
const disableNotifications = async () => {
const { notice } = await createNotice(
'info',
__(
'Disabling the new Product Sync feature, please wait…',
'google-listings-and-ads'
)
);
try {
await fetchDisableNotifications();
invalidateResolution( 'getGoogleMCAccount', [] );
} catch ( error ) {
createNotice(
'error',
__(
'Unable to disable new product sync. Please try again later.',
'google-listings-and-ads'
)
);
}
removeNotice( notice.id );
};
// Show the button if the status is "approved" and the Notification Service is not hidden.
const showDisconnectNotificationsButton =
! hideNotificationService &&
googleMCAccount.wpcom_rest_api_status ===
GOOGLE_WPCOM_APP_CONNECTED_STATUS.APPROVED;
// Show the error if the status is set but is not "approved" and the Notification Service is not hidden.
const showErrorNotificationsNotice =
! hideNotificationService &&
googleMCAccount.wpcom_rest_api_status &&
googleMCAccount.notification_service_enabled &&
googleMCAccount.wpcom_rest_api_status !==
GOOGLE_WPCOM_APP_CONNECTED_STATUS.APPROVED;
const showFooter = ! hideAccountSwitch || showDisconnectNotificationsButton;
return (
<AccountCard
appearance={ APPEARANCE.GOOGLE_MERCHANT_CENTER }
description={ sprintf(
// translators: 1: account domain, 2: account ID.
__( '%1$s (%2$s)', 'google-listings-and-ads' ),
domain,
googleMCAccount.id
) }
indicator={
showErrorNotificationsNotice ? (
<EnableNewProductSyncButton
text={ __( 'Grant access', 'google-listings-and-ads' ) }
eventName="gla_enable_product_sync_click"
eventProps={ { context: 'mc_card' } }
/>
) : (
<ConnectedIconLabel />
)
}
>
{ showDisconnectNotificationsButton && (
<AppNotice status="success" isDismissible={ false }>
{ __(
'Google has been granted access to fetch your product data.',
'google-listings-and-ads'
) }
</AppNotice>
) }
{ showErrorNotificationsNotice && (
<AppNotice status="warning" isDismissible={ false }>
{ __(
'There was an issue granting access to Google for fetching your products.',
'google-listings-and-ads'
) }
</AppNotice>
) }
{ openedModal && (
<DisconnectModal
onRequestClose={ dismissModal }
onDisconnected={ () => {
window.location.href = getSettingsUrl();
} }
disconnectTarget={ openedModal }
disconnectAction={ disableNotifications }
/>
) }
{ showFooter && (
<Section.Card.Footer>
{ ! hideAccountSwitch && (
<AppButton
isLink
disabled={ loadingGoogleMCDisconnect }
text={ __(
'Or, connect to a different Google Merchant Center account',
'google-listings-and-ads'
) }
eventName="gla_mc_account_connect_different_account_button_click"
onClick={ handleSwitch }
/>
) }
{ showDisconnectNotificationsButton && (
<AppButton
isDestructive
isLink
disabled={ loadingDisableNotifications }
text={ __(
'Disable product data fetch',
'google-listings-and-ads'
) }
eventName="gla_disable_product_sync_click"
onClick={ openDisableDataFetchModal }
/>
) }
</Section.Card.Footer>
) }
</AccountCard>
);
};
export default ConnectedGoogleMCAccountCard;