-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
160 lines (152 loc) · 4.53 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
/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { CardDivider } from '@wordpress/components';
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 AppButton from '~/components/app-button';
import Section from '~/components/section';
import Subsection from '~/components/subsection';
import useApiFetchCallback from '~/hooks/useApiFetchCallback';
import useDispatchCoreNotices from '~/hooks/useDispatchCoreNotices';
import { useAppDispatch } from '~/data';
import ContentButtonLayout from '~/components/content-button-layout';
import ReclaimUrlCard from '../reclaim-url-card';
import AccountCard, { APPEARANCE } from '~/components/account-card';
import AppInputLinkControl from '~/components/app-input-link-control';
import './index.scss';
/**
* Clicking on the button to switch URL for a Google Merchant Center account.
*
* @event gla_mc_account_switch_url_button_click
*/
/**
* @param {Object} props React props.
* @param {string} props.id Google Account ID
* @param {string} props.claimedUrl The claimed URL
* @param {string} props.newUrl The new URL to be associated to the Google Account
* @param {Function} [props.onSelectAnotherAccount] Callback when a different account is selected
* @fires gla_mc_account_switch_account_button_click with `context: 'switch-url'`
* @fires gla_mc_account_switch_url_button_click
*/
const SwitchUrlCard = ( {
id,
claimedUrl,
newUrl,
onSelectAnotherAccount = () => {},
} ) => {
const { createNotice } = useDispatchCoreNotices();
const { invalidateResolution } = useAppDispatch();
const [ fetchMCAccountSwitchUrl, { loading, error, response } ] =
useApiFetchCallback( {
path: `/wc/gla/mc/accounts/switch-url`,
method: 'POST',
data: { id },
} );
const homeUrl = getSetting( 'homeUrl' );
const handleSwitch = async () => {
try {
await fetchMCAccountSwitchUrl( { parse: false } );
invalidateResolution( 'getGoogleMCAccount', [] );
} catch ( e ) {
if ( e.status !== 403 ) {
const body = await e.json();
const errorMessage =
body.message ||
__(
'Unable to switch to your new URL. Please try again later.',
'google-listings-and-ads'
);
createNotice( 'error', errorMessage );
}
}
};
const handleUseDifferentMCClick = () => {
onSelectAnotherAccount();
};
if ( response && response.status === 403 ) {
return (
<ReclaimUrlCard
id={ error.id }
websiteUrl={ error.website_url }
onSwitchAccount={ handleUseDifferentMCClick }
/>
);
}
return (
<AccountCard
className="gla-switch-url-card"
appearance={ APPEARANCE.GOOGLE_MERCHANT_CENTER }
description={ sprintf(
// translators: 1: the new URL, 2: account ID.
__( '%1$s (%2$s)', 'google-listings-and-ads' ),
newUrl,
id
) }
indicator={
<AppButton
isSecondary
disabled={ loading }
eventName="gla_mc_account_switch_account_button_click"
eventProps={ {
context: 'switch-url',
} }
onClick={ handleUseDifferentMCClick }
>
{ __( 'Switch account', 'google-listings-and-ads' ) }
</AppButton>
}
>
<CardDivider />
<Section.Card.Body>
<Subsection.Title>
{ __(
'Switch to this new URL',
'google-listings-and-ads'
) }
</Subsection.Title>
<Subsection.Body>
{ sprintf(
// translators: %s: claimed URL.
__(
'This Merchant Center account already has a verified and claimed URL, %s.',
'google-listings-and-ads'
),
claimedUrl
) }
</Subsection.Body>
<ContentButtonLayout>
<AppInputLinkControl disabled value={ homeUrl } />
<AppButton
isSecondary
loading={ loading }
eventName="gla_mc_account_switch_url_button_click"
onClick={ handleSwitch }
>
{ __(
'Switch to this new URL',
'google-listings-and-ads'
) }
</AppButton>
</ContentButtonLayout>
<Subsection.HelperText>
{ sprintf(
/* translators: 1: new URL. 2: claimed URL. */
__(
'If you switch your claimed URL to %1$s, you will lose your claim to %2$s. This will cause any existing product listings tied to %2$s to stop running.',
'google-listings-and-ads'
),
newUrl,
claimedUrl
) }
</Subsection.HelperText>
</Section.Card.Body>
</AccountCard>
);
};
export default SwitchUrlCard;