import { EffectsCommandMap } from 'dva'; import { Reducer, AnyAction } from 'redux'; import { queryCurrent, queryProjectNotice, queryActivities, fakeChartData } from './service'; import { CurrentUser, Notice, Activeties, RadarData } from './data'; export interface ModalState { currentUser: Partial; projectNotice: Notice[]; activities: Activeties[]; radarData: RadarData[]; } export type Effect = ( action: AnyAction, effects: EffectsCommandMap & { select: (func: (state: ModalState) => T) => T } ) => void; export interface ModelType { namespace: string; state: ModalState; reducers: { save: Reducer; clear: Reducer; }; effects: { init: Effect; fetchUserCurrent: Effect; fetchProjectNotice: Effect; fetchActivitiesList: Effect; fetchChart: Effect; }; } const Model: ModelType = { namespace: 'BLOCK_NAME_CAMEL_CASE', state: { currentUser: {}, projectNotice: [], activities: [], radarData: [], }, 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({ type: 'save', payload: { currentUser: response, }, }); }, *fetchProjectNotice(_, { call, put }) { const response = yield call(queryProjectNotice); yield put({ type: 'save', payload: { projectNotice: Array.isArray(response) ? response : [], } }); }, *fetchActivitiesList(_, { call, put }) { const response = yield call(queryActivities); yield put({ type: 'save', payload: { activities: Array.isArray(response) ? response : [], } }); }, *fetchChart(_, { call, put }) { const { radarData } = yield call(fakeChartData); yield put({ type: 'save', payload: { radarData, }, }); }, }, reducers: { save(state, { payload }) { return { ...state, ...payload, }; }, clear() { return { currentUser: {}, projectNotice: [], activities: [], radarData: [], }; }, }, }; export default Model;