import request from '../utils/request';
import apis from '../services/apis';

const UserModel = {
  namespace: 'user',
  state: {
    currentUser: {},
    menuList: [],
  },
  effects: {
    // *fetch(_, { call, put }) {
    //   const response = yield call(queryUsers);
    //   yield put({
    //     type: 'save',
    //     payload: response,
    //   });
    // },

    *fetchCurrent(_, { call, put }) {
      const response = yield call(request, apis.user.current);

      yield put({
        type: 'saveCurrentUser',
        payload: response,
      });
    },
  },
  reducers: {
    saveCurrentUser(state, { payload }) {
      const { taUser = {} , menuList = [] } = payload || {}
      const currentUser = { ...taUser, roles: (taUser.roles || []).map(x => x.roleId) }

      return { ...state, currentUser, menuList };
    },
    changeNotifyCount(
      state = {
        currentUser: {},
      },
      action,
    ) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
        },
      };
    },
  },
};
export default UserModel;
