From a5f54f1794780cf5d39d421e94f47d99f9a3375a Mon Sep 17 00:00:00 2001 From: karimaldeen Date: Wed, 21 Aug 2024 17:30:31 +0300 Subject: [PATCH] add protected route --- src/App.tsx | 10 +++- .../Routes/RenderRoutesRecursively.tsx | 7 +++ src/Layout/Ui/SideBar.tsx | 8 ++- src/Pages/Auth/LoginForm.tsx | 27 +++++++-- src/Pages/Student/Model/ModelForm.tsx | 6 +- src/Pages/Student/Model/formUtil.ts | 14 +++-- src/Pages/Student/useTableColumns.tsx | 27 +++++++-- src/Routes.tsx | 23 ++++---- src/enums/UserType.ts | 5 +- src/types/App.ts | 4 +- src/types/Student.ts | 59 +++++++------------ src/utils/RoleByType.ts | 28 +++++++++ src/zustand/AuthState.ts | 13 ++-- 13 files changed, 155 insertions(+), 76 deletions(-) create mode 100644 src/utils/RoleByType.ts diff --git a/src/App.tsx b/src/App.tsx index d28e884..352923b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -5,11 +5,13 @@ import { Spin } from "antd"; import { hasAbility } from "./utils/hasAbility"; import { renderRoutesRecursively } from "./Components/Routes/RenderRoutesRecursively"; import { RenderRouteElement } from "./Components/Routes/RenderRouteElement"; +import { UserTypeEnum } from "./enums/UserType"; +import { RoleByType } from "./utils/RoleByType"; const Page404 = lazy(() => import("./Layout/Ui/NotFoundPage")); const Auth = lazy(() => import("./Pages/Auth/Page")); - const App = () => { + return ( { } /> + {renderRoutesRecursively(menuItems)} {CrudRoute.map((route) => { - const useAbility = hasAbility(route.abilities, route.abilities_value); + const useAbility = hasAbility(route.abilities, route.abilities_value); + if(!RoleByType(route)){ + return false ; + } if (!useAbility) { return false; } diff --git a/src/Components/Routes/RenderRoutesRecursively.tsx b/src/Components/Routes/RenderRoutesRecursively.tsx index 9769870..874aa47 100644 --- a/src/Components/Routes/RenderRoutesRecursively.tsx +++ b/src/Components/Routes/RenderRoutesRecursively.tsx @@ -3,10 +3,17 @@ import { TMenuItem } from "../../types/App"; import { hasAbility } from "../../utils/hasAbility"; import { Route } from "react-router-dom"; import { RenderRouteElement } from "./RenderRouteElement"; +import { UserTypeEnum } from "../../enums/UserType"; +import Item from "antd/es/list/Item"; +import { RoleByType } from "../../utils/RoleByType"; export const renderRoutesRecursively = (routes: TMenuItem[]) => routes.map((route: TMenuItem) => { const useAbility = hasAbility(route.abilities, route.abilities_value); + if(!RoleByType(route)){ + return false ; + } + if (!useAbility) { return false; } diff --git a/src/Layout/Ui/SideBar.tsx b/src/Layout/Ui/SideBar.tsx index a87e44f..4cdf3e5 100644 --- a/src/Layout/Ui/SideBar.tsx +++ b/src/Layout/Ui/SideBar.tsx @@ -9,6 +9,8 @@ import { useTranslation } from "react-i18next"; import { getLocalStorage } from "../../utils/LocalStorage"; import { BRANCH_OBJECT_KEY } from "../../config/AppKey"; import { MenuItem } from "../../Components/Layout/SideBar/MenuItem"; +import { UserTypeEnum } from "../../enums/UserType"; +import { RoleByType } from "../../utils/RoleByType"; const SideBar = () => { const location = useLocation(); @@ -17,7 +19,6 @@ const SideBar = () => { const { logout } = useAuthState(); const [t] = useTranslation(); const branch_name = getLocalStorage(BRANCH_OBJECT_KEY)?.name; - return (

@@ -31,6 +32,11 @@ const SideBar = () => { if (!useAbility) { return ; } + if(!RoleByType(item)){ + + return ; + } + return ( { const { mutate, isLoading, isSuccess, data } = useLoginAdmin(); @@ -16,12 +19,28 @@ const LoginForm = () => { mutate(values); }; - const { login } = useAuthState(); + const { login,isAuthenticated} = useAuthState(); const LoginData = { ...data, - } as any; - useNavigateOnSuccess(isSuccess, "/", () => login(LoginData?.data as any)); + } as any; + const navigate = useNavigate() + const LocalType = getLocalStorage(USER_KEY)?.type ?? false ; + useEffect(() => { + + if(isSuccess){ + login(LoginData?.data as any) + + } + }, [isSuccess]) + + useEffect(() => { + if(LocalType ){ + window.location.href = ("/") + } + }, [LocalType]) + + return (
diff --git a/src/Pages/Student/Model/ModelForm.tsx b/src/Pages/Student/Model/ModelForm.tsx index a7cefd5..e8252ec 100644 --- a/src/Pages/Student/Model/ModelForm.tsx +++ b/src/Pages/Student/Model/ModelForm.tsx @@ -18,7 +18,10 @@ const Form = ({ isEdit = false }: { isEdit?: boolean }) => { const canChangeGradePage = !!Grade?.links?.next; const GradePage = Grade?.meta?.currentPage; - + const sex = [ + {name:"male" , id :"male"}, + {name:"female" , id :"female"} + ] return ( @@ -45,6 +48,7 @@ const Form = ({ isEdit = false }: { isEdit?: boolean }) => { page={GradePage} /> + diff --git a/src/Pages/Student/Model/formUtil.ts b/src/Pages/Student/Model/formUtil.ts index f1f1bfc..b65322e 100644 --- a/src/Pages/Student/Model/formUtil.ts +++ b/src/Pages/Student/Model/formUtil.ts @@ -5,17 +5,23 @@ export const getInitialValues = ( objectToEdit: Partial, ): StudentInitialValues => { return { - id: objectToEdit?.id, + id: objectToEdit?.user_id, first_name: objectToEdit?.first_name ?? "", last_name: objectToEdit?.last_name ?? "", - username: objectToEdit?.user?.username ?? null , - password: null , + // address: objectToEdit?.address ?? "", + // birthday: objectToEdit?.birthday ?? "", + // city: objectToEdit?.city ?? "", + grade_id: objectToEdit?.grade_id , + // image: objectToEdit?.image ?? "", + sex: objectToEdit?.sex , + }; }; export const getValidationSchema = () => { // validate input return Yup.object().shape({ - name: Yup.string().required("validation.required"), + first_name: Yup.string().required("validation.required"), + last_name: Yup.string().required("validation.required"), }); }; diff --git a/src/Pages/Student/useTableColumns.tsx b/src/Pages/Student/useTableColumns.tsx index 816b9b3..16fc540 100644 --- a/src/Pages/Student/useTableColumns.tsx +++ b/src/Pages/Student/useTableColumns.tsx @@ -13,7 +13,6 @@ import { canShowStudent, } from "../../utils/hasAbilityFn"; import ActionButtons from "../../Components/Table/ActionButtons"; -import ColumnsImage from "../../Components/Columns/ColumnsImage"; export const useColumns = () => { const { handel_open_model } = useModalHandler(); @@ -22,7 +21,7 @@ export const useColumns = () => { const navigate = useNavigate(); const handelShow = (record: Student) => { - navigate(`${record?.id}`); + navigate(`${record?.user_id}`); }; const handelDelete = (data: Student) => { @@ -42,15 +41,31 @@ export const useColumns = () => { dataIndex: "id", key: "id", align: "center", - render: (_text, record) => record?.id, + render: (_text, record) => record?.user_id, }, { - title: `${t("columns.name")}`, - dataIndex: "name", - key: "name", + title: `${t("columns.first_name")}`, + dataIndex: "first_name", + key: "first_name", align: "center", render: (_text, record) => record?.first_name, }, + { + title: `${t("columns.last_name")}`, + dataIndex: "last_name", + key: "last_name", + align: "center", + render: (_text, record) => record?.last_name, + }, + { + title: `${t("columns.sex")}`, + dataIndex: "sex", + key: "sex", + align: "center", + render: (_text, record) => record?.sex, + }, + + { title: canAddStudent ? (