From 646f9b798b6b02846663ff546155361068c2955a Mon Sep 17 00:00:00 2001 From: Amarnath AS Date: Sat, 9 Mar 2024 16:52:10 +0530 Subject: [PATCH 1/4] feat: admin rights managment in client pages --- client/src/pages/AdminPages/Rights/Rights.jsx | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 client/src/pages/AdminPages/Rights/Rights.jsx diff --git a/client/src/pages/AdminPages/Rights/Rights.jsx b/client/src/pages/AdminPages/Rights/Rights.jsx new file mode 100644 index 0000000..64130f4 --- /dev/null +++ b/client/src/pages/AdminPages/Rights/Rights.jsx @@ -0,0 +1,136 @@ +import { useEffect, useState } from "react"; +import AddRight from "../../../components/AddRight/AddRight"; +import EditRight from "../../../components/EditRight/EditRight"; +import { getRight } from "../../../Services/rightServices"; +import { getDelete } from "../../../Services/rightServices"; +import timeFormat from "../../../utils/timeFormat"; +import { toast } from "react-toastify"; + +function Rights() { + const [openModal, setOpenModal] = useState(false); + const [rightDetails, setRightDetails] = useState([]); + + const [editRight, setEditRight] = useState(); + const [editModal, setEditModal] = useState(false); + + const handleModal = () => { + setOpenModal(true); + }; + const handleEditModal = (rightId) => { + setEditModal(true); + const rightToEdit = rightDetails.find((right) => { + return right?._id === rightId; + }); + setEditRight(rightToEdit); + }; + useEffect(() => { + console.log("Runnning.."); + const getRights = async () => { + const response = await getRight(); + setRightDetails(response.rights); + }; + getRights(); + }, [ + setOpenModal, + setEditModal, + openModal, + editModal, + setRightDetails, + setEditRight, + ]); + const handleDelete = async (rightId) => { + const response = await getDelete(rightId); + if (response.success === true) { + setRightDetails((prevDetails) => + prevDetails.filter((right) => right._id !== rightId) + ); + toast.success(response.message); + } + }; + return ( + <> + + +
+
+ {/* Text Components */} +
+
+

Rights Management

+
+
+ +
+
+ {/* Table Component */} +
+ <> + + + + + + + + + + {rightDetails.map((right, index) => { + return ( + <> + + + + + + + + + + ); + })} +
+ No + + Name + + Date + + Edit or Delete +
+ {index + 1} + {right?.name} + {timeFormat(right?.createdAt)} + + + +
+ +
+
+
+ + ); +} +export default Rights; From 0847f2c02ec2fbc5516c8c2f7eff80fe7847bce8 Mon Sep 17 00:00:00 2001 From: Amarnath AS Date: Sat, 9 Mar 2024 16:53:35 +0530 Subject: [PATCH 2/4] feat: added the api calls for rights components --- client/src/components/AddRight/AddRight.jsx | 35 ++++++--------- client/src/components/EditRight/EditRight.jsx | 44 ++++++++++++------- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/client/src/components/AddRight/AddRight.jsx b/client/src/components/AddRight/AddRight.jsx index e9e8d38..d1d826a 100644 --- a/client/src/components/AddRight/AddRight.jsx +++ b/client/src/components/AddRight/AddRight.jsx @@ -1,12 +1,13 @@ import { useState } from "react"; import { Modal } from "flowbite-react"; import { createRight } from "../../Services/rightServices"; +import { toast } from "react-toastify"; // eslint-disable-next-line react/prop-types function AddRight({ setOpenModal, openModal }) { const [formData, setFormData] = useState({ - right_name: "", - right_desc: "", + name: "", + content: "", }); const handleChange = (e) => { const { name, value } = e.target; @@ -17,13 +18,18 @@ function AddRight({ setOpenModal, openModal }) { }; const handleCreate = async () => { - if (!formData.right_name || !formData.right_desc) { + if (!formData.name || !formData.content) { console.log("Data fields Missing"); return; } try { const response = await createRight(formData); - console.log("response in the page", response); + if (response.success === true) { + formData.name = ""; + formData.content = ""; + toast.success(response.message); + setOpenModal(false); + } } catch (error) { console.log("Error occurred", error); } @@ -42,9 +48,9 @@ function AddRight({ setOpenModal, openModal }) { @@ -52,27 +58,14 @@ function AddRight({ setOpenModal, openModal }) { - {/*
- - -
*/} From 8e1bfe3f91ddd6ad65cec4a0d4fa19beb9251bfa Mon Sep 17 00:00:00 2001 From: Amarnath AS Date: Sat, 9 Mar 2024 16:54:45 +0530 Subject: [PATCH 3/4] feat: Admin right services added --- client/src/Services/rightServices.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/src/Services/rightServices.js b/client/src/Services/rightServices.js index 2593405..ef248e2 100644 --- a/client/src/Services/rightServices.js +++ b/client/src/Services/rightServices.js @@ -8,8 +8,10 @@ export const createRight = async (rightDetails) => { return error; } } -export const updateRight = async (rightDetails, rightId) => { +export const editRight = async (rightDetails, rightId) => { try { + console.log("details", rightDetails) + console.log("right id", rightId) const response = await thaliaAPI.put(`/admin/rights/${rightId}`, rightDetails, { withCredentials: true }); return response.data; } catch (error) { From 196940fe51c30f3a743893c46f9df6eccf6eca29 Mon Sep 17 00:00:00 2001 From: Amarnath AS Date: Sat, 9 Mar 2024 17:02:09 +0530 Subject: [PATCH 4/4] feat: admin time formatter added --- client/src/pages/AdminPages/Rights/Rights.tsx | 98 ------------------- client/src/utils/timeFormat.js | 6 ++ 2 files changed, 6 insertions(+), 98 deletions(-) delete mode 100644 client/src/pages/AdminPages/Rights/Rights.tsx create mode 100644 client/src/utils/timeFormat.js diff --git a/client/src/pages/AdminPages/Rights/Rights.tsx b/client/src/pages/AdminPages/Rights/Rights.tsx deleted file mode 100644 index 78f812f..0000000 --- a/client/src/pages/AdminPages/Rights/Rights.tsx +++ /dev/null @@ -1,98 +0,0 @@ -import React, { useEffect, useState } from "react"; -import AddRight from "../../../components/AddRight/AddRight"; -import EditRight from "../../../components/EditRight/EditRight"; -import { getRight } from "../../../Services/rightServices"; -import { getDelete } from "../../../Services/rightServices"; - -function Rights() { - const [openModal, setOpenModal] = useState(false); - const [rightDetails, setRightDetails] = useState(); - const handleModal = () => { - setOpenModal(true); - }; - useEffect(() => { - const getRights = async () => { - const response = await getRight(); - console.log("This is the response", response); - }; - }, []); - const handleDelete = async (rightId: string) => { - const response = await getDelete(rightId); - }; - return ( - <> - - -
-
- {/* Text Components */} -
-
-

Rights Management

-
-
- -
-
- {/* Table Component */} -
- - - - - - - - - - - - - - - - - -
- No - - Name - - Date - - Edit or Delete -
- 1 - Women Council Act 30418-03-2003 - - -
-
-
-
- - ); -} -export default Rights; diff --git a/client/src/utils/timeFormat.js b/client/src/utils/timeFormat.js new file mode 100644 index 0000000..47ccf08 --- /dev/null +++ b/client/src/utils/timeFormat.js @@ -0,0 +1,6 @@ +const timeFormat = (mongoDate) => { + const date = new Date(mongoDate); + const options = { month: '2-digit', day: '2-digit', year: 'numeric' }; + return date.toLocaleDateString('en-US', options); +} +export default timeFormat; \ No newline at end of file