user.ts 1.63 KB
Newer Older
ζ„šι“'s avatar
ζ„šι“ committed
1
import { query as queryUsers, queryCurrent } from '@/services/user';
2 3
import { Effect } from 'dva';
import { Reducer } from 'redux';
4

何乐's avatar
何乐 committed
5 6 7 8 9 10 11 12 13 14 15
export interface CurrentUser {
  avatar?: string;
  name?: string;
  title?: string;
  group?: string;
  signature?: string;
  geographic?: any;
  tags?: any[];
  unreadCount?: number;
}

16 17
export interface UserModelState {
  list: any[];
何乐's avatar
何乐 committed
18
  currentUser: CurrentUser;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    fetch: Effect;
    fetchCurrent: Effect;
  };
  reducers: {
    save: Reducer<any>;
    saveCurrentUser: Reducer<any>;
    changeNotifyCount: Reducer<any>;
  };
}

const UserModel: UserModelType = {
36 37 38 39 40 41 42 43
  namespace: 'user',

  state: {
    list: [],
    currentUser: {},
  },

  effects: {
afc163's avatar
afc163 committed
44
    *fetch(_, { call, put }) {
45 46 47 48 49 50
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },
afc163's avatar
afc163 committed
51
    *fetchCurrent(_, { call, put }) {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
      const response = yield call(queryCurrent);
      yield put({
        type: 'saveCurrentUser',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
afc163's avatar
afc163 committed
70
        currentUser: action.payload || {},
71 72
      };
    },
afc163's avatar
afc163 committed
73 74 75 76 77
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
78 79
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
afc163's avatar
afc163 committed
80 81 82
        },
      };
    },
83 84
  },
};
85 86

export default UserModel;