UNPKG

@snipsonian/redux-first-router

Version:

Redux-first-router snippets

72 lines (71 loc) 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getVirtualPageForRoute = exports.isChildRouteOf = exports.getTopParentRouteKey = exports.getCurrentRouteQueryParams = exports.getPropertyFromCurrentRoutePayload = exports.getCurrentRoutePayload = exports.getCurrentRouteInfo = exports.getCurrentRouteKey = exports.getReducerState = exports.setReducerKey = void 0; const isInteger_1 = require("@snipsonian/core/cjs/is/isInteger"); const routeManager_1 = require("../route/routeManager"); const parseVirtualPage_1 = require("./parseVirtualPage"); const NO_RERENDER = { EMPTY_OBJECT: {}, }; let reducerKey = 'NOT_SET_YET'; function setReducerKey(newReducerKey) { reducerKey = newReducerKey; } exports.setReducerKey = setReducerKey; function getReducerState(state) { return state[reducerKey]; } exports.getReducerState = getReducerState; function getCurrentRouteKey(state) { return getReducerState(state).type; } exports.getCurrentRouteKey = getCurrentRouteKey; function getCurrentRouteInfo(state) { return (0, routeManager_1.getRoute)({ routeKey: getCurrentRouteKey(state), }); } exports.getCurrentRouteInfo = getCurrentRouteInfo; function getCurrentRoutePayload(state) { const payload = getReducerState(state).payload; if (!payload || typeof payload !== 'object') { return payload; } return Object.keys(payload) .reduce((accumulator, current) => { const value = payload[current]; if ((0, isInteger_1.default)(value)) { accumulator[current] = Number(value); } else { accumulator[current] = typeof value === 'string' ? decodeURIComponent(value) : value; } return accumulator; }, {}); } exports.getCurrentRoutePayload = getCurrentRoutePayload; function getPropertyFromCurrentRoutePayload(state, property) { return getCurrentRoutePayload(state)[property]; } exports.getPropertyFromCurrentRoutePayload = getPropertyFromCurrentRoutePayload; function getCurrentRouteQueryParams(state) { return (getReducerState(state).query || NO_RERENDER.EMPTY_OBJECT); } exports.getCurrentRouteQueryParams = getCurrentRouteQueryParams; function getTopParentRouteKey(state) { const currentRoutePathParts = getCurrentRouteInfo(state).path.split('/'); return (0, routeManager_1.getRouteKeyByPath)({ path: `/${currentRoutePathParts[1]}`, }); } exports.getTopParentRouteKey = getTopParentRouteKey; function isChildRouteOf(routeKey, possibleParentRouteKey) { const route = (0, routeManager_1.getRoute)({ routeKey }); const possibleParentRoute = (0, routeManager_1.getRoute)({ routeKey: possibleParentRouteKey }); return route.path.indexOf(possibleParentRoute.path) === 0; } exports.isChildRouteOf = isChildRouteOf; function getVirtualPageForRoute(state, route) { return (0, parseVirtualPage_1.default)(state, route.virtualPage); } exports.getVirtualPageForRoute = getVirtualPageForRoute;