-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathproducts.js
More file actions
95 lines (76 loc) · 2.67 KB
/
products.js
File metadata and controls
95 lines (76 loc) · 2.67 KB
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
/* @flow */
import type {
SyncAction,
ActionGroup,
} from 'types/sdk'
import flatten from 'lodash.flatten'
import createBuildActions from './utils/create-build-actions'
import createMapActionGroup from './utils/create-map-action-group'
import * as productActions from './product-actions'
import * as diffpatcher from './utils/diffpatcher'
const actionGroups = [
'base',
'meta',
'references',
'prices',
'attributes',
'images',
'variants',
'categories',
'categoryOrderHints',
]
function createProductMapActions (mapActionGroup) {
return function doMapActions (diff, newObj, oldObj, options) {
const allActions = []
const { sameForAllAttributeNames } = options
allActions.push(mapActionGroup('base', () =>
productActions.actionsMapBase(diff, oldObj, newObj)))
allActions.push(mapActionGroup('meta', () =>
productActions.actionsMapMeta(diff, oldObj, newObj)))
allActions.push(mapActionGroup('references', () =>
productActions.actionsMapReferences(diff, oldObj, newObj)))
allActions.push(mapActionGroup('variants', () =>
productActions.actionsMapVariants(diff, oldObj, newObj)))
const changeMasterVariantAction =
productActions.generateChangeMasterVariantAction(oldObj, newObj)
if (changeMasterVariantAction) allActions.push(changeMasterVariantAction)
allActions.push(mapActionGroup('attributes', () =>
productActions.actionsMapAttributes(diff, oldObj, newObj,
sameForAllAttributeNames || [])))
allActions.push(mapActionGroup('images', () =>
productActions.actionsMapImages(diff, oldObj, newObj)))
allActions.push(mapActionGroup('prices', () =>
productActions.actionsMapPrices(diff, oldObj, newObj)))
allActions.push(mapActionGroup('categories', () =>
productActions.actionsMapCategories(diff)))
allActions.push(mapActionGroup('categories', () =>
productActions.actionsMapCategoryOrderHints(diff, oldObj)))
return flatten(allActions)
}
}
function moveMasterVariantsIntoVariants (before, now) {
const move = obj => ({
...obj,
masterVariant: undefined,
variants: [
obj.masterVariant,
...obj.variants || [],
],
})
const hasMasterVariant = obj => obj && obj.masterVariant
return [
hasMasterVariant(before) ? move(before) : before,
hasMasterVariant(now) ? move(now) : now,
]
}
export default (config: Array<ActionGroup>): SyncAction => {
const mapActionGroup = createMapActionGroup(config)
const doMapActions = createProductMapActions(mapActionGroup)
const buildActions = createBuildActions(
diffpatcher.diff,
doMapActions,
moveMasterVariantsIntoVariants,
)
return { buildActions }
}
export { actionGroups }