list.js 1.28 KB
Newer Older
1 2 3 4 5 6 7
import { queryFakeList } from '../services/api';

export default {
  namespace: 'list',

  state: {
    list: [],
afc163's avatar
afc163 committed
8
    loading: false,
9 10 11
  },

  effects: {
afc163's avatar
afc163 committed
12
    *fetch({ payload }, { call, put }) {
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryFakeList, payload);
      yield put({
        type: 'queryList',
        payload: Array.isArray(response) ? response : [],
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *appendFetch({ payload }, { call, put }) {
28 29 30 31 32 33
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryFakeList, payload);
      yield put({
afc163's avatar
afc163 committed
34
        type: 'appendList',
afc163's avatar
afc163 committed
35
        payload: Array.isArray(response) ? response : [],
36 37 38 39 40 41 42 43 44
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
  },

  reducers: {
45 46 47 48 49 50
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
afc163's avatar
afc163 committed
51
    appendList(state, action) {
52 53
      return {
        ...state,
afc163's avatar
afc163 committed
54
        list: state.list.concat(action.payload),
55 56 57 58 59 60 61 62 63 64
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};