model.ts 2.39 KB
Newer Older
愚道's avatar
愚道 committed
1 2
import { EffectsCommandMap } from 'dva';
import { Reducer, AnyAction } from 'redux';
3
import { queryCurrent, queryProjectNotice, queryActivities, fakeChartData } from './service';
愚道's avatar
愚道 committed
4
import { CurrentUser, Notice, Activeties, RadarData } from './data';
5

愚道's avatar
愚道 committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
export interface ModalState {
  currentUser: Partial<CurrentUser>;
  projectNotice: Notice[];
  activities: Activeties[];
  radarData: RadarData[];
}

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: ModalState) => T) => T }
) => void;

export interface ModelType {
  namespace: string;
  state: ModalState;
  reducers: {
    save: Reducer<ModalState>;
    clear: Reducer<ModalState>;
  };
  effects: {
    init: Effect;
    fetchUserCurrent: Effect;
    fetchProjectNotice: Effect;
    fetchActivitiesList: Effect;
    fetchChart: Effect;
  };
}

const Model: ModelType = {
35 36
  namespace: 'BLOCK_NAME_CAMEL_CASE',
  state: {
愚道's avatar
愚道 committed
37 38 39 40
    currentUser: {},
    projectNotice: [],
    activities: [],
    radarData: [],
41 42 43 44 45 46 47 48 49 50 51
  },
  effects: {
    *init(_, { put }) {
      yield put({ type: 'fetchUserCurrent' });
      yield put({ type: 'fetchProjectNotice' });
      yield put({ type: 'fetchActivitiesList' });
      yield put({ type: 'fetchChart' });
    },
    *fetchUserCurrent(_, { call, put }) {
      const response = yield call(queryCurrent);
      yield put({
愚道's avatar
愚道 committed
52 53 54 55
        type: 'save',
        payload: {
          currentUser: response,
        },
56 57 58 59 60
      });
    },
    *fetchProjectNotice(_, { call, put }) {
      const response = yield call(queryProjectNotice);
      yield put({
愚道's avatar
愚道 committed
61 62 63 64
        type: 'save',
        payload: {
          projectNotice: Array.isArray(response) ? response : [],
        }
65 66 67 68 69
      });
    },
    *fetchActivitiesList(_, { call, put }) {
      const response = yield call(queryActivities);
      yield put({
愚道's avatar
愚道 committed
70 71 72 73
        type: 'save',
        payload: {
          activities: Array.isArray(response) ? response : [],
        }
74 75 76
      });
    },
    *fetchChart(_, { call, put }) {
愚道's avatar
愚道 committed
77
      const { radarData } = yield call(fakeChartData);
78
      yield put({
愚道's avatar
愚道 committed
79 80 81 82
        type: 'save',
        payload: {
          radarData,
        },
83 84 85
      });
    },
  },
愚道's avatar
愚道 committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    clear() {
      return {
        currentUser: {},
        projectNotice: [],
        activities: [],
        radarData: [],
      };
    },
  },
102
};
愚道's avatar
愚道 committed
103 104

export default Model;