list.js 814 Bytes
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
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryFakeList, payload);
      yield put({
afc163's avatar
afc163 committed
19
        type: 'appendList',
afc163's avatar
afc163 committed
20
        payload: Array.isArray(response) ? response : [],
21 22 23 24 25 26 27 28 29
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
  },

  reducers: {
afc163's avatar
afc163 committed
30
    appendList(state, action) {
31 32
      return {
        ...state,
afc163's avatar
afc163 committed
33
        list: state.list.concat(action.payload),
34 35 36 37 38 39 40 41 42 43
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};