import { getGoodsHotDataAPI, getGoodsHotPageDataAPI } from "../services/api";
import { getCustomer } from "@/utils/request";

export default {
  namespace: "goodsHot",

  state: {
    goodsHotPageData: {},
    goodsHotData: []
  },

  effects: {
    // 请求轮播图片
    *getGoodsHotData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customerId = getCustomer().id;
      const response = yield call(getGoodsHotDataAPI, payload);
      yield put({
        type: "goodsHotData",
        goodsHotData: response
      });
    },
    *getGoodsHotPageData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customerId = getCustomer().id;
      const response = yield call(getGoodsHotPageDataAPI, payload);
      if (response && response.content) {
        response.content.forEach(item => item.orderNum = 1);
      }
      yield put({
        type: "goodsHotPageData",
        goodsHotPageData: response
      });
    }
  },

  reducers: {
    goodsHotData(state, payload) {
      return {
        ...state,
        goodsHotData: payload.goodsHotData
      };
    },
    goodsHotPageData(state, payload) {
      return {
        ...state,
        goodsHotPageData: payload.goodsHotPageData
      };
    }
  }
};
