-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconnect-wpcom-account-card.js
74 lines (67 loc) · 1.95 KB
/
connect-wpcom-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
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
/**
* Internal dependencies
*/
import { glaData } from '~/constants';
import { API_NAMESPACE } from '~/data/constants';
import AppButton from '~/components/app-button';
import AccountCard, { APPEARANCE } from '~/components/account-card';
import useDispatchCoreNotices from '~/hooks/useDispatchCoreNotices';
import useApiFetchCallback from '~/hooks/useApiFetchCallback';
/**
* Clicking on the button to connect WordPress.com account.
*
* @event gla_wordpress_account_connect_button_click
* @property {string} context (`setup-mc`|`reconnect`) - indicates from which page the button was clicked.
*/
/**
* @fires gla_wordpress_account_connect_button_click
*/
const ConnectWPComAccountCard = () => {
const { createNotice } = useDispatchCoreNotices();
const nextPageName = glaData.mcSetupComplete ? 'reconnect' : 'setup-mc';
const query = { next_page_name: nextPageName };
const path = addQueryArgs( `${ API_NAMESPACE }/jetpack/connect`, query );
const [ fetchJetpackConnect, { loading, data } ] = useApiFetchCallback( {
path,
} );
const handleConnectClick = async () => {
try {
const d = await fetchJetpackConnect();
window.location.href = d.url;
} catch ( error ) {
createNotice(
'error',
__(
'Unable to connect your WordPress.com account. Please try again later.',
'google-listings-and-ads'
)
);
}
};
return (
<AccountCard
appearance={ APPEARANCE.WPCOM }
description={ __(
'Required to connect with Google',
'google-listings-and-ads'
) }
indicator={
<AppButton
isSecondary
loading={ loading || data }
eventName="gla_wordpress_account_connect_button_click"
eventProps={ { context: nextPageName } }
onClick={ handleConnectClick }
>
{ __( 'Connect', 'google-listings-and-ads' ) }
</AppButton>
}
/>
);
};
export default ConnectWPComAccountCard;