import React, { useMemo } from "react";
import { Redirect, Route } from "react-router";
import { useSelector } from "react-redux";
import { HOME_PAGE } from "../../constants/pages";
import { selectRoles } from "../../store/selectors/loginSelectors";

const AdminRoute = ({ ...props }) => {
  const role = useSelector(selectRoles);
  const isUserAdmin = useMemo(() => {
    if (!role?.includes("Admin")) return false;
    return true;
  }, [role]);

  return isUserAdmin ? <Route {...props} /> : <Redirect to={HOME_PAGE} />;
};

export default AdminRoute;
