Skip to content
Merged
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
6 changes: 0 additions & 6 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()}) => ({
Expand All @@ -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,
Expand Down
35 changes: 9 additions & 26 deletions src/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
Expand All @@ -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));
}

Expand All @@ -80,26 +66,24 @@ 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;

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();
},
};
}
Expand Down Expand Up @@ -158,7 +142,6 @@ export default (rawRenderMap, rawConfig = {}) => (WrappedComponent) => {
// conflict with existing props.
___relocation___: {
components: getMergedComponents(state, selectorProps),
previousPath: getPreviousPath(state, selectorProps),
config,
renderMap,
},
Expand Down
2 changes: 0 additions & 2 deletions src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {combineReducers} from 'redux';
import {
REMOVE_COMPONENT,
ADD_COMPONENT,
SET_PREVIOUS_PATH,
SET_ROUTE_COMPONENTS,
} from './action';

Expand All @@ -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, ''),
});
12 changes: 1 addition & 11 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -36,7 +36,6 @@ export default (formatPattern) => {
}

componentWillReceiveProps(nextProps) {
this.updatePreviousPath(nextProps);
this.updateRouteComponents(nextProps);
}

Expand Down Expand Up @@ -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;

Expand Down
3 changes: 0 additions & 3 deletions src/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down