model.ts 1.04 KB
Newer Older
1 2 3 4
import { queryCurrent } from './service';
import { CurrentUser } from './data';

export interface ModalState {
陈帅's avatar
陈帅 committed
5
  currentUser: Partial<CurrentUser>;
6 7
}

陈帅's avatar
陈帅 committed
8 9 10 11 12 13
import { Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { AnyAction } from 'redux';

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

export interface ModelType {
  namespace: string;
  state: ModalState;
  effects: {
    fetchCurrent: Effect;
  };
  reducers: {
    saveCurrentUser: Reducer<ModalState>;
  };
}

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

  state: {
    currentUser: {},
  },

  effects: {
陈帅's avatar
陈帅 committed
36
    *fetchCurrent(_, { call, put }) {
37 38 39 40 41 42 43 44 45
      const response = yield call(queryCurrent);
      yield put({
        type: 'saveCurrentUser',
        payload: response,
      });
    },
  },

  reducers: {
陈帅's avatar
陈帅 committed
46
    saveCurrentUser(state, action) {
47 48 49 50 51 52 53
      return {
        ...state,
        currentUser: action.payload || {},
      };
    },
  },
};
陈帅's avatar
陈帅 committed
54 55

export default Model;