Skip to content

Instantly share code, notes, and snippets.

@bogdanq
Created July 28, 2020 07:12
Show Gist options
  • Save bogdanq/b42c1ee085338245f7d10f1e2052212f to your computer and use it in GitHub Desktop.
Save bogdanq/b42c1ee085338245f7d10f1e2052212f to your computer and use it in GitHub Desktop.
Компонент скрытия роутов
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