import React from 'react';
import { Route, Redirect, RouteProps } from 'react-router-dom';
import { getStateStore } from 'src/global-state/Store';
import { isUserHasPermissions } from 'src/utils/UserPermissions/hasPermissions';
interface ProtectedRouteProps extends RouteProps {
render: (props: any) => JSX.Element;
permissions: (match: any) => string[];
}
const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ render, permissions, ...rest }) => {
console.log({ permissions });
return (
<Route
{...rest}
render={(props) => {
const userPermissions = getStateStore().user.permissions;
const hasPermission = isUserHasPermissions(permissions(props.match));
return hasPermission ? (
render(props)
) : (
<Redirect
to={{
pathname: '/',
state: { from: props.location },
}}
/>
);
}}
/>
);
};
export default ProtectedRoute;