From c80cd0cbccc861f6836a609261da25265c4470b7 Mon Sep 17 00:00:00 2001 From: Nakul Krishnakumar Date: Sat, 2 Nov 2024 19:25:21 +0530 Subject: [PATCH] disables create req when already pending req exists --- .../components/StudentHome/StudentHome.css | 7 ++++ .../components/StudentHome/StudentHome.jsx | 23 +++++++++++-- server/routes/student.js | 34 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/app/src/components/StudentHome/StudentHome.css b/app/src/components/StudentHome/StudentHome.css index 8db8089..dc80121 100644 --- a/app/src/components/StudentHome/StudentHome.css +++ b/app/src/components/StudentHome/StudentHome.css @@ -47,6 +47,7 @@ } .std-req-btn { + color: #F0ECE5; background-color: #161A30; width: clamp(100px, 20vw ,150px); height: clamp(15px, 10vw, 40px); @@ -56,11 +57,17 @@ font-weight: 600; } + .std-req-btn:hover { cursor: pointer; background-color: #343a5e; } +.std-req-btn:disabled { + cursor: not-allowed; + background-color: #343a5e; +} + .std-table-cap { color: #161A30; font-size: large; diff --git a/app/src/components/StudentHome/StudentHome.jsx b/app/src/components/StudentHome/StudentHome.jsx index f63711c..a74434a 100644 --- a/app/src/components/StudentHome/StudentHome.jsx +++ b/app/src/components/StudentHome/StudentHome.jsx @@ -9,6 +9,7 @@ const StudentHome = (props) => { const [modal, setModal] = useState(false); const [appData, setAppData] = useState([]); const [order, setOrder] = useState(true); //if order true, then sorting in ascending order + const [disableBtn, setDisableBtn] = useState(false); // SET SORTING ORDER const handleOrder = () => { @@ -30,12 +31,29 @@ const StudentHome = (props) => { .catch((err) => console.error(err)); }; + const checkPendingReq = async (userID) => { + await axios + .post("http://localhost:5000/api/student/checkpending", { + userID: userID, + }) + .then(result => { + console.log(result); + setDisableBtn(result.data) + }) + } + useEffect(() => { if (props.userID) { fetchUser(props.userID, order); } }, [props.userID, order]); + useEffect(() => { + if (props.userID) { + checkPendingReq(props.userID); + } + }, [props.userID]); + // Disable body scroll when modal is open useEffect(() => { if (modal) { @@ -53,6 +71,7 @@ const StudentHome = (props) => { const toggleModal = () => { setModal(!modal); fetchUser(props.userID); + checkPendingReq(props.userID); }; return ( @@ -94,10 +113,10 @@ const StudentHome = (props) => { -
+
+ Your Requests diff --git a/server/routes/student.js b/server/routes/student.js index 3583628..622efbd 100644 --- a/server/routes/student.js +++ b/server/routes/student.js @@ -65,4 +65,38 @@ router.post("/fetchreq", async (req, res) => { }) +// ROUTE 3: api/student/checkpending +router.post("/checkpending", async (req, res) => { + const { userID } = req.body; + console.log("userid", userID); //testing + + // FETCHING FROM DATABASE + try { + const { data, error } = await db + .from('application') + .select('*') + .eq('student_id', userID) + .eq('app_status', 'Pending'); + + console.log(data); + + if (error) { + console.error("Error fetching applications:", error); + return res.status(500).json({ error: "Failed to fetch requests" }); + } else { + console.log("Length Of the Data From DB: ", data.length); + if (data.length === 0) { + return res.status(200).send(false); + } else { + return res.status(200).send(true); + } + } + + } catch (err) { + console.error("Unexpected error:", err); + return res.status(500).json({ error: "An unexpected error occurred" }); + } + +}) + module.exports = router; \ No newline at end of file