Skip to content

Commit

Permalink
feat(Fragment): add parsed params in redirect + switch push - replace
Browse files Browse the repository at this point in the history
  • Loading branch information
woodcutter committed Oct 17, 2018
1 parent a99a677 commit 391fd33
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5,158 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ dist
umd

.idea
.DS_STORE
package-lock.json
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-unity-router",
"version": "1.4.1",
"version": "1.5.0",
"description": "Redux router that syncs history with store",
"main": "dist/index.js",
"scripts": {
Expand Down
29 changes: 14 additions & 15 deletions src/components/Fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import BaseRouterComponent from './Base';
import { ID_DELIM, __DEV__} from '../constants';
import { push as actionPush } from '../actions';
import { replace } from '../actions';

class Fragment extends BaseRouterComponent {

Expand Down Expand Up @@ -41,24 +41,28 @@ class Fragment extends BaseRouterComponent {

if (!this.isSubscribed) return;

const routerStore = this.getStatefromStore();
const { immutable, parseRoute } = this.router;
const { redirect, push } = newProps || this.props;
const { redirect } = newProps || this.props;

const current = this.current;
const storeState = this.getStatefromStore();
const routerStore = immutable ? storeState.toJS() : storeState;

if (routerStore) {
const idPath = immutable ? routerStore.getIn([ 'route', 'idPath' ]) : routerStore.route.idPath;
const idPath = routerStore.route.idPath;
const params = routerStore.route.params;
const query = routerStore.query;
const hash = routerStore.hash;
const routePath = idPath + ID_DELIM;
const fragmentPath = current + ID_DELIM;
const match = (routePath).indexOf(fragmentPath);
const matchExact = routePath === fragmentPath;

if (matchExact && redirect) {
return this.store.dispatch(push(
typeof redirect === 'object' && redirect.id
? parseRoute(redirect)
: redirect
));
const redirectRoute = typeof redirect === 'object' && redirect.id ?
parseRoute({ ...{ params, query, hash }, ...redirect }) : redirect;

return this.store.dispatch(replace(redirectRoute));
}

if (match === 0 && !this.state.visible) {
Expand Down Expand Up @@ -101,10 +105,6 @@ Fragment.childContextTypes = {
store: PropTypes.object
};

Fragment.defaultProps = {
push: actionPush
};

if (__DEV__) {
Fragment.propTypes = {
id: PropTypes.oneOfType([
Expand All @@ -120,8 +120,7 @@ if (__DEV__) {
redirect: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string
]),
push: PropTypes.func.isRequired
])
};
}

Expand Down
Loading

0 comments on commit 391fd33

Please sign in to comment.