Last active
February 17, 2020 19:05
-
-
Save evertonthepaula/a78cf481229c617c47a9e9f1cca6d467 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import { Route, Redirect } from 'react-router-dom'; | |
import AuthLayout from '~/screens/_layouts/auth'; | |
import DefaultLayout from '~/screens/_layouts/default'; | |
import { store } from '~/store'; | |
export default function RouteWrapper({ | |
component: Component, | |
isPrivate = false, | |
layout, | |
...rest | |
}) { | |
const { signed, firstLogin, aceptedTerms } = store.getState().auth; | |
if (!aceptedTerms && signed && rest.path !== '/termos-de-servico') { | |
return <Redirect to="termos-de-servico" />; | |
} | |
if (aceptedTerms && rest.path === '/termos-de-servico') { | |
return <Redirect to="/" />; | |
} | |
if (!firstLogin && rest.path === '/abertura') { | |
return <Redirect to="/" />; | |
} | |
if (!aceptedTerms && signed && rest.path !== '/termos-de-servico') { | |
return <Redirect to="termos-de-servico" />; | |
} | |
if (aceptedTerms && rest.path === '/termos-de-servico') { | |
return <Redirect to="/" />; | |
} | |
if (!firstLogin && rest.path === '/novo-ensino-medio') { | |
return <Redirect to="/" />; | |
} | |
if (!signed && isPrivate) { | |
return <Redirect to="/" />; | |
} | |
if (signed && !isPrivate) { | |
return <Redirect to="principal" />; | |
} | |
const Layout = layout === 'auth' ? AuthLayout : DefaultLayout; | |
return ( | |
<Route | |
{...rest} | |
render={props => ( | |
<Layout {...props}> | |
<Component {...props} /> | |
</Layout> | |
)} | |
/> | |
); | |
} | |
RouteWrapper.propTypes = { | |
isPrivate: PropTypes.bool, | |
component: PropTypes.oneOfType([PropTypes.element, PropTypes.func]) | |
.isRequired, | |
layout: PropTypes.string, | |
}; | |
RouteWrapper.defaultProps = { | |
isPrivate: false, | |
layout: '', | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment