/*
 * @Author: your name
 * @Date: 2021-04-23 15:39:41
 * @LastEditTime: 2021-04-28 16:02:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /react-antd-admin-template/src/store/actions/user.js
 */
import * as types from "../action-types";
// import { reqUserInfo } from "@/api/user";

export const getUserInfo = (data) => (dispatch) => {
  return new Promise((resolve, reject) => {
    // console.log({'getUserInfo':data});
    if (data) {
      const userInfo = {
        ...data,
        role:data.roles,
        name:data.name
      };
      dispatch(setUserInfo(userInfo));
      resolve(data);
    } else {
      const userInfo = localStorage.getItem('userInfo');
      resolve(JSON.parse(userInfo));
      // const msg = data.message;
      // reject(msg);
    }
    // reqUserInfo(token)
    //   .then((response) => {
    //     const { data } = response;
        
    //   })
    //   .catch((error) => {
    //     reject(error);
    //   });
  });
};

export const setUserToken = (token) => {
  return {
    type: types.USER_SET_USER_TOKEN,
    token,
  };
};

export const setUserInfo = (userInfo) => {
  return {
    type: types.USER_SET_USER_INFO,
    ...userInfo,
  };
};

export const resetUser = () => {
  return {
    type: types.USER_RESET_USER,
  };
};
