model.ts 1.43 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import { AnyAction, Reducer } from 'redux';
陈帅's avatar
陈帅 committed
2

陈帅's avatar
陈帅 committed
3
import { EffectsCommandMap } from 'dva';
陈帅's avatar
陈帅 committed
4
import { ListItemDataType } from './data.d';
陈帅's avatar
陈帅 committed
5
import { queryFakeList } from './service';
6

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

export type Effect = (
  action: AnyAction,
陈帅's avatar
陈帅 committed
13
  effects: EffectsCommandMap & { select: <T>(func: (state: StateType) => T) => T },
陈帅's avatar
陈帅 committed
14 15 16 17
) => void;

export interface ModelType {
  namespace: string;
陈帅's avatar
陈帅 committed
18
  state: StateType;
陈帅's avatar
陈帅 committed
19 20 21 22 23
  effects: {
    fetch: Effect;
    appendFetch: Effect;
  };
  reducers: {
陈帅's avatar
陈帅 committed
24 25
    queryList: Reducer<StateType>;
    appendList: Reducer<StateType>;
陈帅's avatar
陈帅 committed
26 27 28 29
  };
}

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 as StateType).list.concat(action.payload),
JoeyKo's avatar
JoeyKo committed
64 65
      };
    },
66 67
  },
};
陈帅's avatar
陈帅 committed
68 69

export default Model;