@@ -10,7 +10,6 @@ import {
1010} from './utils/common-actions'
1111import createBuildArrayActions , {
1212 ADD_ACTIONS ,
13- CHANGE_ACTIONS ,
1413 REMOVE_ACTIONS ,
1514} from './utils/create-build-array-actions'
1615import 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+
2936export 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
518569export 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
542604export function actionsMapAttributes (
0 commit comments