Created
July 28, 2020 07:12
-
-
Save bogdanq/b42c1ee085338245f7d10f1e2052212f 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
const Guard = ({ | |
guards = [], | |
redirect = "/", | |
exact = true, | |
component: Component, | |
pageTitle, | |
path, | |
}) => { | |
const { checkPermissions } = usePermissions(); | |
const { loading } = useUser(); | |
const { | |
language: { code }, | |
} = useLanguage(); | |
const pageName = React.useMemo(() => { | |
const str = path.split("/").pop(); | |
return !str || str === code ? "landing" : str; | |
}, [path, code]); | |
const { hiddenPages } = React.useContext | |
Context | |
); | |
const hasCompletedGuards = React.useMemo(() => checkPermissions(guards), [ | |
guards, | |
checkPermissions, | |
]); | |
if (loading) { | |
return null; | |
} | |
if (exact && hiddenPages.includes(pageName)) { | |
return <LocalizedRedirect to={redirect} />; | |
} | |
return hasCompletedGuards ? ( | |
<Route | |
path={path} | |
exact={exact} | |
render={(props: RouteComponentProps) => ( | |
<HelmetWrapper pageTitle={pageTitle}> | |
<Component {...props} /> | |
</HelmetWrapper> | |
)} | |
/> | |
) : ( | |
<LocalizedRedirect to={redirect} /> | |
); | |
}; | |
Guard.defaultProps = { | |
exact: true, | |
}; | |
/* | |
<Guard | |
guards={[onlyAuth]} | |
path={urljoin(matchUrl, "url} | |
component={Component} | |
/> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment