Skip to content

Commit accd905

Browse files
committed
feat(sync-actions): add sync actions for assets
1 parent eea5af4 commit accd905

File tree

3 files changed

+360
-46
lines changed

3 files changed

+360
-46
lines changed

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

Lines changed: 96 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from './utils/common-actions'
1111
import createBuildArrayActions, {
1212
ADD_ACTIONS,
13-
CHANGE_ACTIONS,
1413
REMOVE_ACTIONS,
1514
} from './utils/create-build-array-actions'
1615
import findMatchingPairs from './utils/find-matching-pairs'
@@ -26,6 +25,14 @@ export const baseActionsList = [
2625
{ action: 'setKey', key: 'key' },
2726
]
2827

28+
export const baseAssetActionsList = [
29+
{ action: 'setAssetKey', key: 'key', actionKey: 'assetKey' },
30+
{ action: 'changeAssetName', key: 'name' },
31+
{ action: 'setAssetDescription', key: 'description' },
32+
{ action: 'setAssetTags', key: 'tags' },
33+
{ action: 'setAssetSources', key: 'sources' },
34+
]
35+
2936
export const metaActionsList = [
3037
{ action: 'setMetaTitle', key: 'metaTitle' },
3138
{ action: 'setMetaDescription', key: 'metaDescription' },
@@ -381,37 +388,81 @@ function toVariantIdentifier(variant) {
381388
return id ? { variantId: id } : { sku }
382389
}
383390

384-
function _buildVariantAssetsActions(diff, oldObj, newObj) {
385-
const handler = createBuildArrayActions('assets', {
386-
[ADD_ACTIONS]: (newAsset) => ({
387-
action: 'addAsset',
388-
asset: newAsset,
389-
...toVariantIdentifier(newObj),
390-
}),
391-
[REMOVE_ACTIONS]: (oldAsset) => ({
392-
action: 'removeAsset',
393-
...toAssetIdentifier(oldAsset),
394-
...toVariantIdentifier(oldObj),
395-
}),
396-
[CHANGE_ACTIONS]: (oldAsset, newAsset) =>
397-
// here we could use more atomic update actions (e.g. changeAssetName)
398-
// but for now we use the simpler approach to first remove and then
399-
// re-add the asset - which reduces the code complexity
400-
[
401-
{
391+
function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) {
392+
const addAssetActions = []
393+
let changeAssetActions = []
394+
const removeAssetActions = []
395+
396+
// generate a hashMap to be able to reference the right image from both ends
397+
const matchingAssetPairs = findMatchingPairs(
398+
diffAssets,
399+
oldVariant.assets,
400+
newVariant.assets
401+
)
402+
403+
forEach(diffAssets, (asset, key) => {
404+
const { oldObj: oldAsset, newObj: newAsset } = extractMatchingPairs(
405+
matchingAssetPairs,
406+
key,
407+
oldVariant.assets,
408+
newVariant.assets
409+
)
410+
if (REGEX_NUMBER.test(key)) {
411+
if (Array.isArray(asset) && asset.length) {
412+
addAssetActions.push({
413+
action: 'addAsset',
414+
asset: diffpatcher.getDeltaValue(asset),
415+
...toVariantIdentifier(newVariant),
416+
position: Number(key),
417+
})
418+
} else if (Object.keys(asset).length) {
419+
// todo add changeAssetOrder
420+
const basicActions = buildBaseAttributesActions({
421+
actions: baseAssetActionsList,
422+
diff: asset,
423+
oldObj: oldAsset,
424+
newObj: newAsset,
425+
}).map((action) => {
426+
if (action.action === 'setAssetKey') {
427+
return {
428+
...action,
429+
...toVariantIdentifier(oldVariant),
430+
assetId: oldAsset.id,
431+
}
432+
}
433+
434+
return {
435+
...action,
436+
...toVariantIdentifier(oldVariant),
437+
...toAssetIdentifier(oldAsset),
438+
}
439+
})
440+
changeAssetActions = changeAssetActions.concat(basicActions)
441+
442+
if (asset.custom) {
443+
const customActions = actionsMapCustom(asset, newAsset, oldAsset, {
444+
actions: {
445+
setCustomType: 'setAssetCustomType',
446+
setCustomField: 'setAssetCustomField',
447+
},
448+
...toVariantIdentifier(oldVariant),
449+
...toAssetIdentifier(oldAsset),
450+
})
451+
changeAssetActions = changeAssetActions.concat(customActions)
452+
}
453+
}
454+
} else if (REGEX_UNDERSCORE_NUMBER.test(key))
455+
if (Number(asset[2]) === 0) {
456+
// asset removed
457+
removeAssetActions.push({
402458
action: 'removeAsset',
403459
...toAssetIdentifier(oldAsset),
404-
...toVariantIdentifier(oldObj),
405-
},
406-
{
407-
action: 'addAsset',
408-
asset: newAsset,
409-
...toVariantIdentifier(newObj),
410-
},
411-
],
460+
...toVariantIdentifier(oldVariant),
461+
})
462+
}
412463
})
413464

414-
return handler(diff, oldObj, newObj)
465+
return [addAssetActions, changeAssetActions, removeAssetActions]
415466
}
416467

417468
/**
@@ -516,7 +567,10 @@ export function actionsMapCategoryOrderHints(diff) {
516567
}
517568

518569
export function actionsMapAssets(diff, oldObj, newObj, variantHashMap) {
519-
let actions = []
570+
let addAssetActions = []
571+
let changeAssetActions = []
572+
let removeAssetActions = []
573+
520574
const { variants } = diff
521575

522576
if (variants)
@@ -527,16 +581,24 @@ export function actionsMapAssets(diff, oldObj, newObj, variantHashMap) {
527581
oldObj.variants,
528582
newObj.variants
529583
)
530-
if (REGEX_NUMBER.test(key) && !Array.isArray(variant)) {
531-
const assetActions = _buildVariantAssetsActions(
532-
variant,
584+
if (
585+
variant.assets &&
586+
(REGEX_UNDERSCORE_NUMBER.test(key) || REGEX_NUMBER.test(key))
587+
) {
588+
const [a, c, r] = _buildVariantAssetsActions(
589+
variant.assets,
533590
oldVariant,
534591
newVariant
535592
)
536-
if (assetActions) actions = actions.concat(assetActions)
593+
594+
// add if (assetActions)
595+
addAssetActions = addAssetActions.concat(a)
596+
changeAssetActions = changeAssetActions.concat(c)
597+
removeAssetActions = removeAssetActions.concat(r)
537598
}
538599
})
539-
return actions
600+
601+
return changeAssetActions.concat(removeAssetActions).concat(addAssetActions)
540602
}
541603

542604
export function actionsMapAttributes(

packages/sync-actions/src/utils/action-map-custom.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ export default function actionsMapCustom(
3333
customProps = { actions: {} }
3434
) {
3535
const actions = []
36-
const priceId = customProps.priceId
37-
const actionGroup = { ...Actions, ...customProps.actions }
36+
const { actions: customPropsActions, ...options } = customProps
37+
const actionGroup = { ...Actions, ...customPropsActions }
3838

3939
if (!diff.custom) return actions
4040
if (hasSingleCustomFieldChanged(diff)) {
4141
// If custom is not defined on the new or old category
4242
const custom = diffpatcher.getDeltaValue(diff.custom, oldObj)
43-
actions.push({ action: actionGroup.setCustomType, priceId, ...custom })
43+
actions.push({ action: actionGroup.setCustomType, ...options, ...custom })
4444
} else if (hasCustomTypeChanged(diff)) {
4545
// If custom is set to an empty object on the new or old category
4646
const type = extractCustomType(diff, oldObj)
4747

48-
if (!type) actions.push({ action: actionGroup.setCustomType, priceId })
48+
if (!type) actions.push({ action: actionGroup.setCustomType, ...options })
4949
else if (type.id)
5050
actions.push({
5151
action: actionGroup.setCustomType,
52-
priceId,
52+
...options,
5353
type: {
5454
typeId: 'type',
5555
id: extractTypeId(type, newObj),
@@ -59,7 +59,7 @@ export default function actionsMapCustom(
5959
else if (type.key)
6060
actions.push({
6161
action: actionGroup.setCustomType,
62-
priceId,
62+
...options,
6363
type: {
6464
typeId: 'type',
6565
key: extractTypeKey(type, newObj),
@@ -69,7 +69,7 @@ export default function actionsMapCustom(
6969
} else if (haveMultipleCustomFieldsChanged(diff)) {
7070
const customFieldsActions = Object.keys(diff.custom.fields).map((name) => ({
7171
action: actionGroup.setCustomField,
72-
priceId,
72+
...options,
7373
name,
7474
value: extractFieldValue(newObj.custom.fields, name),
7575
}))

0 commit comments

Comments
 (0)