{"size": 946, "ext": "js", "max_stars_repo_path": "app/containers/App/reducer.js", "max_stars_repo_name": "Statfine/react-template-qt", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/containers/App/reducer.js", "max_issues_repo_name": "Statfine/react-template-qt", "max_issues_repo_issues_event_min_datetime": "2020-12-07T09:50:34.000Z", "max_issues_repo_issues_event_max_datetime": "2020-12-07T09:50:34.000Z", "max_forks_repo_path": "app/containers/App/reducer.js", "max_forks_repo_name": "Statfine/react-template-qt", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 19.306122449, "max_line_length": 83, "alphanum_fraction": 0.6543340381}
/*
 *
 * Form reducer
 *
 */

import produce from 'immer';
import { getLocal } from 'utility/localStorageCookie';
import _ from 'lodash';
import * as cons from './constants';

/*
 * logined Auth用户验证
*/
// 验证用户有效的条件是 必须有access_token 同时有效期大于当前
function isLogined() {
  return !!getLocal('access_token') && Date.now() < Number(getLocal('expires_in'));
}

/**
 *  logined 是否已有用户
 *  userInfo 用户信息
 *  promptInfo 全局提示信息
 */
export const initialState = {
  logined: isLogined(),
  userInfo: {
    user_id: '',
  },
  promptInfo: {
    promptMsg: '',
    promptType: 0,
  },
};

/* eslint-disable default-case, no-param-reassign */
const loginReducer = produce((draft, action) => {
  switch (action.type) {
    case cons.LOGIN_CHANGE:
      draft.logined = action.payload;
      break;
    case cons.FETCH_USERINFO_SUC:
      draft.userInfo = _.merge({}, draft.userInfo, action.payload);
      break;
  }
}, initialState);

export default loginReducer;
