user.ts 1.61 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

5 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
export interface UserModelState {
  list: any[];
  currentUser: {
    avatar?: string;
    name?: string;
    title?: string;
    group?: string;
    signature?: string;
    geographic?: any;
    tags?: any[];
    unreadCount?: number;
  };
}

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 = {
34 35 36 37 38 39 40 41
  namespace: 'user',

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

  effects: {
afc163's avatar
afc163 committed
42
    *fetch(_, { call, put }) {
43 44 45 46 47 48
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },
afc163's avatar
afc163 committed
49
    *fetchCurrent(_, { call, put }) {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
      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
68
        currentUser: action.payload || {},
69 70
      };
    },
afc163's avatar
afc163 committed
71 72 73 74 75
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
76 77
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
afc163's avatar
afc163 committed
78 79 80
        },
      };
    },
81 82
  },
};
83 84

export default UserModel;