-
Notifications
You must be signed in to change notification settings - Fork 21
/
attribute-mapping-table.js
270 lines (259 loc) · 7.27 KB
/
attribute-mapping-table.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
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { Pagination, Table, TablePlaceholder } from '@woocommerce/components';
import { CardBody, CardFooter, Flex, FlexItem } from '@wordpress/components';
import { useEffect } from '@wordpress/element';
/**
* Internal dependencies
*/
import Section from '~/components/section';
import AppButton from '~/components/app-button';
import AppTableCardDiv from '~/components/app-table-card-div';
import AppButtonModalTrigger from '~/components/app-button-modal-trigger';
import AttributeMappingTableCategories from './attribute-mapping-table-categories';
import AttributeMappingRuleModal from './attribute-mapping-rule-modal';
import AttributeMappingDeleteRuleModal from './attribute-mapping-delete-rule-modal';
import AttributeMappingSync from './attribute-mapping-sync';
import useMappingAttributes from '~/hooks/useMappingAttributes';
import useMappingRules from '~/hooks/useMappingRules';
import usePagination from '~/hooks/usePagination';
import { recordGlaEvent, recordTablePageEvent } from '~/utils/tracks';
const PER_PAGE = 10;
const ATTRIBUTE_MAPPING_TABLE_HEADERS = [
{
key: 'attribute',
label: __( 'Target Attribute', 'google-listings-and-ads' ),
isLeftAligned: true,
required: true,
},
{
key: 'source',
label: __( 'Data Source / Default Value', 'google-listings-and-ads' ),
isLeftAligned: true,
required: true,
},
{
key: 'categories',
label: __( 'Categories', 'google-listings-and-ads' ),
isLeftAligned: true,
required: true,
},
{
key: 'controls',
label: '',
required: true,
},
];
/**
* Renders the Attribute Mapping table component
*
* @fires gla_modal_closed When any of the modals is closed
* @fires gla_modal_open When any of the modals is open with `{ context: 'attribute-mapping-manage-rule-modal' | 'attribute-mapping-create-rule-modal' }`
* @return {JSX.Element} The component
*/
const AttributeMappingTable = () => {
const { page, setPage } = usePagination( 'attribute-mapping' );
const {
data: { rules, total },
hasFinishedResolution: rulesHasFinishedResolution,
} = useMappingRules( { page, perPage: PER_PAGE } );
const {
data: attributes,
hasFinishedResolution: attributesHasFinishedResolution,
} = useMappingAttributes();
const parseDestinationName = ( destination ) =>
attributes.find( ( e ) => e.id === destination )?.label || '';
const isLoading =
! attributesHasFinishedResolution || ! rulesHasFinishedResolution;
const handlePageChange = ( newPage, direction ) => {
setPage( newPage );
recordTablePageEvent( `attribute-mapping-rules`, newPage, direction );
};
/**
* Prevent to stay in a page without rules.
* This is because maybe the user is in the page 2 which has only one rule.
* If the user deletes that rule we don't want to stay in page 2 anymore, since it doesn't exists.
*/
useEffect( () => {
if ( rulesHasFinishedResolution && rules?.length === 0 && page > 1 ) {
setPage( page - 1 );
}
}, [ page, rules, rulesHasFinishedResolution, setPage ] );
return (
<AppTableCardDiv>
<Section.Card>
<CardBody size={ null }>
{ isLoading ? (
<TablePlaceholder
headers={ ATTRIBUTE_MAPPING_TABLE_HEADERS }
caption={ __(
'Loading Attribute Mapping rules',
'google-listings-and-ads'
) }
/>
) : (
<Table
emptyMessage={ __(
'You have no attribute rules',
'google-listings-and-ads'
) }
caption={ __(
'Attribute Mapping configuration',
'google-listings-and-ads'
) }
headers={ ATTRIBUTE_MAPPING_TABLE_HEADERS }
rows={ rules.map( ( rule ) => [
{
display: parseDestinationName(
rule.attribute
),
},
{
// TODO: replace with source_name after implementation
display: (
<span className="gla-attribute-mapping__table-label">
{ rule.source }
</span>
),
},
{
display: (
<span className="gla-attribute-mapping__table-categories">
<AttributeMappingTableCategories
categories={ rule.categories }
condition={
rule.category_condition_type
}
/>
</span>
),
},
{
display: (
<Flex justify="end">
<FlexItem>
<AppButtonModalTrigger
button={
<AppButton
isLink
text={ __(
'Edit',
'google-listings-and-ads'
) }
eventName="gla_modal_open"
eventProps={ {
context:
'attribute-mapping-manage-rule-modal',
} }
/>
}
modal={
<AttributeMappingRuleModal
rule={ rule }
onRequestClose={ (
action
) => {
recordGlaEvent(
'gla_modal_closed',
{
context:
'attribute-mapping-manage-rule-modal',
action,
}
);
} }
/>
}
/>
</FlexItem>
<FlexItem>
<AppButtonModalTrigger
button={
<AppButton
isLink
text={ __(
'Delete',
'google-listings-and-ads'
) }
eventName="gla_modal_open"
eventProps={ {
context:
'attribute-mapping-delete-rule-modal',
} }
/>
}
modal={
<AttributeMappingDeleteRuleModal
rule={ rule }
onRequestClose={ (
action
) => {
recordGlaEvent(
'gla_modal_closed',
{
context:
'attribute-mapping-delete-rule-modal',
action,
}
);
} }
/>
}
/>
</FlexItem>
</Flex>
),
},
] ) }
/>
) }
</CardBody>
<CardFooter
align="between"
className="gla-attribute-mapping__table-footer"
>
<AppButtonModalTrigger
button={
<AppButton
isSecondary
text={ __(
'Create attribute rule',
'google-listings-and-ads'
) }
eventName="gla_modal_open"
eventProps={ {
context:
'attribute-mapping-create-rule-modal',
} }
/>
}
modal={
<AttributeMappingRuleModal
onRequestClose={ ( action ) => {
recordGlaEvent( 'gla_modal_closed', {
context:
'attribute-mapping-create-rule-modal',
action,
} );
} }
/>
}
/>
<Pagination
className="gla-attribute-mapping__pagination"
page={ page }
perPage={ PER_PAGE }
total={ total }
showPagePicker={ false }
showPerPagePicker={ false }
onPageChange={ handlePageChange }
/>
<AttributeMappingSync />
</CardFooter>
</Section.Card>
</AppTableCardDiv>
);
};
export default AttributeMappingTable;