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

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

export default {
  [Types.UPDATE_PRODUCT_DETAIL](state, payload) {
    state.product = payload;
    ensureSelectedProduct(state, state.product.product_id);
  },
  [Types.UPDATE_PRODUCT_FAV](state, { productId, isFav}) {
    if (state.product.product_id === productId) {
      state.product.isFav = isFav;
    }
  },
  [Types.UPDATE_SELECTED_RPODUCT_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;
  },
  [Types.UPDATE_SELECTED_TRADE_TYPE](state, { productId, tradeTypeId}) {
    ensureSelectedProduct(state, productId);

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