From 794c3dc02e349a83e3fffa0098cd572f5287942c Mon Sep 17 00:00:00 2001 From: Neal Granger Date: Thu, 22 Dec 2016 16:34:45 -0800 Subject: [PATCH] Remove previousPath related logic. Remove the previousPath reducer and related conditional `router.goBack` logic. This never really worked correctly in practice since using the back function of history leaves the reducer with the wrong `previousPath` value (actually the "next" path). A proper solution would be to store an array of location history, and push or pop the location on navigation. It's better to just keep things simple. --- src/action.js | 6 ------ src/connect.js | 35 +++++++++-------------------------- src/reducer.js | 2 -- src/router.js | 12 +----------- src/selector.js | 3 --- 5 files changed, 10 insertions(+), 48 deletions(-) diff --git a/src/action.js b/src/action.js index 4a47037..c4df23b 100644 --- a/src/action.js +++ b/src/action.js @@ -2,7 +2,6 @@ import cuid from 'cuid'; export const ADD_COMPONENT = '@@relocation/ADD_COMPONENT'; export const REMOVE_COMPONENT = '@@relocation/REMOVE_COMPONENT'; -export const SET_PREVIOUS_PATH = '@@relocation/SET_PREVIOUS_PATH'; export const SET_ROUTE_COMPONENTS = '@@relocation/SET_ROUTE_COMPONENTS'; export const addComponent = ({type, props, id = cuid()}) => ({ @@ -12,11 +11,6 @@ export const addComponent = ({type, props, id = cuid()}) => ({ export const removeComponent = (id) => ({type: REMOVE_COMPONENT, payload: id}); -export const setPreviousPath = (path) => ({ - type: SET_PREVIOUS_PATH, - payload: path, -}); - export const setRouteComponents = (components) => ({ type: SET_ROUTE_COMPONENTS, payload: components, diff --git a/src/connect.js b/src/connect.js index beff7a0..0e37545 100644 --- a/src/connect.js +++ b/src/connect.js @@ -2,7 +2,7 @@ import {Component, createElement, PropTypes} from 'react'; import hoistStatics from 'hoist-non-react-statics'; import {connect} from 'react-redux'; -import {getMergedComponents, getPreviousPath} from './selector'; +import {getMergedComponents} from './selector'; import {removeComponent} from './action'; import {componentsShape, renderMapShape, getDisplayName} from './util'; @@ -22,7 +22,6 @@ export default (rawRenderMap, rawConfig = {}) => (WrappedComponent) => { dispatch: PropTypes.func.isRequired, ___relocation___: PropTypes.shape({ components: componentsShape.isRequired, - previousPath: PropTypes.string.isRequired, config: PropTypes.object.isRequired, renderMap: renderMapShape.isRequired, }).isRequired, @@ -32,29 +31,16 @@ export default (rawRenderMap, rawConfig = {}) => (WrappedComponent) => { router: PropTypes.object, } - navigateToPath(path, {useHistoryBack = true} = {}) { + navigateToPath(path) { // Check for the react-router context. if (!this.context.router) { return; } - const {previousPath} = this.props.___relocation___; - - // The `useHistoryBack` option will trigger the use of the `goBack` method - // instead of `push` in an effort to keep the if the requested path is - // equal to the previous path. - // - // This is useful in scenraios where component that is displayed in - // response to a route change is considered dismissed or completed on - // removal. - if (useHistoryBack && path === previousPath) { - this.context.router.goBack(); - } else { - this.context.router.push(path); - } + this.context.router.push(path); } - removeComponent(id/* , options */) { + removeComponent(id) { return this.props.dispatch(removeComponent(id)); } @@ -80,8 +66,7 @@ export default (rawRenderMap, rawConfig = {}) => (WrappedComponent) => { // The component object does not have a `remove` property, or it has // a truthy value that is not a function. Either case indicates that // it should use the default remove handler. - removeHandler = (options) => - this.removeComponent(component.id, options); + removeHandler = () => this.removeComponent(component.id); } let pathRemoveHandler = null; @@ -89,17 +74,16 @@ export default (rawRenderMap, rawConfig = {}) => (WrappedComponent) => { if (typeof component.removePath === 'string') { // Create a function that will change the history state when removing // the component. - pathRemoveHandler = (options) => - this.navigateToPath(component.removePath, options); + pathRemoveHandler = () => this.navigateToPath(component.removePath); } if (pathRemoveHandler && removeHandler) { // A remove handler function and a return { ...component, - remove: (options) => { - pathRemoveHandler(options); - return removeHandler(options); + remove: () => { + pathRemoveHandler(); + return removeHandler(); }, }; } @@ -158,7 +142,6 @@ export default (rawRenderMap, rawConfig = {}) => (WrappedComponent) => { // conflict with existing props. ___relocation___: { components: getMergedComponents(state, selectorProps), - previousPath: getPreviousPath(state, selectorProps), config, renderMap, }, diff --git a/src/reducer.js b/src/reducer.js index 6388816..8f5ca5d 100644 --- a/src/reducer.js +++ b/src/reducer.js @@ -2,7 +2,6 @@ import {combineReducers} from 'redux'; import { REMOVE_COMPONENT, ADD_COMPONENT, - SET_PREVIOUS_PATH, SET_ROUTE_COMPONENTS, } from './action'; @@ -16,5 +15,4 @@ export default combineReducers({ state.filter((item) => item.id !== payload), }[action.type] || (() => state))(state, action), routeComponents: createReducer(SET_ROUTE_COMPONENTS, []), - previousPath: createReducer(SET_PREVIOUS_PATH, ''), }); diff --git a/src/router.js b/src/router.js index 2d61286..405421c 100644 --- a/src/router.js +++ b/src/router.js @@ -2,7 +2,7 @@ import {Component, PropTypes, Children} from 'react'; import {connect} from 'react-redux'; import invariant from 'fbjs/lib/invariant'; -import {setRouteComponents, setPreviousPath} from './action'; +import {setRouteComponents} from './action'; /** * [description] @@ -36,7 +36,6 @@ export default (formatPattern) => { } componentWillReceiveProps(nextProps) { - this.updatePreviousPath(nextProps); this.updateRouteComponents(nextProps); } @@ -103,15 +102,6 @@ export default (formatPattern) => { dispatch(setRouteComponents(components)); } - updatePreviousPath(nextProps) { - const {location: {pathname: previousPath}, dispatch} = this.props; - const {location: {pathname: currentPath}} = nextProps; - - if (previousPath && previousPath !== currentPath) { - dispatch(setPreviousPath(previousPath)); - } - } - render() { const {children} = this.props; diff --git a/src/selector.js b/src/selector.js index 4df09e2..e92094d 100644 --- a/src/selector.js +++ b/src/selector.js @@ -11,9 +11,6 @@ export const getComponents = (state, props) => export const getRouteComponents = (state, props) => getRelocation(state, props).routeComponents; -export const getPreviousPath = (state, props) => - getRelocation(state, props).previousPath; - export const getMergedComponents = (state, props) => [...getRouteComponents(state, props), ...getComponents(state, props)]. reduce((components, component) => {