list.js 1.24 KB
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 : [],
      });
    },
JoeyKo's avatar
JoeyKo committed
18 19 20 21 22 23 24
    *appendFetch({ payload }, { call, put }) {
      const response = yield call(queryFakeList, payload);
      yield put({
        type: 'appendList',
        payload: Array.isArray(response) ? response : [],
      });
    },
valleykid's avatar
valleykid committed
25 26 27 28 29 30 31 32
    *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
33
      yield put({
valleykid's avatar
valleykid committed
34 35
        type: 'queryList',
        payload: response,
36 37 38 39 40
      });
    },
  },

  reducers: {
41 42 43 44 45 46
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
JoeyKo's avatar
JoeyKo committed
47 48 49 50 51 52
    appendList(state, action) {
      return {
        ...state,
        list: state.list.concat(action.payload),
      };
    },
53 54
  },
};