model.ts 1.42 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import { Reducer, AnyAction } from 'redux';
陈帅's avatar
陈帅 committed
2
import { EffectsCommandMap } from 'dva';
陈帅's avatar
陈帅 committed
3 4 5

import { ListItemDataType } from './data';
import { queryFakeList } from './service';
6

陈帅's avatar
陈帅 committed
7 8 9 10 11 12
export interface IStateType {
  list: ListItemDataType[];
}

export type Effect = (
  action: AnyAction,
陈帅's avatar
陈帅 committed
13
  effects: EffectsCommandMap & { select: <T>(func: (state: IStateType) => T) => T },
陈帅's avatar
陈帅 committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
) => void;

export interface ModelType {
  namespace: string;
  state: IStateType;
  effects: {
    fetch: Effect;
    appendFetch: Effect;
  };
  reducers: {
    queryList: Reducer<IStateType>;
    appendList: Reducer<IStateType>;
  };
}

const Model: ModelType = {
30
  namespace: 'BLOCK_NAME_CAMEL_CASE',
31 32 33 34 35 36

  state: {
    list: [],
  },

  effects: {
afc163's avatar
afc163 committed
37
    *fetch({ payload }, { call, put }) {
38 39 40 41 42 43
      const response = yield call(queryFakeList, payload);
      yield put({
        type: 'queryList',
        payload: Array.isArray(response) ? response : [],
      });
    },
JoeyKo's avatar
JoeyKo committed
44 45 46 47 48 49 50
    *appendFetch({ payload }, { call, put }) {
      const response = yield call(queryFakeList, payload);
      yield put({
        type: 'appendList',
        payload: Array.isArray(response) ? response : [],
      });
    },
51 52 53
  },

  reducers: {
54 55 56 57 58 59
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
JoeyKo's avatar
JoeyKo committed
60 61 62
    appendList(state, action) {
      return {
        ...state,
陈帅's avatar
陈帅 committed
63
        list: state!.list.concat(action.payload),
JoeyKo's avatar
JoeyKo committed
64 65
      };
    },
66 67
  },
};
陈帅's avatar
陈帅 committed
68 69

export default Model;