@shopgate/pwa-common
Version:
Common library for the Shopgate Connect PWA.
6 lines • 1.65 kB
JavaScript
function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import{APP_DID_START,REQUEST_LOGIN,SUCCESS_LOGIN,TOGGLE_LOGGED_IN,ERROR_LOGIN,SUCCESS_LOGOUT,DISABLE_LOGIN}from"../../constants/ActionTypes";var defaultState={isFetching:false,disabled:false,isLoggedIn:false,errors:null,strategy:null,expires:null};/**
* Stores the login state
* @param {Object} [state] The current state.
* @param {Object} action The action object.
* @return {Object} The new state.
*/export default function userLoginReducer(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:defaultState;var action=arguments.length>1?arguments[1]:undefined;switch(action.type){case APP_DID_START:return _extends({},state,{disabled:false});case DISABLE_LOGIN:return _extends({},state,{disabled:action.value});case REQUEST_LOGIN:return _extends({},state,{strategy:action.strategy,isFetching:true});case SUCCESS_LOGIN:return _extends({},state,{isLoggedIn:true,errors:null,isFetching:false,strategy:action.strategy},typeof action.sessionLifetimeInSeconds==='number'?{expires:new Date().getTime()+action.sessionLifetimeInSeconds*1000}:{expires:null});case ERROR_LOGIN:return _extends({},state,{errors:action.messages,isLoggedIn:false,isFetching:false});case TOGGLE_LOGGED_IN:return _extends({},state,{isLoggedIn:action.value,strategy:null},action.value===false?{expires:null}:null);case SUCCESS_LOGOUT:return defaultState;default:return state;}}