Skip to content

Commit

Permalink
refactor forbidden redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
FreakDev committed Oct 26, 2017
1 parent 2f0e78f commit 5a1f101
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/ScreenManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from 'react';

import { withRouter, Switch } from 'react-router-dom'
import { withRouter, Switch, Redirect } from 'react-router-dom'
import { BrowserRouter as Router, Route } from 'react-router-dom';

import { TransitionGroup, CSSTransition } from 'react-transition-group'
Expand Down Expand Up @@ -73,7 +73,18 @@ export class ScreenManager extends Component {
const hideNavbarPath = pages.filter( r => r.props.hideNavbar ).map(r => r.props.path)
const navbarScreenInfos = pages.filter(r => !r.props.noNavbar).map(r => ({ name: r.props.name, path: r.props.path }))


const renderForbidden = (screen) => {
if (screen) {
if (screen.props.redirectTo) {
return (
<Redirect to={screen.props.redirectTo} />
)
} else {
return screen
}
} else
return null
}

const renderScreens = (pages) => pages.map((page, i) => {
const { exact, path, ...props } = page.props
Expand All @@ -82,7 +93,7 @@ export class ScreenManager extends Component {
if (!this.props.authCheck || (!page.props.protected || this.props.authCheck()))
return React.cloneElement(page, { ...props })
else
return forbiddenScreen || null
return renderForbidden(forbiddenScreen)
} } />
)
})
Expand Down
8 changes: 2 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from 'react'
import ReactDOM from 'react-dom'

import { Redirect } from 'react-router-dom';

import './index.css'

import ScreenManager from './ScreenManager'
Expand All @@ -16,7 +14,7 @@ ReactDOM.render(
<ScreenManager
authCheck={() => {
// return auth status
return true
return false
}}
>
<SplashScreen style={{ background: 'black' }} />
Expand All @@ -35,9 +33,7 @@ ReactDOM.render(
<Screen protected style={{ background: 'blue' }} name="Screen 3" exact path="/settings">
<h1>Screen 3</h1>
</Screen>
<ForbiddenScreen style={{ background: 'red' }} name="forbidden" exact path="/forbidden">
<Redirect to={{ pathname:"/" }} />
</ForbiddenScreen>
<ForbiddenScreen style={{ background: 'red' }} name="forbidden" exact path="/forbidden" redirectTo={{ pathname:"/" }} />
<NavigationBar position="bottom" />
</ScreenManager>
</div>
Expand Down

0 comments on commit 5a1f101

Please sign in to comment.