| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import type { Effect, Reducer } from 'umi';
- import { queryCurrent, query as queryUsers } from '@/services/user';
- export type CurrentUser = {
- avatar?: string;
- name?: string;
- title?: string;
- group?: string;
- signature?: string;
- tags?: {
- key: string;
- label: string;
- }[];
- userid?: string;
- unreadCount?: number;
- };
- export type UserModelState = {
- currentUser?: CurrentUser;
- };
- export type UserModelType = {
- namespace: 'user';
- state: UserModelState;
- effects: {
- fetch: Effect;
- fetchCurrent: Effect;
- };
- reducers: {
- saveCurrentUser: Reducer<UserModelState>;
- changeNotifyCount: Reducer<UserModelState>;
- };
- };
- const UserModel: UserModelType = {
- namespace: 'user',
- state: {
- currentUser: {},
- },
- effects: {
- *fetch(_, { call, put }) {
- const response = yield call(queryUsers);
- yield put({
- type: 'save',
- payload: response,
- });
- },
- *fetchCurrent(_, { call, put }) {
- const response = yield call(queryCurrent);
- yield put({
- type: 'saveCurrentUser',
- payload: response,
- });
- },
- },
- reducers: {
- saveCurrentUser(state, action) {
- return {
- ...state,
- currentUser: action.payload || {},
- };
- },
- changeNotifyCount(
- state = {
- currentUser: {},
- },
- action,
- ) {
- return {
- ...state,
- currentUser: {
- ...state.currentUser,
- notifyCount: action.payload.totalCount,
- unreadCount: action.payload.unreadCount,
- },
- };
- },
- },
- };
- export default UserModel;
|