-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstore-address-card.js
234 lines (210 loc) · 6.99 KB
/
store-address-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
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useRef, createInterpolateElement } from '@wordpress/element';
import { Spinner } from '@woocommerce/components';
import { update as updateIcon } from '@wordpress/icons';
import { getPath, getQuery } from '@woocommerce/navigation';
/**
* Internal dependencies
*/
import useStoreAddress from '~/hooks/useStoreAddress';
import AccountCard, { APPEARANCE } from '~/components/account-card';
import AppButton from '~/components/app-button';
import ValidationErrors from '~/components/validation-errors';
import ContactInformationPreviewCard from './contact-information-preview-card';
import TrackableLink from '~/components/trackable-link';
import mapStoreAddressErrors from './mapStoreAddressErrors';
import { recordGlaEvent } from '~/utils/tracks';
import './store-address-card.scss';
/**
* Triggered when store address "Edit in WooCommerce Settings" button is clicked.
* Before `1.5.0` it was called `edit_mc_store_address`.
*
* @event gla_edit_wc_store_address
* @property {string} path The path used in the page from which the link was clicked, e.g. `"/google/settings"`.
* @property {string|undefined} [subpath] The subpath used in the page, e.g. `"/edit-store-address"` or `undefined` when there is no subpath.
*/
/**
* Track how many times and what fields the store address is having validation errors.
*
* @event gla_wc_store_address_validation
* @property {string} path The path used in the page from which the event tracking was sent, e.g. `"/google/setup-mc"` or `"/google/settings"`.
* @property {string|undefined} [subpath] The subpath used in the page, e.g. `"/edit-store-address"` or `undefined` when there is no subpath.
* @property {string} country_code The country code of store address, e.g. `"US"`.
* @property {string} missing_fields The string of the missing required fields of store address separated by comma, e.g. `"city,postcode"`.
*/
/**
* Renders a component with a given store address.
*
* @fires gla_edit_wc_store_address Whenever "Edit in WooCommerce Settings" button is clicked.
* @fires gla_wc_store_address_validation Whenever the new store address data is fetched after clicking "Update store address" button.
* @return {JSX.Element} Filled AccountCard component.
*/
const StoreAddressCard = () => {
const { loaded, data, refetch } = useStoreAddress();
const { isAddressFilled } = data;
const path = getPath();
const { subpath } = getQuery();
const refetchedCallbackRef = useRef( null );
if ( loaded && refetchedCallbackRef.current ) {
refetchedCallbackRef.current( data );
refetchedCallbackRef.current = null;
}
const handleRefreshClick = () => {
refetch();
refetchedCallbackRef.current = ( storeAddress ) => {
const eventProps = {
path,
subpath,
country_code: storeAddress.countryCode,
missing_fields: storeAddress.missingRequiredFields.join( ',' ),
};
recordGlaEvent( 'gla_wc_store_address_validation', eventProps );
};
};
const refreshButton = (
<AppButton
isSecondary
icon={ updateIcon }
iconSize={ 20 }
iconPosition="right"
text={ __( 'Update store address', 'google-listings-and-ads' ) }
onClick={ handleRefreshClick }
disabled={ ! loaded }
/>
);
const settingsLink = (
<TrackableLink
target="_blank"
type="external"
href="admin.php?page=wc-settings"
eventName="gla_edit_wc_store_address"
eventProps={ { path, subpath } }
/>
);
let addressContent = <Spinner />;
if ( loaded ) {
const { address, address2, city, state, country, postcode } = data;
const stateAndCountry = state ? `${ state } - ${ country }` : country;
const rest = [ city, stateAndCountry, postcode ]
.filter( Boolean )
.join( ', ' );
addressContent = (
<>
{ address && <div>{ address }</div> }
{ address2 && <div>{ address2 }</div> }
<div>{ rest }</div>
</>
);
}
const description = (
<p>
{ isAddressFilled
? createInterpolateElement(
__(
'We’re using your store address for Google verification. This information won’t be public. Edit in <link>WooCommerce settings</link> if needed and update to review the changes.',
'google-listings-and-ads'
),
{
link: settingsLink,
}
)
: createInterpolateElement(
__(
'Your store address is required by Google for verification. This information won’t be public. Complete that in <link>WooCommerce settings</link> and update to review the changes.',
'google-listings-and-ads'
),
{
link: settingsLink,
}
) }
</p>
);
const detail = (
<>
{ addressContent }
{ ! isAddressFilled && (
<ValidationErrors messages={ mapStoreAddressErrors( data ) } />
) }
</>
);
return (
<AccountCard
className="gla-store-address-card"
appearance={ APPEARANCE.ADDRESS }
alignIcon="top"
alignIndicator="top"
description={ description }
detail={ detail }
indicator={ refreshButton }
/>
);
};
export default StoreAddressCard;
/**
* Trigger when store address edit button is clicked.
* Before `1.5.0` this name was used for tracking clicking "Edit in settings" to edit the WC address. As of `>1.5.0`, that event is now tracked as `edit_wc_store_address`.
*
* @event gla_edit_mc_store_address
* @property {string} path The path used in the page from which the link was clicked, e.g. `"/google/settings"`.
* @property {string|undefined} [subpath] The subpath used in the page, e.g. `"/edit-store-address"` or `undefined` when there is no subpath.
*/
/**
* Renders a component with the store address.
* In preview mode, meaning there will be no refresh button, just the edit link.
*
* @fires gla_edit_mc_store_address Whenever "Edit" is clicked.
*
* @param {Object} props React props
* @param {string} props.editHref URL where Edit button should point to.
* @param {JSX.Element} props.learnMore Link to be shown at the end of missing data message.
* @return {JSX.Element} Filled AccountCard component.
*/
export function StoreAddressCardPreview( { editHref, learnMore } ) {
const { loaded, data } = useStoreAddress( 'mc' );
let content, warning;
if ( loaded ) {
const {
isAddressFilled,
isMCAddressDifferent,
address,
address2,
city,
state,
country,
postcode,
} = data;
const stateAndCountry = state ? `${ state } - ${ country }` : country;
if ( isAddressFilled && ! isMCAddressDifferent ) {
content = [ address, address2, city, stateAndCountry, postcode ]
.filter( Boolean )
.join( ', ' );
} else {
warning = __(
'Please add your store address',
'google-listings-and-ads'
);
content = (
<>
{ __(
'Google requires the store address for all stores using Google Merchant Center. ',
'google-listings-and-ads'
) }
{ learnMore }
</>
);
}
}
return (
<ContactInformationPreviewCard
appearance={ APPEARANCE.ADDRESS }
editHref={ editHref }
editEventName="gla_edit_mc_store_address"
loading={ ! loaded }
warning={ warning }
content={ content }
></ContactInformationPreviewCard>
);
}