user.ts 1.59 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
export interface CurrentUser {
  avatar?: string;
  name?: string;
  title?: string;
  group?: string;
  signature?: string;
  geographic?: any;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
12 13 14 15
  tags?: {
    key: string;
    label: string;
  }[];
何乐's avatar
何乐 committed
16 17 18
  unreadCount?: number;
}

19
export interface UserModelState {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
20
  currentUser?: CurrentUser;
21 22 23 24 25 26 27 28 29 30
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    fetch: Effect;
    fetchCurrent: Effect;
  };
  reducers: {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
31 32
    saveCurrentUser: Reducer<UserModelState>;
    changeNotifyCount: Reducer<UserModelState>;
33 34 35 36
  };
}

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

  state: {
    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
      const response = yield call(queryCurrent);
      yield put({
        type: 'saveCurrentUser',
        payload: response,
      });
    },
  },

  reducers: {
    saveCurrentUser(state, action) {
      return {
        ...state,
afc163's avatar
afc163 committed
64
        currentUser: action.payload || {},
65 66
      };
    },
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
67 68 69 70 71 72
    changeNotifyCount(
      state = {
        currentUser: {},
      },
      action,
    ) {
afc163's avatar
afc163 committed
73 74 75 76
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
77 78
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
afc163's avatar
afc163 committed
79 80 81
        },
      };
    },
82 83
  },
};
84 85

export default UserModel;