diff --git a/src/CompletedBody/CompletedBody.jsx b/src/CompletedBody/CompletedBody.jsx index 956ca91..f82a861 100644 --- a/src/CompletedBody/CompletedBody.jsx +++ b/src/CompletedBody/CompletedBody.jsx @@ -16,12 +16,9 @@ const CompletedBody = () => { const [output, setOutput] = useState(""); // State to store the output const { questionId } = useParams(); // Getting questionId from the URL params const [question, setQuestion] = useState(""); - const [response, setResponse] = useState( - JSON.parse(localStorage.getItem("response")) || null - ); // Retrieve response from localStorage if available + const [response, setResponse] = useState(null); const location = useLocation(); - // Extract studentId and paperId from location state const studentId = location.state?.studentId || ""; const paperId = location.state?.paperId || ""; const questionUrl = @@ -29,30 +26,28 @@ const CompletedBody = () => { const responseUrl = "http://localhost:5000/student/getResponse"; useEffect(() => { - // Fetch question details - axios - .post(questionUrl, { questionId }) - .then((res) => { - setQuestion(res.data.question); - }) - .catch((err) => { - console.error(err); - }); + if (questionId) { + axios + .post(questionUrl, { questionId }) + .then((res) => { + setQuestion(res.data.question); + }) + .catch((err) => { + console.error(err); + }); + } - // Fetch the response by paperId and studentId if both are available - if (paperId && studentId && !response) { + if (paperId && studentId && questionId) { axios .post(responseUrl, { paperId, studentId }) .then((res) => { setResponse(res.data.response); // Store the response data - localStorage.setItem("response", JSON.stringify(res.data.response)); // Save response to localStorage }) .catch((err) => { console.error("Error fetching response:", err); }); } - // Resize observer const observer = new ResizeObserver((entries) => { if (bodyContentsRef.current) { const { width } = entries[0].contentRect; @@ -71,7 +66,7 @@ const CompletedBody = () => { } observer.disconnect(); }; - }, [questionId, paperId, studentId, questionUrl, response]); + }, [questionId, paperId, studentId, questionUrl]); return ( <> @@ -80,8 +75,6 @@ const CompletedBody = () => {
- - {/* Conditionally render based on width */} {isSmallWidth ? (

Code

diff --git a/src/CompletedNavbar/CompletedNavbar.jsx b/src/CompletedNavbar/CompletedNavbar.jsx index 8be9ad2..464d40f 100644 --- a/src/CompletedNavbar/CompletedNavbar.jsx +++ b/src/CompletedNavbar/CompletedNavbar.jsx @@ -1,16 +1,19 @@ import React, { useState, useEffect } from "react"; import "./CompletedNavbar.css"; import axios from "axios"; -import { FaChevronLeft, - FaChevronRight, } from "react-icons/fa"; -import { useParams} from "react-router-dom"; +import { FaChevronLeft, FaChevronRight } from "react-icons/fa"; +import { useParams, useNavigate, useLocation } from "react-router-dom"; const CompletedNavbar = () => { const [allotDisplay, setAllotDisplay] = useState(false); const { questionId } = useParams(); const [marks, setMarks] = useState(""); - const [question,setQuestion] = useState(); - const [studentDetails, setStudentDetails] = useState([]); + const [question, setQuestion] = useState(null); + const [studentDetails, setStudentDetails] = useState({}); + const navigate = useNavigate(); + const location = useLocation(); + + const studentId = location.state?.studentId || ""; const handleMarksChange = (e) => { const value = e.target.value.replace(/[^0-9]/g, ""); @@ -30,9 +33,10 @@ const CompletedNavbar = () => { if (response.status === 200) { const nextQuestion = response.data?.question; if (nextQuestion && nextQuestion._id) { - setQuestion(response.data.question); - // Use React Router's navigate to change route - window.location.href=`/Evaluation/${nextQuestion._id}`; + setQuestion(nextQuestion); + navigate(`/Evaluation/${nextQuestion._id}`, { + state: { studentId }, + }); } } } catch (err) { @@ -49,45 +53,55 @@ const CompletedNavbar = () => { }; useEffect(() => { - axios - .post("http://localhost:5000/student/getStudentDetailsByStudentId", { - studentId: JSON.parse(localStorage.getItem("response")).studentId, - }) - .then((res) => { - setStudentDetails(res.data.student[0]); - }) - .catch((err) => { - console.error(err); - }); - }, []); + if (studentId) { + axios + .post("http://localhost:5000/student/getStudentDetailsByStudentId", { + studentId, + }) + .then((res) => { + setStudentDetails(res.data.student[0]); + }) + .catch((err) => { + console.error(err); + }); + } + }, [studentId]); return ( - <> -
-
- {studentDetails.fullName} +
+
+ {studentDetails.fullName} +
+
+
+ +
Previous
-
-
- -
Previous
-
-
- Next Student -
-
-
Next
- -
+
+ Next Student
-
-
Allotted Marks:
- -
/20
- {allotDisplay &&
Allot
} +
+
Next
+
- +
+
Allotted Marks:
+ +
/20
+ {allotDisplay && ( +
Allot
+ )} +
+
); };