UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

43 lines 2.87 kB
import{createSelector}from'reselect';import{makeGetDefaultSortOrder}from'@shopgate/engage/filter/selectors';import{getCurrentPathname,getCurrentQuery,getRouterStack}from"./router";import{parseObjectToQueryString}from"../helpers/router";/** * @deprecated */ /** * Selects the history state. * @param {Object} state The global state. * @deprecated * @return {Object} */export var getHistoryState=function getHistoryState(state){return state.history;};/** * Retrieves a single url parameter from the query parameters object. * @param {Object} state The global state. * @param {string} param The dedicated url parameter. * @return {*} The URL parameter value. */export var getQueryParam=createSelector(getCurrentQuery,function(state,props,param){return param;},function(params,param){if(!params||!params[param]){return null;}return params[param];});var getDefaultSortOrder=makeGetDefaultSortOrder();/** * Retrieves the sort order from the URL query parameters. * @param {Object} state The global state. * @returns {string} The current sort order. */export var getSortOrder=createSelector(function(state,props){return getQueryParam(state,props,'sort');},getDefaultSortOrder,function(sortParam,defaultSort){return sortParam||defaultSort;});/** * Retrieves the search phrase from the URL query parameters. * @param {Object} state The global state. * @returns {string|null} The current search phrase. */export var getSearchPhrase=createSelector(function(state,props){return getQueryParam(state,props,'s');},function(param){return param?param.trim():null;});/** * Gets the current history pathname. * @deprecated * @param {Object} state The current application state. * @returns {string} */export var getHistoryPathname=getCurrentPathname;/** * Gets the length of the current history stack. * @param {Object} state The current application state. * @return {number} */export var getHistoryLength=createSelector(getRouterStack,function(stack){return stack.length;});/** * Gets the current query params from history state as a preformatted string. * @param {Object} state The global state. * @return {string} */export var getQueryParamsAsString=createSelector(getCurrentQuery,function(queryParams){return parseObjectToQueryString(queryParams);});/** * Gets the current history location from the history state. * @param {Object} state The current application state. * @return {string|null} */export var getHistoryLocation=createSelector(getHistoryPathname,getQueryParamsAsString,function(pathname,params){return"".concat(pathname).concat(params);});/** * Gets the current redirectLocation from the history state. * @param {Object} state The current application state. * @deprecated * @return {string|null} */export var getRedirectLocation=createSelector(getHistoryState,function(historyState){return historyState.redirectLocation||null;});