import { getGoodsOptAPI,
  getGoodsOptsResultAPI,
  saveGoodsOptsResultAPI
} from "../services/api";

export default {
  namespace: "goodsOpts",

  state: {
    goodsOpts: [],
    goodsOptsResult: [],
    optSaveReturnData: []
  },

  effects: {
    *getGoodsOpt({ payload }, { call, put }) {
      const response = yield call(getGoodsOptAPI, payload);
      yield put({
        type: "goodsOpts",
        goodsOpts: response
      });
    },
    *getGoodsOptsResult({ payload }, { call, put }) {
      const response = yield call(getGoodsOptsResultAPI, payload);
      yield put({
        type: "goodsOptsResult",
        goodsOptsResult: response
      });
    },
    *saveGoodsOpts({ payload }, { call, put }) {
      const response = yield call(saveGoodsOptsResultAPI, payload);
      yield put({
        type: "optSaveReturnData",
        optSaveReturnData: response
      });
    }
  },

  reducers: {
    goodsOpts(state, payload) {
      return {
        ...state,
        goodsOpts: payload.goodsOpts
      };
    },
    goodsOptsResult(state, payload) {
      return {
        ...state,
        goodsOptsResult: payload.goodsOptsResult
      };
    },
    optSaveReturnData(state, payload) {
      return {
        ...state,
        optSaveReturnData: payload.optSaveReturnData
      };
    },
  }
};
