Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Ability to configure route as redirect #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions __tests__/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,33 @@ describe('Router without interceptors', () => {
expect(wrapper.prop('history').location.search).toEqual('?the=query')
expect(Router.getCurrentQuery()).toEqual(query)
})

test('Redirect from some path to Details page', () => {
const historyLength = wrapper.prop('history').index

Router.push(null, {path: '/path_to_redirect_from'})

expect(Router.getCurrentRoute()).toEqual(Router.routes.details.path)
expect(wrapper.prop('history').index).toEqual(historyLength + 1)
})

test('Default redirect to About page', () => {
const historyLength = wrapper.prop('history').index

Router.push(null, {path: 'some_unrealistic_path'})

expect(Router.getCurrentRoute()).toEqual(Router.routes.about.path)
expect(wrapper.prop('history').index).toEqual(historyLength + 1)
})

test('Redirect to any external url', () => {
const externalUrl = 'http://example.com'

window.location.replace = jest.fn()
Router.redirect(externalUrl)
expect(window.location.replace).toHaveBeenCalledWith(externalUrl)
window.location.replace.mockRestore()
})
})


Expand Down
7 changes: 7 additions & 0 deletions __tests__/setup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ export default {
component: DummyComponent('WhiteListParams'),
whiteListParams: ['a', 'c'],
},
redirectFromRule: {
redirectFrom: '/path_to_redirect_from',
redirectTo: '/details'
},
redirectToAbout: {
redirectTo: '/about',
},
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const navigationMethodFields = [
'pop',
'popToTop',
'replace',
'redirect',
'showModal',
'dismissModal',
]
Expand Down
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tipsi-router",
"version": "1.7.0",
"version": "1.8.0",
"description": "React and RN router solution",
"main": "index.js",
"scripts": {
Expand Down
26 changes: 24 additions & 2 deletions src/Router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { Router, Switch, Route } from 'react-router-dom'
import { Router, Switch, Route, Redirect } from 'react-router-dom'
import { ModalRoute } from 'react-router-modal'
import createBrowserHistory from 'history/createBrowserHistory'
import createMemoryHistory from 'history/createMemoryHistory'
Expand Down Expand Up @@ -34,7 +34,10 @@ export default class TipsiRouter extends RouterBase {
const initialEntries = Object.values(routes).map(route => route.path)
const initialIndex = initialEntries.indexOf(initialRoute)

return createMemoryHistory({ initialEntries, initialIndex })
// filter out all Redirects (items with 'path' prop equal to undefined)
const entriesWithoutRedirects = initialEntries.filter(entry => !!entry)

return createMemoryHistory({ initialEntries: entriesWithoutRedirects, initialIndex })
}

filterSyncedState(state, filterFields) {
Expand All @@ -53,6 +56,21 @@ export default class TipsiRouter extends RouterBase {
createRouter(initialRoute, routes) {
const shouldScrollToTop = this.defaultRouteConfig.shouldScrollToTop || true
const elements = Object.entries(routes).reduce((memo, [key, route]) => {
// "to" is only required property for Redirect
if (route.redirectTo) {
const { redirectFrom, redirectTo, exact } = route

// only allow "to", "from", or "exact" props to be passed to <Redirect />
const redirectParams = {
to: redirectTo,
}

if (redirectFrom) redirectParams.from = redirectFrom
if (exact) redirectParams.exact = exact

return memo.concat(<Redirect key={key} {...redirectParams} />)
}

const RouteContainer = route.modal ? ModalRoute : Route
const RouteComponent = route.component

Expand Down Expand Up @@ -217,6 +235,10 @@ export default class TipsiRouter extends RouterBase {
this.callHistoryMethodWithArguments('push', e, route, paramsOrOptions)
}

redirect(url) {
window.location.replace(url)
}

async dismissModal(e) {
if (e) {
e.preventDefault()
Expand Down
3 changes: 3 additions & 0 deletions src/Router.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,8 @@ export default class TipsiRouter extends RouterBase {
)
}

/* eslint-disable-next-line */
redirect() {}

routeName = route => findKey(this.routes, { path: route.path })
}
5 changes: 5 additions & 0 deletions src/Router.wix.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,10 @@ export default class TipsiRouter extends RouterBase {
Navigation.dismissModal()
}

/* eslint-disable-next-line */
redirect(url) {
window.location.replace(url)
}

routeName = route => findKey(this.routes, { path: route.path })
}