mutations.js
1.75 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
import * as Types from './types';
import { find } from 'lodash';
import { defaultProduct } from './index';
function ensureProduct(state, productId) {
if (state.products[productId] === undefined) {
state.products = {...state.products, [productId]: defaultProduct()};
}
}
function ensureSelectedProduct(state, productId) {
if (!state.selectedProductInfo || state.selectedProductInfo.productId !== productId) {
ensureProduct(state, productId);
state.selectedProductInfo = {
productId,
product: state.products[productId],
sizeId: null,
storageId: null,
size: {},
};
}
}
export default {
[Types.ENSURE_PRODUCT_DETAIL](state, { productId }) {
ensureProduct(state, productId);
},
[Types.UPDATE_PRODUCT_DETAIL](state, payload) {
state.products = {...state.products, [payload.product_id]: payload};
ensureSelectedProduct(state, state.product.product_id);
},
[Types.UPDATE_PRODUCT_TOP3](state, { productId, topList }) {
state.topLists = {...state.topLists, [productId]: topList};
},
[Types.UPDATE_PRODUCT_FAV](state, { productId, isFav }) {
state.fav = {...state.fav, [productId]: isFav};
},
[Types.UPDATE_SELECTED_PRODUCT_SIZE](state, { productId, sizeId, storageId }) {
ensureSelectedProduct(state, productId);
const sizeList = state.selectedProductInfo.product.goods_list[0].size_list;
let sizeInfo;
if (storageId) {
sizeInfo = find(sizeList, (size) => size.storage_id === storageId);
} else {
sizeInfo = find(sizeList, (size) => size.size_id === sizeId);
}
if (sizeInfo) {
state.selectedProductInfo.sizeId = sizeInfo.size_id;
state.selectedProductInfo.storageId = sizeInfo.storage_id;
}
state.selectedProductInfo.size = sizeInfo;
},
};