UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

65 lines 5.6 kB
import{createSelector}from'reselect';import{isObject}from"../helpers/validation";import authRoutes from"../collections/AuthRoutes";/** * @param {Object} state The application state. * @return {Object} */export var getRouterState=function getRouterState(state){return state.router;};/** * @param {Object} state The application state. * @return {Array} */export var getRouterStack=createSelector(getRouterState,function(state){return state&&state.stack?state.stack:[];});/** * @param {Object} state The application state. * @returns {Object|null} */export var getCurrentRoute=createSelector(getRouterState,getRouterStack,function(state){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.routeId;},function(router,stack,routeId){if(!router||!router.currentRoute){return null;}if(!routeId){return router.currentRoute;}return stack.find(function(entry){return entry.id===routeId;});});/** * @param {Object} state The application state. * @returns {Object|null} */export var getCurrentParams=createSelector(getCurrentRoute,function(route){if(!route||!route.params){return null;}return route.params;});/** * @param {Object} state The application state. * @returns {string|null} The current history pathname. */export var getCurrentPathname=createSelector(getCurrentRoute,function(route){if(!route||!route.pathname){return null;}return route.pathname;});/** * @param {Object} state The application state. * @returns {Object|null} The current history query. */export var getCurrentQuery=createSelector(getCurrentRoute,function(route){if(!route||!route.query){return null;}return route.query;});/** * @param {Object} state The application state. * @returns {string|null} The current history search query. */export var getCurrentSearchQuery=createSelector(getCurrentQuery,function(query){if(!query||!query.s){return null;}return query.s;});/** * @param {Object} state The application state. * @returns {string|null} The current history entry state. */export var getCurrentState=createSelector(getCurrentRoute,function(route){if(!route||!route.state){return null;}return route.state;});/** * Determines, if a router state entry is the last entry in the stack. * @param {string} id The id of the entry. * @returns {Function} */export function makeIsLastStackEntry(){return createSelector(function(state){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.routeId;},getRouterStack,function(routeId,stack){var index=stack.findIndex(function(entry){return entry.id===routeId;});return index>=0&&index===stack.length-1;});}/** * Get the previous route from stack. * @returns {Function} */export function makeGetPrevRoute(){return createSelector(function(state){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.routeId;},getRouterStack,function(routeId,stack){var routeIndex=stack.findIndex(function(entry){return entry.id===routeId;});if(routeIndex<=0){return null;}return stack[routeIndex-1];});}/** * Creates a selector that retrieves the pattern of the route. * @returns {Function} */export var makeGetRoutePattern=function makeGetRoutePattern(){return(/** * Retrieves the route pattern. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {string|null} The pattern. */createSelector(getCurrentRoute,function(route){if(!route||!route.pattern){return null;}return route.pattern;}));};/** * Creates a selector that retrieves the value of a specific parameter from the route. * @param {string} name The name of the desired parameter. * @returns {Function} */export var makeGetRouteParam=function makeGetRouteParam(){var name=arguments.length>0&&arguments[0]!==undefined?arguments[0]:'';return(/** * Retrieves a parameter from the route. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {string|null} The parameter value. */createSelector(getCurrentParams,function(params){if(!isObject(params)){return null;}return params[name]||null;}));};/** * Creates a selector that retrieves the index of the current or a specific route when passed * within the props. * @returns {number|null} */export var getRouterStackIndex=createSelector(getRouterStack,getCurrentRoute,function(routerStack,currentRoute){if(!currentRoute){return null;}var currentId=currentRoute.id;var index=routerStack.findIndex(function(_ref){var id=_ref.id;return currentId===id;});return index>=0?index:null;});/** * Creates a selector that retrieves the index of the most next previous route with * the desired pattern within the stack * @param {string} pattern The desired route pattern * @returns {Function} */export var makeGetPrevRouteIndexByPattern=function makeGetPrevRouteIndexByPattern(pattern){return(/** * @param {Object} state The application state. * @param {Object} props The component props. * @returns {Object} The route. */createSelector(getRouterStack,getRouterStackIndex,function(routerStack,routerStackIndex){var sliced=routerStack.slice(0,routerStackIndex);var reversedIndex=sliced.reverse().findIndex(function(route){return route.pattern===pattern;});return reversedIndex===-1?reversedIndex:sliced.length-1-reversedIndex;}));};/** * Creates a selector to determine if the current active route is "protected" (needs login). * @returns {Function} */export var makeGetIsCurrentRouteProtected=function makeGetIsCurrentRouteProtected(){return createSelector(getCurrentPathname,function(pattern){return!!authRoutes.getProtector(pattern);});};