Skip to content

Commit

Permalink
Update React Router
Browse files Browse the repository at this point in the history
  • Loading branch information
kpuputti committed Apr 3, 2017
1 parent ae56344 commit 5d32f57
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"react-helmet": "^5.0.2",
"react-intl": "^2.2.3",
"react-redux": "^5.0.3",
"react-router-dom": "4.0.0-beta.6",
"react-router-dom": "^4.0.0",
"react-sortable-hoc": "^0.6.1",
"redux": "^3.6.0",
"redux-form": "^6.6.1",
Expand Down
6 changes: 6 additions & 0 deletions src/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@ Routes.defaultProps = { staticContext: {} };

Routes.propTypes = {
isAuthenticated: bool.isRequired,

// from withFlattenedRoutes
flattenedRoutes: arrayOf(propTypes.route).isRequired,

// from withRouter
staticContext: object,

// from connect
dispatch: func.isRequired,
};

Expand Down
5 changes: 2 additions & 3 deletions src/components/NamedLink/NamedLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ NamedLinkComponent.propTypes = {
// Link component props
to: shape({ search: string, hash: string, state: object }),
children: any,
// Note: The Link `replace` prop conflicts with the replace function
// from withRouter. If the replace boolean is required for the Link
// component, add it here with another name.

// generic props for the underlying <a> element
className: string,
style: object,
activeClassName: string,

// from withFlattenedRoutes
flattenedRoutes: arrayOf(propTypes.route).isRequired,

// from withRouter
match: object,
};
Expand Down
11 changes: 7 additions & 4 deletions src/components/PageLayout/PageLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const scrollToTop = () => {

class PageLayout extends Component {
componentDidMount() {
this.historyUnlisten = this.props.listen(() => scrollToTop());
this.historyUnlisten = this.props.history.listen(() => scrollToTop());
}

componentWillUnmount() {
Expand Down Expand Up @@ -62,7 +62,7 @@ class PageLayout extends Component {
}
}

const { any, string, instanceOf, func } = PropTypes;
const { any, string, instanceOf, func, shape } = PropTypes;

PageLayout.defaultProps = { className: '', children: null, authInfoError: null, logoutError: null };

Expand All @@ -72,8 +72,11 @@ PageLayout.propTypes = {
children: any,
authInfoError: instanceOf(Error),
logoutError: instanceOf(Error),
// history.listen function from withRouter
listen: func.isRequired,

// from withRouter
history: shape({
listen: func.isRequired,
}).isRequired,
};

const mapStateToProps = state => ({
Expand Down
12 changes: 7 additions & 5 deletions src/containers/LandingPage/LandingPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import * as propTypes from '../../util/propTypes';
import css from './LandingPage.css';

export const LandingPageComponent = props => {
const { push: historyPush, flattenedRoutes } = props;
const { history, flattenedRoutes } = props;

const handleSubmit = values => {
const selectedPlace = values && values.location ? values.location.selectedPlace : null;
const { address, origin, bounds } = selectedPlace || {};
const searchParams = { address, origin, bounds };
historyPush(createResourceLocatorString('SearchPage', flattenedRoutes, {}, searchParams));
history.push(createResourceLocatorString('SearchPage', flattenedRoutes, {}, searchParams));
};

return (
Expand All @@ -26,13 +26,15 @@ export const LandingPageComponent = props => {
);
};

const { func, arrayOf } = PropTypes;
const { func, arrayOf, shape } = PropTypes;

LandingPageComponent.propTypes = {
flattenedRoutes: arrayOf(propTypes.route).isRequired,

// history.push from withRouter
push: func.isRequired,
// from withRouter
history: shape({
push: func.isRequired,
}).isRequired,
};

export default withRouter(LandingPageComponent);
6 changes: 5 additions & 1 deletion src/containers/LandingPage/LandingPage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const noop = () => null;
describe('LandingPage', () => {
it('matches snapshot', () => {
const tree = renderShallow(
<LandingPageComponent onLocationChanged={v => v} push={noop} flattenedRoutes={[]} />
<LandingPageComponent
onLocationChanged={v => v}
history={{ push: noop }}
flattenedRoutes={[]}
/>
);
expect(tree).toMatchSnapshot();
});
Expand Down
14 changes: 9 additions & 5 deletions src/containers/SearchPage/SearchPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const SearchPageComponent = props => {
listings,
location,
pagination,
push: historyPush,
history,
searchInProgress,
searchListingsError,
searchParams,
Expand Down Expand Up @@ -88,15 +88,15 @@ export const SearchPageComponent = props => {
const onNextPage = hasNext
? () => {
const urlParams = { address, origin, bounds, page: page + 1 };
historyPush(createResourceLocatorString('SearchPage', flattenedRoutes, {}, urlParams));
history.push(createResourceLocatorString('SearchPage', flattenedRoutes, {}, urlParams));
}
: null;

const hasPrev = listingsAreLoaded && page > 1;
const onPreviousPage = hasPrev
? () => {
const urlParams = { address, origin, bounds, page: page - 1 };
historyPush(createResourceLocatorString('SearchPage', flattenedRoutes, {}, urlParams));
history.push(createResourceLocatorString('SearchPage', flattenedRoutes, {}, urlParams));
}
: null;

Expand Down Expand Up @@ -152,8 +152,12 @@ SearchPageComponent.propTypes = {
totalItems: number,
totalPages: number.isRequired,
}),
// history.push from withRouter
push: func.isRequired,

// from withRouter
history: shape({
push: func.isRequired,
}).isRequired,

searchParams: object,
searchInProgress: bool.isRequired,
searchListingsError: instanceOf(Error),
Expand Down
4 changes: 3 additions & 1 deletion src/containers/SearchPage/SearchPage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('SearchPageComponent', () => {
const props = {
flattenedRoutes: [],
location: { search: '' },
push: () => console.log('HistoryPush called'),
history: {
push: () => console.log('HistoryPush called'),
},
tab: 'listings',
searchInProgress: false,
};
Expand Down
13 changes: 8 additions & 5 deletions src/containers/Topbar/Topbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ const House = () => <span dangerouslySetInnerHTML={{ __html: '&#127968;' }} />;
/* eslint-enable react/no-danger */

const Topbar = props => {
const { isAuthenticated, onLogout, push: historyPush } = props;
const { isAuthenticated, onLogout, history } = props;

const handleLogout = () => {
// History push function is passed to the action to enable
// redirect to home when logout succeeds.
onLogout(historyPush);
onLogout(history.push);
};

return (
Expand All @@ -37,13 +37,16 @@ const Topbar = props => {

Topbar.defaultProps = { user: null };

const { bool, func } = PropTypes;
const { bool, func, shape } = PropTypes;

Topbar.propTypes = {
isAuthenticated: bool.isRequired,
onLogout: func.isRequired,
// history.push prop from withRouter
push: func.isRequired,

// from withRouter
history: shape({
push: func.isRequired,
}).isRequired,
};

const mapStateToProps = state => ({ isAuthenticated: state.Auth.isAuthenticated });
Expand Down
4 changes: 2 additions & 2 deletions src/util/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export const matchPathname = (pathname, routesConfiguration) => {

return flattenedRoutes.reduce(
(matches, route) => {
const { exact = false } = route;
const match = !route.path || matchPath(pathname, route.path, { exact });
const { path, exact = false } = route;
const match = matchPath(pathname, { path, exact });
if (match) {
matches.push({
route,
Expand Down
27 changes: 19 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2888,6 +2888,16 @@ history@^4.5.1:
value-equal "^0.2.0"
warning "^3.0.0"

history@^4.6.0:
version "4.6.1"
resolved "https://registry.yarnpkg.com/history/-/history-4.6.1.tgz#911cf8eb65728555a94f2b12780a0c531a14d2fd"
dependencies:
invariant "^2.2.1"
loose-envify "^1.2.0"
resolve-pathname "^2.0.0"
value-equal "^0.2.0"
warning "^3.0.0"

hmac-drbg@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.0.tgz#3db471f45aae4a994a0688322171f51b8b91bee5"
Expand Down Expand Up @@ -5400,21 +5410,22 @@ react-redux@^5.0.3:
lodash-es "^4.2.0"
loose-envify "^1.1.0"

react-router-dom@4.0.0-beta.6:
version "4.0.0-beta.6"
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-4.0.0-beta.6.tgz#bdbd8f2fea3def52970735778db03b24cc082a02"
react-router-dom@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-4.0.0.tgz#4fa4418e14b8cfc5bcc0bdea0c4083fb8c2aef10"
dependencies:
history "^4.5.1"
react-router "^4.0.0-beta.6"
react-router "^4.0.0"

react-router@^4.0.0-beta.6:
version "4.0.0-beta.6"
resolved "https://registry.yarnpkg.com/react-router/-/react-router-4.0.0-beta.6.tgz#561ac0bf1929960813bf201319ff85d821d5547b"
react-router@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/react-router/-/react-router-4.0.0.tgz#6532075231f0bb5077c2005c1d417ad6165b3997"
dependencies:
history "^4.5.1"
history "^4.6.0"
invariant "^2.2.2"
loose-envify "^1.3.1"
path-to-regexp "^1.5.3"
warning "^3.0.0"

react-side-effect@^1.1.0:
version "1.1.0"
Expand Down

0 comments on commit 5d32f57

Please sign in to comment.