remove conditional redirect component

This commit is contained in:
Jake McDermott
2019-01-02 01:15:31 -05:00
parent 18505b35b8
commit a2601d5f67
3 changed files with 76 additions and 137 deletions

View File

@@ -1,35 +0,0 @@
import React from 'react';
import {
Route,
Redirect
} from 'react-router-dom';
import { shallow } from 'enzyme';
import ConditionalRedirect from '../../src/components/ConditionalRedirect';
describe('<ConditionalRedirect />', () => {
test('renders Redirect when shouldRedirect is passed truthy func', () => {
const truthyFunc = () => true;
const shouldHaveRedirectChild = shallow(
<ConditionalRedirect
shouldRedirect={() => truthyFunc()}
/>
);
const redirectChild = shouldHaveRedirectChild.find(Redirect);
expect(redirectChild.length).toBe(1);
const routeChild = shouldHaveRedirectChild.find(Route);
expect(routeChild.length).toBe(0);
});
test('renders Route when shouldRedirect is passed falsy func', () => {
const falsyFunc = () => false;
const shouldHaveRouteChild = shallow(
<ConditionalRedirect
shouldRedirect={() => falsyFunc()}
/>
);
const routeChild = shouldHaveRouteChild.find(Route);
expect(routeChild.length).toBe(1);
const redirectChild = shouldHaveRouteChild.find(Redirect);
expect(redirectChild.length).toBe(0);
});
});

View File

@@ -5,7 +5,8 @@ import {
HashRouter as Router, HashRouter as Router,
Redirect, Redirect,
Switch, Switch,
withRouter withRouter,
Route,
} from 'react-router-dom'; } from 'react-router-dom';
import { import {
BackgroundImage, BackgroundImage,
@@ -28,12 +29,10 @@ import { API_LOGOUT, API_CONFIG } from './endpoints';
import ja from '../build/locales/ja/messages'; import ja from '../build/locales/ja/messages';
import en from '../build/locales/en/messages'; import en from '../build/locales/en/messages';
import Login from './pages/Login'; import Login from './pages/Login';
import HelpDropdown from './components/HelpDropdown'; import HelpDropdown from './components/HelpDropdown';
import LogoutButton from './components/LogoutButton'; import LogoutButton from './components/LogoutButton';
import TowerLogo from './components/TowerLogo'; import TowerLogo from './components/TowerLogo';
import ConditionalRedirect from './components/ConditionalRedirect';
import NavExpandableGroup from './components/NavExpandableGroup'; import NavExpandableGroup from './components/NavExpandableGroup';
const catalogs = { en, ja }; const catalogs = { en, ja };
@@ -61,6 +60,7 @@ class App extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
// initialize with a closed navbar if window size is small
const isNavOpen = typeof window !== 'undefined' const isNavOpen = typeof window !== 'undefined'
&& window.innerWidth >= parseInt(breakpointMd.value, 10); && window.innerWidth >= parseInt(breakpointMd.value, 10);
@@ -95,10 +95,9 @@ class App extends React.Component {
} }
render () { render () {
const { isNavOpen } = this.state; const { config, isNavOpen } = this.state;
const { logo, loginInfo, history, routeConfig = [] } = this.props;
// extract a flattened array of all routes from the provided route config // extract a flattened array of all routes from the provided route config
const allRoutes = routeConfig.reduce((flattened, { routes }) => flattened.concat(routes), []); const { logo, loginInfo, routeGroups = [] } = this.props;
return ( return (
<Router> <Router>
@@ -108,22 +107,25 @@ class App extends React.Component {
> >
<I18n> <I18n>
{({ i18n }) => ( {({ i18n }) => (
<Fragment> api.isAuthenticated () ? (
<ConfigContext.Provider> <ConfigContext.Provider value={config}>
<BackgroundImage src={backgroundConfig} />
<Switch> <Switch>
<ConditionalRedirect <Route path="/login" render={() => <Redirect to='/home' />} />
shouldRedirect={() => api.isAuthenticated()} <Route exact path="/" render={() => <Redirect to='/home' />} />
redirectPath="/" <Route render={() => (
path="/login"
component={() => <Login logo={logo} loginInfo={loginInfo} />}
/>
<Fragment> <Fragment>
<BackgroundImage src={backgroundConfig} />
<Page <Page
usecondensed="True" usecondensed="True"
header={( header={(
<PageHeader <PageHeader
logo={<TowerLogo onClick={this.onLogoClick} />} showNavToggle
onNavToggle={() => this.onNavToggle()}
logo={(
<TowerLogo
onClick={this.onLogoClick}
/>
)}
toolbar={( toolbar={(
<Toolbar> <Toolbar>
<ToolbarGroup> <ToolbarGroup>
@@ -131,13 +133,13 @@ class App extends React.Component {
<HelpDropdown /> <HelpDropdown />
</ToolbarItem> </ToolbarItem>
<ToolbarItem> <ToolbarItem>
<LogoutButton onDevLogout={() => this.onDevLogout()} /> <LogoutButton
onDevLogout={() => this.onDevLogout()}
/>
</ToolbarItem> </ToolbarItem>
</ToolbarGroup> </ToolbarGroup>
</Toolbar> </Toolbar>
)} )}
showNavToggle
onNavToggle={this.onNavToggle}
/> />
)} )}
sidebar={( sidebar={(
@@ -146,44 +148,39 @@ class App extends React.Component {
nav={( nav={(
<Nav aria-label={i18n._("Primary Navigation")}> <Nav aria-label={i18n._("Primary Navigation")}>
<NavList> <NavList>
{ routeConfig.map(({ groupId, routes, title }) => ( {
<NavExpandableGroup routeGroups.map(params => <NavExpandableGroup key={params.groupId} {...params} />)
key={groupId} }
groupId={groupId}
title={i18n._(title)}
routes={routes.map(route => ({
path: route.path,
component: route.component,
title: i18n._(route.title)
}))}
/>
))}
</NavList> </NavList>
</Nav> </Nav>
)} )}
/> />
)} )}
> >
<ConditionalRedirect {
shouldRedirect={() => !api.isAuthenticated()} //
redirectPath="/login" // Extract a flattened array of all route params from the provided route groups
exact path="/" // and use it to create the route components.
component={() => (<Redirect to="/home" />)} //
/> // [{ routes }, { routes }] -> [route, route, route] -> (<Route/><Route/><Route/>)
{ allRoutes.map(({ component, path }) => ( //
<ConditionalRedirect routeGroups
key={path} .reduce((allRoutes, { routes }) => allRoutes.concat(routes), [])
path={path} .map(({ component: Component, path }) => (
redirectPath="/login" <Route key={path} path={path} render={params => <Component {...params } />} />
shouldRedirect={() => !api.isAuthenticated()} ))
component={component} }
/>
))}
</Page> </Page>
</Fragment> </Fragment>
)} />
</Switch> </Switch>
</ConfigContext.Provider> </ConfigContext.Provider>
</Fragment> ) : (
<Switch>
<Route path="/login" render={() => <Login logo={logo} loginInfo={loginInfo} />} />
<Redirect to="/login" />
</Switch>
)
)} )}
</I18n> </I18n>
</I18nProvider> </I18nProvider>

View File

@@ -1,23 +0,0 @@
import React from 'react';
import {
Route,
Redirect
} from 'react-router-dom';
const ConditionalRedirect = ({
component: Component,
shouldRedirect,
redirectPath,
location,
...props
}) => (shouldRedirect() ? (
<Redirect to={{
pathname: redirectPath,
state: { from: location }
}}
/>
) : (
<Route {...props} render={rest => (<Component {...rest} />)} />
));
export default ConditionalRedirect;