-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBusinessAccountSelection.js
137 lines (130 loc) · 3.26 KB
/
BusinessAccountSelection.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
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { Spinner } from '@woocommerce/components';
import { recordEvent } from '@woocommerce/tracks';
import { addQueryArgs } from '@wordpress/url';
import { useState } from '@wordpress/element';
import {
Button,
CardBody,
Flex,
FlexBlock,
FlexItem,
SelectControl,
__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';
/**
* Clicking on "Create business account" button.
*
* @event wcadmin_pfw_business_account_create_button_click
*/
/**
* Clicking on "Connect" business account button.
*
* @event wcadmin_pfw_business_account_connect_button_click
*/
/**
* Business Account Connection card.
*
* @fires wcadmin_pfw_business_account_create_button_click
* @fires wcadmin_pfw_business_account_connect_button_click
*
* @param {Object} props React props.
* @param {Array<Object>} props.businessAccounts
* @return {JSX.Element} Rendered component.
*/
const BusinessAccountSelection = ( { businessAccounts } ) => {
const [ targetBusinessId, setTargetBusinessId ] = useState(
undefined !== businessAccounts && businessAccounts.length > 0
? businessAccounts[ 0 ].value
: null
);
const handleConnectToBusiness = () => {
recordEvent( 'pfw_business_account_connect_button_click' );
const newURL = addQueryArgs(
wcSettings.pinterest_for_woocommerce.switchBusinessAccountUrl,
{ business_id: targetBusinessId }
);
window.location = new URL( newURL );
};
if ( businessAccounts === undefined ) {
return (
<CardBody size="large">
<Spinner />
</CardBody>
);
}
return (
<CardBody size="large" className="business-connection">
{ businessAccounts.length > 0 ? (
<>
<Text variant="subtitle">
{ __(
'Select a business account',
'pinterest-for-woocommerce'
) }
</Text>
<Flex>
<FlexBlock>
<SelectControl
options={ businessAccounts }
selected={ targetBusinessId }
onChange={ ( businessId ) =>
setTargetBusinessId( businessId )
}
/>
</FlexBlock>
<FlexItem>
<Button
isSecondary
onClick={ handleConnectToBusiness }
>
{ __( 'Connect', 'pinterest-for-woocommerce' ) }
</Button>
</FlexItem>
</Flex>
</>
) : (
<Flex>
<FlexBlock>
<Text variant="subtitle">
{ __(
'No business account detected',
'pinterest-for-woocommerce'
) }
</Text>
<Text variant="body">
{ __(
'A Pinterest business account is required to connect Pinterest with your WooCommerce store.',
'pinterest-for-woocommerce'
) }
</Text>
</FlexBlock>
<FlexItem>
<Button
isSecondary
href={
wcSettings.pinterest_for_woocommerce
.createBusinessAccountUrl
}
onClick={ () =>
recordEvent(
'pfw_business_account_create_button_click'
)
}
target="_blank"
>
{ __(
'Create business account',
'pinterest-for-woocommerce'
) }
</Button>
</FlexItem>
</Flex>
) }
</CardBody>
);
};
export default BusinessAccountSelection;