From 7800fb471cd65cd7e2348f666894391975d1a745 Mon Sep 17 00:00:00 2001 From: Sianida26 Date: Mon, 5 Feb 2024 14:57:24 +0700 Subject: [PATCH] Added perission check and permission management --- .../roles/_modals/FormModal/FormModal.tsx | 93 ++++++++++++++++--- src/features/auth/authUtils.ts | 29 ++++-- src/features/auth/tools/getCurrentUser.ts | 21 ++++- .../dashboard/errors/DashboardError.ts | 33 ++++++- .../permissions/actions/getAllPermissions.ts | 48 ++++++++++ .../permissions/data/getPermissions.ts | 2 +- .../dashboard/roles/actions/getRoleById.ts | 72 ++++++++------ .../dashboard/roles/actions/upsertRole.ts | 56 +++++++---- src/features/dashboard/roles/data/getRoles.ts | 2 +- .../roles/formSchemas/RoleFormData.ts | 2 + .../dashboard/utils/withServerAction.ts | 23 +++-- src/types/Action.d.ts | 34 ++++--- 12 files changed, 311 insertions(+), 104 deletions(-) create mode 100644 src/features/dashboard/permissions/actions/getAllPermissions.ts diff --git a/src/app/dashboard/(auth)/roles/_modals/FormModal/FormModal.tsx b/src/app/dashboard/(auth)/roles/_modals/FormModal/FormModal.tsx index 3388ae5..29c03ad 100644 --- a/src/app/dashboard/(auth)/roles/_modals/FormModal/FormModal.tsx +++ b/src/app/dashboard/(auth)/roles/_modals/FormModal/FormModal.tsx @@ -1,5 +1,6 @@ /* eslint-disable react-hooks/exhaustive-deps */ import DashboardError from "@/features/dashboard/errors/DashboardError"; +import getAllPermissions from "@/features/dashboard/permissions/actions/getAllPermissions"; import getRoleById from "@/features/dashboard/roles/actions/getRoleById"; import upsertRole from "@/features/dashboard/roles/actions/upsertRole"; import roleFormDataSchema, { @@ -20,11 +21,13 @@ import { Skeleton, Fieldset, Alert, + Chip, } from "@mantine/core"; import { useForm, zodResolver } from "@mantine/form"; import { useRouter } from "next/navigation"; -import React, { useCallback, useEffect, useState } from "react"; +import React, { useCallback, useEffect, useMemo, useState } from "react"; import { TbDeviceFloppy } from "react-icons/tb"; +import { string } from "zod"; export interface ModalProps { title: string; @@ -45,6 +48,9 @@ export default function FormModal(props: ModalProps) { const [isSubmitting, setSubmitting] = useState(false); const [isFetching, setFetching] = useState(false); const [errorMessage, setErrorMessage] = useState(""); + const [allPermissions, setAllPermissions] = useState< + { code: string; name: string }[] | undefined + >(undefined); const form = useForm({ initialValues: { @@ -53,6 +59,7 @@ export default function FormModal(props: ModalProps) { id: "", isActive: false, name: "", + permissions: [], }, validate: zodResolver(roleFormDataSchema), validateInputOnChange: false, @@ -61,6 +68,29 @@ export default function FormModal(props: ModalProps) { }, }); + //Fetch Permissions + useEffect(() => { + setFetching(true); + withErrorHandling(getAllPermissions) + .then((response) => { + setAllPermissions(response.data); + }) + .catch((e) => { + if (e instanceof DashboardError) { + setErrorMessage(`ERROR: ${e.message} (${e.errorCode})`); + } else if (e instanceof Error) { + setErrorMessage(`ERROR: ${e.message}`); + } else { + setErrorMessage( + `Unkown error is occured. Please contact administrator` + ); + } + }) + .finally(() => { + setFetching(false); + }); + }, []); + /** * Fetches role data by ID and populates the form if the modal is opened and an ID is provided. */ @@ -70,22 +100,30 @@ export default function FormModal(props: ModalProps) { } setFetching(true); - getRoleById(props.id) + withErrorHandling(getRoleById, props.id) .then((response) => { - if (response.success) { - const data = response.data; - form.setValues({ - code: data.code, - description: data.description, - id: data.id, - isActive: data.isActive, - name: data.name, - }); - } + const data = response.data; + form.setValues({ + code: data.code, + description: data.description, + id: data.id, + isActive: data.isActive, + name: data.name, + permissions: data.permissions.map( + (permission) => permission.code + ), + }); }) .catch((e) => { - //TODO: Handle error - console.log(e); + if (e instanceof DashboardError) { + setErrorMessage(`ERROR: ${e.message} (${e.errorCode})`); + } else if (e instanceof Error) { + setErrorMessage(`ERROR: ${e.message}`); + } else { + setErrorMessage( + `Unkown error is occured. Please contact administrator` + ); + } }) .finally(() => { setFetching(false); @@ -94,11 +132,12 @@ export default function FormModal(props: ModalProps) { const closeModal = () => { props.onClose ? props.onClose() : router.replace("?"); + form.reset(); }; const handleSubmit = (values: RoleFormData) => { setSubmitting(true); - withErrorHandling(() => upsertRole(values)) + withErrorHandling(upsertRole, values) .then((response) => { showNotification(response.message!, "success"); closeModal(); @@ -187,6 +226,30 @@ export default function FormModal(props: ModalProps) { /> +
+ + !props.readonly && + form.setFieldValue("permissions", values) + } + > + + {allPermissions?.map((permission) => ( +
+ + {permission.code} + +
+ ))} +
+
+
+ {/* Buttons */}