From 3fb1e53a03a069d92a024fb821363e7a6c252ef2 Mon Sep 17 00:00:00 2001 From: Vesa Luusua Date: Wed, 11 Oct 2017 16:00:15 +0300 Subject: [PATCH] routeConfiguration uses exact routes by default --- src/Routes.js | 6 +++++- src/routeConfiguration.js | 43 +++------------------------------------ src/util/routes.js | 15 +++++++++++--- 3 files changed, 20 insertions(+), 44 deletions(-) diff --git a/src/Routes.js b/src/Routes.js index a399b171..88f3c4a7 100644 --- a/src/Routes.js +++ b/src/Routes.js @@ -91,11 +91,15 @@ const Routes = props => { staticContext, dispatch, }; + + // By default, our routes are exact. + // https://reacttraining.com/react-router/web/api/Route/exact-bool + const isExact = route.exact != null ? route.exact : true; return ( ( ; +// Our routes are exact by default. +// See behaviour from Routes.js where Route is created. const routeConfiguration = () => { return [ - { path: '/', exact: true, name: 'LandingPage', component: props => }, + { path: '/', name: 'LandingPage', component: props => }, { path: '/s', - exact: true, name: 'SearchPage', component: props => , loadData: SearchPage.loadData, }, { path: '/s/filters', - exact: true, name: 'SearchFiltersPage', component: props => , loadData: SearchPage.loadData, }, { path: '/s/listings', - exact: true, name: 'SearchListingsPage', component: props => , loadData: SearchPage.loadData, }, { path: '/s/map', - exact: true, name: 'SearchMapPage', component: props => , loadData: SearchPage.loadData, }, { path: '/l', - exact: true, name: 'ListingBasePage', component: RedirectToLandingPage, }, { path: '/l/:slug/:id', - exact: true, name: 'ListingPage', component: props => , loadData: ListingPage.loadData, }, { path: '/l/:slug/:id/book', - exact: true, name: 'ListingPage', component: props => , loadData: ListingPage.loadData, @@ -90,7 +85,6 @@ const routeConfiguration = () => { { path: '/l/:slug/:id/checkout', auth: true, - exact: true, name: 'CheckoutPage', component: props => , setInitialValues: CheckoutPage.setInitialValues, @@ -98,7 +92,6 @@ const routeConfiguration = () => { { auth: true, path: '/l/new', - exact: true, name: 'NewListingPage', component: () => ( { { auth: true, path: '/l/:slug/:id/:type/:tab', - exact: true, name: 'EditListingPage', component: props => , loadData: EditListingPage.loadData, @@ -120,59 +112,50 @@ const routeConfiguration = () => { // conflict and `new` is not a valid listing UUID. { path: '/l/:id', - exact: true, name: 'ListingPageCanonical', component: props => , loadData: ListingPage.loadData, }, { path: '/u', - exact: true, name: 'ProfileBasePage', component: RedirectToLandingPage, }, { path: '/u/:displayName', - exact: true, name: 'ProfilePage', component: props => , }, { path: '/profile-settings', auth: true, - exact: true, name: 'ProfileSettingsPage', component: props => , }, { path: '/login', - exact: true, name: 'LoginPage', component: props => , }, { path: '/signup', - exact: true, name: 'SignupPage', component: props => , }, { path: '/recover-password', - exact: true, name: 'PasswordRecoveryPage', component: props => , }, { path: '/inbox', auth: true, - exact: true, name: 'InboxBasePage', component: () => , }, { path: '/inbox/:tab', auth: true, - exact: true, name: 'InboxPage', component: props => , loadData: InboxPage.loadData, @@ -180,14 +163,12 @@ const routeConfiguration = () => { { path: '/order/:id', auth: true, - exact: true, name: 'OrderPage', component: RedirectToLandingPage, }, { path: '/order/:id/details', auth: true, - exact: true, name: 'OrderDetailsPage', component: props => , loadData: OrderPage.loadData, @@ -195,7 +176,6 @@ const routeConfiguration = () => { { path: '/order/:id/discussion', auth: true, - exact: true, name: 'OrderDiscussionPage', component: props => , loadData: OrderPage.loadData, @@ -203,14 +183,12 @@ const routeConfiguration = () => { { path: '/sale/:id', auth: true, - exact: true, name: 'SalePage', component: props => , }, { path: '/sale/:id/details', auth: true, - exact: true, name: 'SaleDetailsPage', component: props => , loadData: SalePage.loadData, @@ -218,7 +196,6 @@ const routeConfiguration = () => { { path: '/sale/:id/discussion', auth: true, - exact: true, name: 'SaleDiscussionPage', component: props => , loadData: SalePage.loadData, @@ -226,7 +203,6 @@ const routeConfiguration = () => { { path: '/listings', auth: true, - exact: true, name: 'ManageListingsPage', component: props => , loadData: ManageListingsPage.loadData, @@ -234,14 +210,12 @@ const routeConfiguration = () => { { path: '/account', auth: true, - exact: true, name: 'AccountSettingsPage', component: () => , }, { path: '/account/contact-details', auth: true, - exact: true, name: 'ContactDetailsPage', component: props => , loadData: ContactDetailsPage.loadData, @@ -249,57 +223,48 @@ const routeConfiguration = () => { { path: '/account/change-password', auth: true, - exact: true, name: 'PasswordChangePage', component: props => , }, { path: '/account/payout-preferences', auth: true, - exact: true, name: 'PayoutPreferencesPage', component: props => , }, { path: '/account/security', auth: true, - exact: true, name: 'SecurityPage', component: props => , }, { path: '/styleguide', - exact: true, name: 'Styleguide', component: props => , }, { path: '/styleguide/g/:group', - exact: true, name: 'StyleguideGroup', component: props => , }, { path: '/styleguide/c/:component', - exact: true, name: 'StyleguideComponent', component: props => , }, { path: '/styleguide/c/:component/:example', - exact: true, name: 'StyleguideComponentExample', component: props => , }, { path: '/styleguide/c/:component/:example/raw', - exact: true, name: 'StyleguideComponentExampleRaw', component: props => , }, { path: '/notfound', - exact: true, name: 'NotFoundPage', component: props => , }, @@ -309,7 +274,6 @@ const routeConfiguration = () => { // The API expects that the Starter App implements /reset-password endpoint { path: '/reset-password', - exact: true, name: 'PasswordResetPage', component: props => , }, @@ -321,7 +285,6 @@ const routeConfiguration = () => { path: '/verify-email', auth: true, authPage: 'LoginPage', - exact: true, name: 'EmailVerificationPage', component: props => , }, diff --git a/src/util/routes.js b/src/util/routes.js index 373ef07b..d9b52ca3 100644 --- a/src/util/routes.js +++ b/src/util/routes.js @@ -29,12 +29,13 @@ export const pathByRouteName = (nameToFind, routes, params = {}) => * @param {String} pathname - Full URL path from root with possible * search params and hash included * - * @return {Array<{ route, params }>} - All matches as { route, params } objects + * @return {Array<{ route, params }>} - All matches as { route, params } objects if matches has + * exact flag set to false. If not, an array containing just the first matched exact route is returned. */ export const matchPathname = (pathname, routeConfiguration) => { - return routeConfiguration.reduce( + const matchedRoutes = routeConfiguration.reduce( (matches, route) => { - const { path, exact = false } = route; + const { path, exact = true } = route; const match = matchPath(pathname, { path, exact }); if (match) { matches.push({ @@ -46,6 +47,14 @@ export const matchPathname = (pathname, routeConfiguration) => { }, [] ); + + const matchedExactRoute = matchedRoutes.find(r => { + return r.exact === true || r.exact == null; + }); + + // We return matched 'exact' path route only if such exists + // and all matches if no exact flag exists. + return matchedExactRoute ? [matchedExactRoute] : matchedRoutes; }; /**