From c2a92b38278b3ba37b3741914fd68e2cd46b447f Mon Sep 17 00:00:00 2001 From: Christopher Hastings Date: Mon, 7 Aug 2023 14:06:35 +0100 Subject: [PATCH 1/2] refactor(routerUtils): combinedfiles findRoute findRoutes and processRoutePath into routerUtils Signed-off-by: Christopher Hastings --- src/router/utils/findRoute.js | 46 ---------------------- src/router/utils/findRoutes.js | 11 ------ src/router/utils/processRoutePath.js | 10 ----- src/utils/routerUtils.js | 58 ++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 67 deletions(-) delete mode 100644 src/router/utils/findRoute.js delete mode 100644 src/router/utils/findRoutes.js delete mode 100644 src/router/utils/processRoutePath.js create mode 100644 src/utils/routerUtils.js diff --git a/src/router/utils/findRoute.js b/src/router/utils/findRoute.js deleted file mode 100644 index 535357b5..00000000 --- a/src/router/utils/findRoute.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - - given a route structure with children e.g. - - [ - { - name: 'home', - path: '/' - }, - { - name: 'login', - path: '/login', - apples: 10, - children: [ - { - name: 'test', - path: '/test', - oranges: 11, - }, - ], - }, - ] - - this will find a route based on the given name e.g. - - login.test will return: - - { - name: 'test', - path: '/test', - oranges: 11, - } - -*/ - -const findRoute = (routes, name) => { - if (!name) return null - return name.split('.').reduce((currentRoute, part) => { - if (!currentRoute || !currentRoute.children) return null - return currentRoute.children.find((route) => route.name === part) - }, { - children: routes, - }) -} - -export default findRoute diff --git a/src/router/utils/findRoutes.js b/src/router/utils/findRoutes.js deleted file mode 100644 index 4a5422b1..00000000 --- a/src/router/utils/findRoutes.js +++ /dev/null @@ -1,11 +0,0 @@ -/* - - calls find route over an array of route names - -*/ - -import findRoute from './findRoute' - -const findRoutes = (routes, names) => names.map((name) => findRoute(routes, name)) - -export default findRoutes diff --git a/src/router/utils/processRoutePath.js b/src/router/utils/processRoutePath.js deleted file mode 100644 index 40f1fd9e..00000000 --- a/src/router/utils/processRoutePath.js +++ /dev/null @@ -1,10 +0,0 @@ -// if we are running under a sub-path then prepend that path -// to all the frontend routes -const processRoutePath = (path) => { - // if we are not running under a sub-path then just return the route - if (!window.SEXTANT_ROOT_PATH || window.SEXTANT_ROOT_PATH === '/') return path - // prepend our sub-path to the route - return window.SEXTANT_ROOT_PATH + path -} - -export default processRoutePath diff --git a/src/utils/routerUtils.js b/src/utils/routerUtils.js new file mode 100644 index 00000000..a02d861f --- /dev/null +++ b/src/utils/routerUtils.js @@ -0,0 +1,58 @@ +/* + + given a route structure with children e.g. + + [ + { + name: 'home', + path: '/' + }, + { + name: 'login', + path: '/login', + apples: 10, + children: [ + { + name: 'test', + path: '/test', + oranges: 11, + }, + ], + }, + ] + + this will find a route based on the given name e.g. + + login.test will return: + + { + name: 'test', + path: '/test', + oranges: 11, + } + +*/ + +export const findRoute = (routes, name) => { + if (!name) return null + return name.split('.').reduce( + (currentRoute, part) => { + if (!currentRoute || !currentRoute.children) return null + return currentRoute.children.find((route) => route.name === part) + }, + { + children: routes, + }, + ) +} + +export const findRoutes = (routes, names) => names.map((name) => findRoute(routes, name)) + +// if we are running under a sub-path then prepend that path +// to all the frontend routes +export const processRoutePath = (path) => { + // if we are not running under a sub-path then just return the route + if (!window.SEXTANT_ROOT_PATH || window.SEXTANT_ROOT_PATH === '/') return path + // prepend our sub-path to the route + return window.SEXTANT_ROOT_PATH + path +} From a75701909a9af2ec5fdbeb2e360d12dc3d721e37 Mon Sep 17 00:00:00 2001 From: Christopher Hastings Date: Mon, 7 Aug 2023 14:17:34 +0100 Subject: [PATCH 2/2] refactor(update-imports): update the imports to point to the refactored routerUtils Signed-off-by: Christopher Hastings --- src/router/index.js | 3 +-- src/router/middleware/authorize.js | 5 +++-- src/router/middleware/log.js | 2 +- src/router/middleware/redirect.js | 12 ++++++------ src/router/middleware/trigger.js | 2 +- src/store/index.js | 26 +++++++++----------------- src/store/rootSagas.js | 2 +- src/store/selectors.js | 2 +- 8 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 1e732f4b..44f3a4e0 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -7,10 +7,9 @@ import logMiddleware from './middleware/log' import authorizeMiddleware from './middleware/authorize' import redirectMiddleware from './middleware/redirect' import triggerMiddleware from './middleware/trigger' -import processRoutePath from './utils/processRoutePath' +import { processRoutePath } from '../utils/routerUtils' const Router = () => { - // remap the top-level routes to include the sub-path const mappedRoutes = routes.map((route) => ({ ...route, path: processRoutePath(route.path) })) const router = createRouter(mappedRoutes, { diff --git a/src/router/middleware/authorize.js b/src/router/middleware/authorize.js index 50880c17..4dd36d41 100644 --- a/src/router/middleware/authorize.js +++ b/src/router/middleware/authorize.js @@ -3,8 +3,7 @@ import transitionPath from 'router5-transition-path' import snackbarActions from 'store/modules/snackbar' import routerActions from 'store/modules/router' -import findRoutes from '../utils/findRoutes' - +import { findRoutes } from '../../utils/routerUtils' /* run an authorize function on the route if present @@ -46,3 +45,5 @@ const authorizeRoute = (routes) => (router, dependencies) => (toState, fromState } export default authorizeRoute +// test past a mock router store contains something to dispatch too did i get done or not +// diff --git a/src/router/middleware/log.js b/src/router/middleware/log.js index 78d7953d..feba6c35 100644 --- a/src/router/middleware/log.js +++ b/src/router/middleware/log.js @@ -1,6 +1,6 @@ import transitionPath from 'router5-transition-path' import settings from 'settings' -import findRoutes from '../utils/findRoutes' +import { findRoutes } from '../../utils/routerUtils' // log each route transition in development const logRoute = (routes) => () => (toState, fromState, done) => { diff --git a/src/router/middleware/redirect.js b/src/router/middleware/redirect.js index 1779bfc4..9ca773f8 100644 --- a/src/router/middleware/redirect.js +++ b/src/router/middleware/redirect.js @@ -1,6 +1,6 @@ import transitionPath from 'router5-transition-path' import routerActions from 'store/modules/router' -import findRoutes from '../utils/findRoutes' +import { findRoutes } from '../../utils/routerUtils' /* @@ -23,14 +23,14 @@ const redirectRoute = (routes) => (router, dependencies) => (toState, fromState, let redirectTo = null // if the redirect is a string - redirect there - if (typeof (redirectInfo) === 'string') { + if (typeof redirectInfo === 'string') { redirectTo = redirectInfo - } else if (typeof (redirectInfo) === 'function') { - // if it's a function - run the function passing the redux state - // the function should return the redirect or falsey value for don't redirect + } else if (typeof redirectInfo === 'function') { + // if it's a function - run the function passing the redux state + // the function should return the redirect or falsey value for don't redirect redirectTo = redirectInfo(store.getState()) } else { - return done(`unknown type of redirect info: ${typeof (redirectInfo)}`) + return done(`unknown type of redirect info: ${typeof redirectInfo}`) } if (redirectTo && redirectTo !== activeRoute.name) { diff --git a/src/router/middleware/trigger.js b/src/router/middleware/trigger.js index 3bac0f29..b644a2e6 100644 --- a/src/router/middleware/trigger.js +++ b/src/router/middleware/trigger.js @@ -1,5 +1,5 @@ import transitionPath from 'router5-transition-path' -import findRoutes from '../utils/findRoutes' +import { findRoutes } from '../../utils/routerUtils' const runTriggers = ({ routes, diff --git a/src/store/index.js b/src/store/index.js index 4e71c87d..43c5daf1 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,37 +1,29 @@ +/* eslint-disable import/no-import-module-exports */ /* eslint-disable global-require */ /* eslint-disable no-underscore-dangle */ import { applyMiddleware, createStore, compose } from 'redux' import createSagaMiddleware from 'redux-saga' import thunk from 'redux-thunk' import { router5Middleware } from 'redux-router5' - import SagaManager from './sagaManager' import reducer from './reducer' const Store = (router, initialState = {}) => { const sagaMiddleware = createSagaMiddleware() - const middleware = [ - router5Middleware(router), - thunk, - sagaMiddleware, - ] + const middleware = [router5Middleware(router), thunk, sagaMiddleware] - const storeEnhancers = [ - applyMiddleware(...middleware), - ] + const storeEnhancers = [applyMiddleware(...middleware)] if (window.__REDUX_DEVTOOLS_EXTENSION__) { - storeEnhancers.push(window.__REDUX_DEVTOOLS_EXTENSION__({ - shouldHotReload: false, - })) + storeEnhancers.push( + window.__REDUX_DEVTOOLS_EXTENSION__({ + shouldHotReload: false, + }), + ) } - const store = createStore( - reducer, - initialState, - compose(...storeEnhancers), - ) + const store = createStore(reducer, initialState, compose(...storeEnhancers)) router.setDependency('store', store) SagaManager.startSagas(sagaMiddleware, router) diff --git a/src/store/rootSagas.js b/src/store/rootSagas.js index a2f434b1..437ea6c7 100644 --- a/src/store/rootSagas.js +++ b/src/store/rootSagas.js @@ -12,7 +12,7 @@ import userActions from './modules/user' import configActions from './modules/config' import networkActions from './modules/network' import { sagas as fileUploadSagas } from './modules/fileupload' -import processRoutePath from '../router/utils/processRoutePath' +import { processRoutePath } from '../utils/routerUtils' import selectors from './selectors' const errorFilter = (name) => (action) => { diff --git a/src/store/selectors.js b/src/store/selectors.js index f005dff8..f90b8fed 100644 --- a/src/store/selectors.js +++ b/src/store/selectors.js @@ -2,7 +2,7 @@ /* eslint-disable import/no-cycle */ import { createSelector } from 'reselect' import routes from 'router/routes' -import findRoute from 'router/utils/findRoute' +import { findRoute } from 'utils/routerUtils' // pluck a single prop from a previous selector const prop = (baseSelector, propName) => createSelector(