From 27d1cc09b4800b6e03235d33a371a69f3f7ba933 Mon Sep 17 00:00:00 2001 From: Kalex Date: Fri, 27 Oct 2023 17:06:51 +0200 Subject: [PATCH] implementation of update rolePermission --- package.json | 4 +- .../roles&permissions/ApplicationEntity.tsx | 82 +++-- .../roles&permissions/CohortEntity.tsx | 78 +++-- src/pages/TraineApplicant/Trainee.tsx | 1 + .../roles&permissions/RolePermission.tsx | 306 ++++++++++++++++-- src/routes/routes.tsx | 1 - 6 files changed, 365 insertions(+), 107 deletions(-) diff --git a/package.json b/package.json index 96d2d93bd..99e652b6f 100755 --- a/package.json +++ b/package.json @@ -89,7 +89,7 @@ "date-fns": "^2.29.3", "dotenv": "^16.0.3", "flowbite": "^1.5.3", - "formik": "^2.4.3", + "formik": "^2.4.5", "googleapis": "^126.0.1", "graphql": "^16.6.0", "graphql-request": "^5.1.0", @@ -124,7 +124,7 @@ "stream-http": "^3.2.0", "ts-jest": "^29.0.3", "with-click-outside": "^1.0.1", - "yup": "^1.2.0", + "yup": "^1.3.2", "zod": "^3.22.2" }, "browserslist": { diff --git a/src/components/roles&permissions/ApplicationEntity.tsx b/src/components/roles&permissions/ApplicationEntity.tsx index b2b97ddd0..ba8e244d3 100644 --- a/src/components/roles&permissions/ApplicationEntity.tsx +++ b/src/components/roles&permissions/ApplicationEntity.tsx @@ -1,5 +1,3 @@ -import * as IoIcons from 'react-icons/io5'; - function ApplicationEntity({ appSelectedEntity, setAppSelectedEntity, @@ -12,36 +10,32 @@ function ApplicationEntity({ return (
{appOpen && ( -
- {/* setOpenP(false)} - className='cursor-pointer dark:text-white top-[10px] text-[25px] flex justify-end absolute right-0' - /> */} -
-
+
+
+
setAppPermissions((currentState) => [ ...currentState, @@ -52,10 +46,10 @@ function ApplicationEntity({ create
-
+
setAppPermissions((currentState) => [ ...currentState, @@ -67,10 +61,10 @@ function ApplicationEntity({
-
+
setAppPermissions((currentState) => [ ...currentState, @@ -82,10 +76,10 @@ function ApplicationEntity({
-
+
setAppPermissions((currentState) => [ ...currentState, @@ -97,10 +91,10 @@ function ApplicationEntity({
-
+
setAppPermissions((currentState) => [ ...currentState, @@ -112,10 +106,10 @@ function ApplicationEntity({
-
+
setAppPermissions((currentState) => [ ...currentState, @@ -127,10 +121,10 @@ function ApplicationEntity({
-
+
setAppPermissions((currentState) => [ ...currentState, @@ -142,10 +136,10 @@ function ApplicationEntity({
-
+
setAppPermissions((currentState) => [ ...currentState, @@ -157,10 +151,10 @@ function ApplicationEntity({
-
+
setAppPermissions((currentState) => [ ...currentState, @@ -172,10 +166,10 @@ function ApplicationEntity({
-
+
setAppPermissions((currentState) => [ ...currentState, diff --git a/src/components/roles&permissions/CohortEntity.tsx b/src/components/roles&permissions/CohortEntity.tsx index 61add23bc..65d556e6f 100644 --- a/src/components/roles&permissions/CohortEntity.tsx +++ b/src/components/roles&permissions/CohortEntity.tsx @@ -1,5 +1,3 @@ -import * as IoIcons from 'react-icons/io5'; - function CohortEntity({ selectedCohortEntity, setSelectedCohortEntity, @@ -12,36 +10,36 @@ function CohortEntity({ return (
{openCohort && ( -
+
{/* setOpenP(false)} className='cursor-pointer dark:text-white top-[10px] text-[25px] flex justify-end absolute right-0' /> */} -
-
+
+
setCohortPermissions((currentState) => [ ...currentState, @@ -52,10 +50,10 @@ function CohortEntity({ create
-
+
setCohortPermissions((currentState) => [ ...currentState, @@ -67,10 +65,10 @@ function CohortEntity({
-
+
setCohortPermissions((currentState) => [ ...currentState, @@ -82,10 +80,10 @@ function CohortEntity({
-
+
setCohortPermissions((currentState) => [ ...currentState, @@ -97,10 +95,10 @@ function CohortEntity({
-
+
setCohortPermissions((currentState) => [ ...currentState, @@ -112,10 +110,10 @@ function CohortEntity({
-
+
setCohortPermissions((currentState) => [ ...currentState, @@ -127,10 +125,10 @@ function CohortEntity({
-
+
setCohortPermissions((currentState) => [ ...currentState, @@ -142,10 +140,10 @@ function CohortEntity({
-
+
setCohortPermissions((currentState) => [ ...currentState, @@ -157,10 +155,10 @@ function CohortEntity({
-
+
setCohortPermissions((currentState) => [ ...currentState, @@ -172,10 +170,10 @@ function CohortEntity({
-
+
setCohortPermissions((currentState) => [ ...currentState, diff --git a/src/pages/TraineApplicant/Trainee.tsx b/src/pages/TraineApplicant/Trainee.tsx index b79a00dff..ed12aaaca 100755 --- a/src/pages/TraineApplicant/Trainee.tsx +++ b/src/pages/TraineApplicant/Trainee.tsx @@ -64,6 +64,7 @@ const AddTrainee = (props: any) => { useEffect(() => { dispatch(fetchtraine(input)); }, [delettraine, softdeletettraine, page, itemsPerPage, itemsPerPage]); + const [moredrop, setmoredrop] = useState(""); const onSubmitHandler = (userid: any) => { if (!moredrop) setmoredrop(userid); diff --git a/src/pages/roles&permissions/RolePermission.tsx b/src/pages/roles&permissions/RolePermission.tsx index dd84f2971..2f9c7bf85 100644 --- a/src/pages/roles&permissions/RolePermission.tsx +++ b/src/pages/roles&permissions/RolePermission.tsx @@ -21,25 +21,26 @@ import UserEntity from "../../components/roles&permissions/UserEntity"; import ApplicationEntity from "../../components/roles&permissions/ApplicationEntity"; import CohortEntity from "../../components/roles&permissions/CohortEntity"; import RoleEntity from "../../components/roles&permissions/RoleEntity"; +import { useFormik } from "formik"; export interface SingleRole { _id?: string; - roleName: string; - description: string; - permissions: Permission[]; + roleName?: string; + description?: string; + permissions?: Permission[]; } export type Permission = { - create: boolean; - deleteMultiple: boolean; - deleteOne: boolean; - deleteOwn: boolean; - entity: string; - updateMultiple: boolean; - updateOne: boolean; - updateOwn: boolean; - viewMultiple: boolean; - viewOne: boolean; - viewOwn: boolean; + create?: boolean; + deleteMultiple?: boolean; + deleteOne?: boolean; + deleteOwn?: boolean; + entity?: string; + updateMultiple?: boolean; + updateOne?: boolean; + updateOwn?: boolean; + viewMultiple?: boolean; + viewOne?: boolean; + viewOwn?: boolean; _id?: string; }; @@ -68,7 +69,26 @@ function RolePermission(props: any) { const [updateRoleId, setUpdateRoleId] = useState(null); const [isViewingSingleRole, setIsViewSingleRole] = useState(false); const [isSingleRole, setIsSingleRole] = useState(false); - const [singleRoleData, setSingleRoleData] = useState(null); + const [singleRoleData, setSingleRoleData] = useState< + SingleRole | null | undefined + >({ + roleName: "", + description: "", + permissions: [], + }); + const [updatePermissionData, setUpdatePermissionData] = useState({ + create: false, + deleteMultiple: false, + deleteOne: false, + deleteOwn: false, + entity: "", + updateMultiple: false, + updateOne: false, + updateOwn: false, + viewMultiple: false, + viewOne: false, + viewOwn: false, + }); const [currentDropdownIndex, setCurrentDropdownIndex] = useState< number | null >(null); @@ -289,6 +309,10 @@ function RolePermission(props: any) { setCurrentDropdownIndex(nextDropdownIndex); } }; + const handleInputChange = (e: any) => { + // const { name, value } = e.target; + setSingleRoleData({ ...singleRoleData, [e.target.name]: e.target.value }); + }; const handleViewSingleRole = async (getRoleId) => { try { @@ -333,8 +357,10 @@ function RolePermission(props: any) { }); const role = response.data.data.getRole; setSingleRoleData(role); + setOpenUpdateModel(true); setIsSingleRole(true); setIsViewSingleRole(true); + setUpdatePermissionData(role.permissions); } catch (error) { console.error("Error viewing role:", error); } @@ -344,6 +370,60 @@ function RolePermission(props: any) { setIsSingleRole(false); setSingleRoleData(null); }; + const handleUpdateRole = async (e: any) => { + e.preventDefault(); + console.log("update idd ===>", updatePermissionData); + // try { + // if (!getRoleId) { + // console.error("Role ID is null"); + // return; + // } + // const updatedData = { + // roleName: singleRoleData?.roleName, + // description: singleRoleData?.description, + // permissions: singleRoleData?.permissions, + // }; + // const response = await axios.post("/", { + // query: ` + // mutation UpdateRole($updateRoleId: ID!, $input: UpdateRoleInput!) { + // updateRole(id: $updateRoleId, input: $input) { + // _id + // roleName + // description + // permissions { + // entity + // _id + // create + // viewOwn + // viewMultiple + // viewOne + // updateOwn + // updateMultiple + // updateOne + // deleteOwn + // deleteMultiple + // deleteOne + // } + // } + // } + // `, + // variables: { + // updateRoleId: getRoleId, + // input: updatedData, + // }, + // headers: { + // "Content-Type": "application/json", + // Authorization: `${token}`, + // }, + // }); + + // const updatedRole = response.data.data.updateRole; + // console.log("Role updated:", updatedRole); + // setOpenUpdateModel(false); + // } catch (error) { + // console.error("Error updating role:", error); + // } + }; const COLS = [ { @@ -697,7 +777,7 @@ function RolePermission(props: any) { onClose={handleClose} open={open} > - handleOpenUpdateModal()}> + handleViewSingleRole(getRoleId)}> Edit @@ -912,22 +992,208 @@ function RolePermission(props: any) {
+
+ {singleRoleData?.permissions?.map((permission, index) => ( +
+
+

+ {permission.entity} +

+
+
+
+ + + setUpdatePermissionData((currentState) => ({ + ...currentState, + [e.target.name]: e.target.checked, + })) + } + checked={updatePermissionData.create} + /> +
+
+ + + setUpdatePermissionData((currentState) => ({ + ...currentState, + [e.target.name]: e.target.checked, + })) + } + checked={updatePermissionData.deleteMultiple} + /> +
+
+ + + setUpdatePermissionData((currentState) => ({ + ...currentState, + [e.target.name]: e.target.checked, + })) + } + checked={updatePermissionData.deleteOne} + /> +
+
+ + + setUpdatePermissionData((currentState) => ({ + ...currentState, + [e.target.name]: e.target.checked, + })) + } + checked={updatePermissionData.deleteOwn} + /> +
+
+ + + setUpdatePermissionData((currentState) => ({ + ...currentState, + [e.target.name]: e.target.checked, + })) + } + checked={updatePermissionData.updateOne} + /> +
+
+ + + setUpdatePermissionData((currentState) => ({ + ...currentState, + [e.target.name]: e.target.checked, + })) + } + checked={updatePermissionData.updateOwn} + /> +
+
+ + + setUpdatePermissionData((currentState) => ({ + ...currentState, + [e.target.name]: e.target.checked, + })) + } + checked={updatePermissionData.updateMultiple} + /> +
+
+ + + setUpdatePermissionData((currentState) => ({ + ...currentState, + [e.target.name]: e.target.checked, + })) + } + checked={updatePermissionData.viewOwn} + /> +
+
+ + + setUpdatePermissionData((currentState) => ({ + ...currentState, + [e.target.name]: e.target.checked, + })) + } + checked={updatePermissionData.viewOne} + /> +
+
+ + + setUpdatePermissionData((currentState) => ({ + ...currentState, + [e.target.name]: e.target.checked, + })) + } + checked={updatePermissionData.viewMultiple} + /> +
+
+
+ ))} +
diff --git a/src/routes/routes.tsx b/src/routes/routes.tsx index 87f1f7c18..ab4cc2767 100644 --- a/src/routes/routes.tsx +++ b/src/routes/routes.tsx @@ -13,7 +13,6 @@ import UpdateTraine from './../pages/updateTrainee/traineUpdate'; import CreateScoreType from './../pages/FilterTeainee/createScoreType'; import ScoreTypesActions from './../pages/FilterTeainee/ScoreTypesActions'; import ImportTraineeDetailsFromGoogleSheet from './../pages/importAndSaveManyTraineesFromGoogleSheet/importAndSaveManyTraineesFromGoogleSheet'; - const Counter = React.lazy(() => import('./../components/Counter/Counter')); import Trash from './../pages/Trash/Trash'; import ApplicationCycle from './../pages/ApplicationCycle/ApplicationCycle';