Skip to content

Commit 363ebba

Browse files
committed
feat(product attributes): revert productAttributes to attributes
1 parent 97a623a commit 363ebba

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

packages/sync-actions/src/product-actions.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,17 @@ function _buildNewSetProductAttributeAction(attr) {
104104
return action
105105
}
106106

107-
function _buildNewSetAttributeAction(variantId, attr, sameForAllAttributeNames) {
107+
function _buildNewSetAttributeAction(
108+
variantId,
109+
attr,
110+
sameForAllAttributeNames
111+
) {
108112
const attributeName = attr && attr.name
109113
if (!attributeName) return undefined
110114

111115
let action = {
112116
action: 'setAttribute',
113-
variantId: variantId,
117+
variantId,
114118
name: attributeName,
115119
value: attr.value,
116120
}
@@ -123,7 +127,11 @@ function _buildNewSetAttributeAction(variantId, attr, sameForAllAttributeNames)
123127
return action
124128
}
125129

126-
function _buildAttributeValue(diffedValue, oldAttributeValue, newAttributeValue) {
130+
function _buildAttributeValue(
131+
diffedValue,
132+
oldAttributeValue,
133+
newAttributeValue
134+
) {
127135
let value
128136

129137
if (Array.isArray(diffedValue))
@@ -174,7 +182,11 @@ function _buildAttributeValue(diffedValue, oldAttributeValue, newAttributeValue)
174182
return value
175183
}
176184

177-
function _buildSetProductAttributeAction(diffedValue, oldProductData, newAttribute) {
185+
function _buildSetProductAttributeAction(
186+
diffedValue,
187+
oldProductData,
188+
newAttribute
189+
) {
178190
if (!newAttribute) return undefined
179191

180192
const action = {
@@ -186,7 +198,11 @@ function _buildSetProductAttributeAction(diffedValue, oldProductData, newAttribu
186198
const oldAttribute =
187199
oldProductData.attributes.find((a) => a.name === newAttribute.name) || {}
188200

189-
action.value = _buildAttributeValue(diffedValue, oldAttribute.value, newAttribute.value)
201+
action.value = _buildAttributeValue(
202+
diffedValue,
203+
oldAttribute.value,
204+
newAttribute.value
205+
)
190206

191207
return action
192208
}
@@ -214,7 +230,11 @@ function _buildSetAttributeAction(
214230
delete action.variantId
215231
}
216232

217-
action.value = _buildAttributeValue(diffedValue, oldAttribute.value, attribute.value)
233+
action.value = _buildAttributeValue(
234+
diffedValue,
235+
oldAttribute.value,
236+
attribute.value
237+
)
218238

219239
return action
220240
}
@@ -373,16 +393,12 @@ function _buildProductAttributesActions(
373393
if (!diffedAttributes) return actions
374394

375395
forEach(diffedAttributes, (value, key) => {
376-
// What does this test tell us?
377396
if (REGEX_NUMBER.test(key)) {
378-
// What does this test tell us?
379397
if (Array.isArray(value)) {
380-
// Whats the difference between _buildNew... and _build... ?
381398
const setAction = _buildNewSetProductAttributeAction(
382399
diffpatcher.getDeltaValue(value)
383400
)
384401
if (setAction) actions.push(setAction)
385-
// What does this test tell us?
386402
} else if (newProductData.attributes) {
387403
const setAction = _buildSetProductAttributeAction(
388404
value.value,
@@ -391,29 +407,24 @@ function _buildProductAttributesActions(
391407
)
392408
if (setAction) actions.push(setAction)
393409
}
394-
// What does this test tell us?
395410
} else if (REGEX_UNDERSCORE_NUMBER.test(key)) {
396411
if (Array.isArray(value)) {
397-
// What does this test tell us?
398412
// Ignore pure array moves!
399413
if (value.length === 3 && value[2] === 3) return
400414

401415
let deltaValue = diffpatcher.getDeltaValue(value)
402416

403-
// What happens here?
404417
if (!deltaValue)
405418
if (value[0] && value[0].name)
406419
// unset attribute if
407420
deltaValue = { name: value[0].name }
408421
else deltaValue = undefined
409422

410-
const setAction = _buildNewSetProductAttributeAction(
411-
deltaValue
412-
)
423+
const setAction = _buildNewSetProductAttributeAction(deltaValue)
413424

414425
if (setAction) actions.push(setAction)
415426
} else {
416-
const index = key.substring(1) // what happens here? Shouldn't this always be underscore?
427+
const index = key.substring(1)
417428
if (newProductData.attributes) {
418429
const setAction = _buildSetProductAttributeAction(
419430
value.value,
@@ -761,7 +772,7 @@ export function actionsMapAssets(diff, oldObj, newObj, variantHashMap) {
761772
export function actionsMapProductAttributes(
762773
diffedProductData,
763774
oldProductData,
764-
newProductData,
775+
newProductData
765776
) {
766777
return _buildProductAttributesActions(
767778
diffedProductData.attributes,
@@ -770,7 +781,7 @@ export function actionsMapProductAttributes(
770781
)
771782
}
772783

773-
export function actionsMapVariantAttributes(
784+
export function actionsMapAttributes(
774785
diff,
775786
oldObj,
776787
newObj,

packages/sync-actions/src/products.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const actionGroups = [
2020
'prices',
2121
'pricesCustom',
2222
'productAttributes',
23-
'variantAttributes',
23+
'attributes',
2424
'images',
2525
'variants',
2626
'categories',
@@ -54,17 +54,13 @@ function createProductMapActions(
5454

5555
allActions.push(
5656
mapActionGroup('productAttributes', (): Array<UpdateAction> =>
57-
productActions.actionsMapProductAttributes(
58-
diff,
59-
oldObj,
60-
newObj
61-
)
57+
productActions.actionsMapProductAttributes(diff, oldObj, newObj)
6258
)
6359
)
6460

6561
allActions.push(
66-
mapActionGroup('variantAttributes', (): Array<UpdateAction> =>
67-
productActions.actionsMapVariantAttributes(
62+
mapActionGroup('attributes', (): Array<UpdateAction> =>
63+
productActions.actionsMapAttributes(
6864
diff,
6965
oldObj,
7066
newObj,

packages/sync-actions/test/product-sync-base.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('Exports', () => {
1515
'prices',
1616
'pricesCustom',
1717
'productAttributes',
18-
'variantAttributes',
18+
'attributes',
1919
'images',
2020
'variants',
2121
'categories',

0 commit comments

Comments
 (0)