list.js 915 Bytes
Newer Older
sorrycc's avatar
sorrycc committed
1
import { queryFakeList, removeFakeList, addFakeList, updateFakeList } from '@/services/api';
2 3 4 5 6 7 8 9 10

export default {
  namespace: 'list',

  state: {
    list: [],
  },

  effects: {
afc163's avatar
afc163 committed
11
    *fetch({ payload }, { call, put }) {
12 13 14 15 16 17
      const response = yield call(queryFakeList, payload);
      yield put({
        type: 'queryList',
        payload: Array.isArray(response) ? response : [],
      });
    },
valleykid's avatar
valleykid committed
18 19 20 21 22 23 24 25
    *submit({ payload }, { call, put }) {
      let callback;
      if (payload.id) {
        callback = Object.keys(payload).length === 1 ? removeFakeList : updateFakeList;
      } else {
        callback = addFakeList;
      }
      const response = yield call(callback, payload); // post
26
      yield put({
valleykid's avatar
valleykid committed
27 28
        type: 'queryList',
        payload: response,
29 30 31 32 33
      });
    },
  },

  reducers: {
34 35 36 37 38 39
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
40 41
  },
};