mutations.js 1.41 KB
import * as Types from './types';
import {get, find } from 'lodash';

function ensureSelectedProduct(state, productId) {
  if (!state.selectedProductInfo || state.selectedProductInfo.productId !== productId) {
    state.selectedProductInfo = {
      productId,
      product: state.products.byId[productId],
      sizeId: null,
      size: {},
      tradeTypeId: null,
      tradeType: {}
    };
  }
}

export default {
  [Types.UPDATE_PRODUCT_DETAIL](state, payload) {
    state.products.byId[state.currentId] = null;
    state.currentId = payload.product_id;
    state.products.byId[state.currentId] = payload;
    ensureSelectedProduct(state, state.currentId);
  },
  [Types.UPDATE_PRODUCT_FAV](state, { productId, isFav}) {
    if (state.products.byId[productId]) {
      state.products.byId[productId].isFav = isFav;
    }
  },
  [Types.UPDATE_SELECTED_RPODUCT_SIZE](state, { productId, sizeId}) {
    ensureSelectedProduct(state, productId);
    state.selectedProductInfo.sizeId = sizeId;
    const sizeList = get(state.selectedProductInfo.product, 'goods_list[0].size_list', []);

    state.selectedProductInfo.size = find(sizeList, (size) => size.size_id === sizeId) || {};
  },
  [Types.UPDATE_SELECTED_TRADE_TYPE](state, { productId, tradeTypeId}) {
    ensureSelectedProduct(state, productId);

    state.selectedProductInfo.tradeTypeId = tradeTypeId;
    state.selectedProductInfo.tradeType = {}; // FIXME: 查询交易类型
  },
};