Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/utilsrefactor #311

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
5 changes: 3 additions & 2 deletions src/router/middleware/authorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
//
2 changes: 1 addition & 1 deletion src/router/middleware/log.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
12 changes: 6 additions & 6 deletions src/router/middleware/redirect.js
Original file line number Diff line number Diff line change
@@ -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'

/*

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/router/middleware/trigger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import transitionPath from 'router5-transition-path'
import findRoutes from '../utils/findRoutes'
import { findRoutes } from '../../utils/routerUtils'

const runTriggers = ({
routes,
Expand Down
46 changes: 0 additions & 46 deletions src/router/utils/findRoute.js

This file was deleted.

11 changes: 0 additions & 11 deletions src/router/utils/findRoutes.js

This file was deleted.

10 changes: 0 additions & 10 deletions src/router/utils/processRoutePath.js

This file was deleted.

26 changes: 9 additions & 17 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/store/rootSagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
58 changes: 58 additions & 0 deletions src/utils/routerUtils.js
Original file line number Diff line number Diff line change
@@ -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
}