list.js 945 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 9
    loading: false,
    cursor: 0,
10 11 12
  },

  effects: {
afc163's avatar
afc163 committed
13
    *fetch({ payload }, { call, put }) {
14 15 16 17 18 19
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryFakeList, payload);
      yield put({
afc163's avatar
afc163 committed
20
        type: 'appendList',
afc163's avatar
afc163 committed
21
        payload: Array.isArray(response) ? response : [],
22 23 24 25 26 27 28 29 30
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
  },

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