-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
294 lines (278 loc) · 7.47 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* External dependencies
*/
import { __, _n, sprintf } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import {
CheckboxControl,
Card,
CardHeader,
CardBody,
CardFooter,
} from '@wordpress/components';
import {
EmptyTable,
Pagination,
Table,
TablePlaceholder,
} from '@woocommerce/components';
import classnames from 'classnames';
/**
* Internal dependencies
*/
import { recordGlaEvent, recordTablePageEvent } from '~/utils/tracks';
import AppTableCardDiv from '~/components/app-table-card-div';
import EditProductLink from '~/components/edit-product-link';
import './index.scss';
import { useAppDispatch } from '~/data';
import useAppSelectDispatch from '~/hooks/useAppSelectDispatch';
import useDispatchCoreNotices from '~/hooks/useDispatchCoreNotices';
import EditVisibilityAction from './edit-visibility-action';
import statusLabelMap from './statusLabelMap';
import Text from '~/components/app-text';
const PER_PAGE = 10;
const EVENT_CONTEXT = 'product-feed';
const toVisibilityEventProp = ( visible ) =>
visible ? 'sync_and_show' : 'dont_sync_and_show';
/**
* Triggered when the product feed "bulk edit" functionality is being used
*
* @event gla_bulk_edit_click
* @property {string} context Name of the table
* @property {number} number_of_items Edit how many items
* @property {string} visibility_to `("sync_and_show" | "dont_sync_and_show")`
*/
/**
* Triggered when edit links are clicked from product feed table.
*
* @event gla_edit_product_click
* @property {string} status `("approved" | "partially_approved" | "expiring" | "pending" | "disapproved" | "not_synced")`
* @property {string} visibility `("sync_and_show" | "dont_sync_and_show")`
*/
/**
* Product Feed table.
*
* @fires gla_bulk_edit_click with `context: 'product-feed'`
* @fires gla_edit_product_click
* @fires gla_table_go_to_page with `context: 'product-feed'`
* @fires gla_table_page_click with `context: 'product-feed'`
*/
const ProductFeedTableCard = () => {
const [ selectedRows, setSelectedRows ] = useState( new Set() );
const [ query, setQuery ] = useState( {
page: 1,
per_page: PER_PAGE,
orderby: 'title',
order: 'asc',
} );
const { hasFinishedResolution, data } = useAppSelectDispatch(
'getMCProductFeed',
query
);
const { updateMCProductVisibility } = useAppDispatch();
const { createNotice } = useDispatchCoreNotices();
const handleSelectAllCheckboxChange = ( checked ) => {
if ( checked ) {
const ids = data?.products.map( ( el ) => el.id );
setSelectedRows( new Set( [ ...ids ] ) );
} else {
setSelectedRows( new Set() );
}
};
const getHandleSelectRowCheckboxChange = ( productId ) => ( checked ) => {
if ( checked ) {
setSelectedRows( new Set( [ ...selectedRows, productId ] ) );
} else {
selectedRows.delete( productId );
setSelectedRows( new Set( selectedRows ) );
}
};
const handlePageChange = ( newPage, direction ) => {
setQuery( {
...query,
page: newPage,
} );
recordTablePageEvent( EVENT_CONTEXT, newPage, direction );
};
const handleSort = ( orderby, order ) => {
setQuery( {
...query,
orderby,
order,
} );
};
const headers = [
{
key: 'select',
label: (
<CheckboxControl
disabled={ ! data?.products }
checked={
data?.products?.length > 0 &&
data?.products?.every( ( el ) =>
selectedRows.has( el.id )
)
}
onChange={ handleSelectAllCheckboxChange }
/>
),
isLeftAligned: true,
required: true,
},
{
key: 'title',
label: __( 'Product Title', 'google-listings-and-ads' ),
isLeftAligned: true,
required: true,
isSortable: true,
},
{
key: 'visible',
label: __( 'Channel Visibility', 'google-listings-and-ads' ),
isLeftAligned: true,
isSortable: true,
},
{
key: 'status',
label: __( 'Status', 'google-listings-and-ads' ),
isLeftAligned: true,
isSortable: true,
},
{ key: 'action', label: '', required: true },
];
const handleEditVisibilityClick = ( visible ) => {
const ids = Array.from( selectedRows );
const { length } = ids;
updateMCProductVisibility( ids, visible ).then( () => {
const message = sprintf(
// translators: %d: number of products are updated successfully, with minimum value of 1.
_n(
'You successfully changed the channel visibility of %d product',
'You successfully changed the channel visibility of %d products',
length,
'google-listings-and-ads'
),
length
);
createNotice( 'success', message );
} );
recordGlaEvent( 'gla_bulk_edit_click', {
context: EVENT_CONTEXT,
number_of_items: length,
visibility_to: toVisibilityEventProp( visible ),
} );
handleSelectAllCheckboxChange( false );
};
const actions = (
<EditVisibilityAction
selectedSize={ selectedRows.size }
onActionClick={ handleEditVisibilityClick }
/>
);
return (
<AppTableCardDiv className="gla-product-feed-table-card">
<Card
className={ classnames( 'woocommerce-table', {
'has-actions': !! actions,
} ) }
>
<CardHeader>
{ /* We use this Text component to make it similar to TableCard component. */ }
<Text variant="title-small" as="h2">
{ __( 'Product Feed', 'google-listings-and-ads' ) }
</Text>
{ /* This is also similar to TableCard component implementation. */ }
<div className="woocommerce-table__actions">
{ actions }
</div>
</CardHeader>
<CardBody size={ null }>
{ ! hasFinishedResolution && (
<TablePlaceholder
headers={ headers }
numberOfRows={ query.per_page }
caption={ __(
'Loading product feed',
'google-listings-and-ads'
) }
/>
) }
{ hasFinishedResolution && ! data?.products && (
<EmptyTable headers={ headers } numberOfRows={ 1 }>
{ __(
'An error occurred while retrieving products. Please try again later.',
'google-listings-and-ads'
) }
</EmptyTable>
) }
{ hasFinishedResolution && data?.products && (
<Table
headers={ headers }
rows={ data.products.map( ( el ) => {
return [
{
display: (
<CheckboxControl
checked={ selectedRows.has(
el.id
) }
onChange={ getHandleSelectRowCheckboxChange(
el.id
) }
/>
),
},
{ display: el.title },
{
display: el.visible
? __(
'Sync and show',
'google-listings-and-ads'
)
: __(
`Don't sync and show`,
'google-listings-and-ads'
),
},
{
display: statusLabelMap[ el.status ],
},
{
display: (
<EditProductLink
productId={ el.id }
eventName="gla_edit_product_click"
eventProps={ {
status: el.status,
visibility:
toVisibilityEventProp(
el.visible
),
} }
/>
),
},
];
} ) }
query={ query }
onSort={ handleSort }
/>
) }
</CardBody>
<CardFooter justify="center">
{ data?.total > 0 && (
<Pagination
page={ query.page }
perPage={ query.per_page }
total={ data.total }
showPagePicker={ true }
showPerPagePicker={ false }
onPageChange={ handlePageChange }
/>
) }
</CardFooter>
</Card>
</AppTableCardDiv>
);
};
export default ProductFeedTableCard;