Snai3i-LandingPage-FormBuilder / frontend / src / layouts / Dashboard / index.tsx
index.tsx
Raw
import SideBar from './SideBar';
import PageContent from './PageContent';
import { Navigate, useLocation } from 'react-router-dom';
import useUser from '@/hooks/useUser';
import { useGetUserMutation } from '@/app/backend/endpoints/auth';
import { useEffectOnce } from '@/hooks/useEffectOnce';
import useTitle from '@/hooks/useTitle';

function DashboardLayout() {
  const [getUser] = useGetUserMutation();
  const { setUser, removeUser } = useUser();
  useTitle("Dashboard");

  useEffectOnce(() => {
    getUser()
      .unwrap()
      .then((response: any) => {
        setUser(response.data);
      })
      .catch(() => {
        removeUser();
      });
  });
  const location = useLocation();
  const { user } = useUser();

  if (location.pathname === '/admin') {
    return user?.role === 'superAdmin' ? (
      <Navigate to="/admin/users" />
    ) : (
      <Navigate to="/admin/forms" />
    );
  }
  return (
    <>
      <div className="w-full min-h-screen flex">
        <SideBar />
        <PageContent />
      </div>
    </>
  );
}

export default DashboardLayout;