UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

45 lines 2.71 kB
import{createSelector}from'reselect';import{getUrl}from"./url";/** * @param {Object} state The current application state. * @return {Object} */export var getUserState=function getUserState(state){return state.user;};/** * Gets user.login from the redux store. * @param {Object} state The application state. * @return {Object|null} */export var getLoginData=createSelector(getUserState,function(user){if(!user||!user.login){return null;}return user.login;});/** * @param {Object} state The current application state. * @return {Object|null} * @deprecated */export var getUserLogin=getLoginData;/** * @param {Object} state The current application state. * @return {Object} */export var getUserData=createSelector(getUserState,function(user){if(!user.data){return null;}return user.data;});/** * Selects the isLoggedIn state from the user. * @param {Object} state The global state. * @return {boolean} */export var isUserLoggedIn=createSelector(getLoginData,function(login){if(!login){return false;}return login.isLoggedIn;});/** * @param {Object} state The global state. * @returns {string} */export var getUserDisplayName=createSelector(getUserData,function(data){if(!data){return null;}return[data.firstName,data.lastName].join(' ').trim();});/** * @param {Object} state The global state. * @returns {string} */export var getUserFirstName=createSelector(getUserData,function(data){if(!data){return null;}return data.firstName;});/** * @param {Object} state The global state. * @returns {string} */export var getUserEmail=createSelector(getUserData,function(data){if(!data){return null;}return data.mail||null;});/** * Selects the disabled state of the login action from the redux store. * @param {Object} state The application state. * @return {Object|null} */export var isUserLoginDisabled=createSelector(getLoginData,function(login){if(!login){return false;}return login.disabled;});/** * Gets the register url. * @param {Object} state The application state. * @return {string|null} */export var getRegisterUrl=function getRegisterUrl(state){return getUrl(state,{type:'register'});};/** * Retrieves the expiry timestamp from the user login storage */export var getSessionExpiry=createSelector(getLoginData,function(loginData){return loginData.expires;});/** * Checks if the current session is expired. * * !!! CAUTION Not implemented with createSelector since selector caching conflicts with processing * the current timestamp * @param {Object} state The application state. * @returns {boolean} */export var getIsSessionExpired=function getIsSessionExpired(state){var expiry=getSessionExpiry(state);return typeof expiry==='number'&&new Date().getTime()>=expiry;};