diff --git a/src/Navbar/Navbar.jsx b/src/Navbar/Navbar.jsx index 1a0a264..a642eb8 100644 --- a/src/Navbar/Navbar.jsx +++ b/src/Navbar/Navbar.jsx @@ -24,8 +24,9 @@ const Navbar = () => { const paperId = localStorage.getItem("paperId"); const [questionList, setQuestionList] = useState([]); const studentId = localStorage.getItem("studentId"); - const [modalIsOpen, setModalIsOpen] = useState(false); // State to control modal visibility - const [modalMessage, setModalMessage] = useState(""); // State to control modal visibility + const [timeOutModalIsOpen, setTimeOutModalIsOpen] = useState(false); // State to control modal visibility + const [submitModalIsOpen, setSubmitModalIsOpen] = useState(false); // State to control modal visibility + const [modalMessage, setModalMessage] = useState(""); // State to control modal message const { questionId } = useParams(); const location = useLocation(); @@ -130,22 +131,26 @@ const Navbar = () => { } if (timeup) { setModalMessage( - "Test time is up, paper will now be submited automatically please confirm!!" + "Test time is up, your paper is submited automatically please exit!!" ); - setModalIsOpen(true); + setTimeOutModalIsOpen(true); + await onSubmit({ timeup: true }); } else { setModalMessage("Are you sure you want to submit?"); - setModalIsOpen(true); + setSubmitModalIsOpen(true); } } catch (error) { console.error("Error during submission:", error); } }; - const onSubmit = async () => { - await submitResponse(); - navigate("/"); - setModalIsOpen(false); + const onSubmit = async ({ timeup = false }) => { + await submitResponse({ontimeOut: true}); + if (!timeup) { + navigate("/"); + setTimeOutModalIsOpen(false); + } + setSubmitModalIsOpen(false); }; useEffect(() => { @@ -248,10 +253,21 @@ const Navbar = () => { {sidebarOpen &&
} onSubmit()} - onClose={() => setModalIsOpen(false)} + onClose={() => setSubmitModalIsOpen(false)} + message={modalMessage} + iserror={false} + /> + { + setTimeOutModalIsOpen(false); + localStorage.clear(); + navigate("/"); + }} message={modalMessage} iserror={false} /> diff --git a/src/SubmitFunction/Submit.js b/src/SubmitFunction/Submit.js index e53f795..fde134e 100644 --- a/src/SubmitFunction/Submit.js +++ b/src/SubmitFunction/Submit.js @@ -1,6 +1,6 @@ import axios from "axios"; -export const submitResponse = async () => { +export const submitResponse = async ({ ontimeOut = false }) => { try { // Get teacherId, studentId, and paperId from local storage const teacherId = localStorage.getItem("teacherId"); @@ -21,7 +21,8 @@ export const submitResponse = async () => { // Prepare the responses for each question const questionResponses = questions.map((question) => { const finalCode = localStorage.getItem(`code_${question._id}`); - const runHistory = JSON.parse(localStorage.getItem(`runHistory_${question._id}`)) || []; + const runHistory = + JSON.parse(localStorage.getItem(`runHistory_${question._id}`)) || []; return { questionId: question._id, @@ -46,11 +47,17 @@ export const submitResponse = async () => { if (submitResponse.status === 200) { console.log("Response submitted successfully!"); - // Clear all localStorage data - localStorage.clear(); + if (!ontimeOut) { + // Clear all localStorage data + localStorage.clear(); - // Redirect to the home page - window.location.href = "/"; + // Redirect to the home page + window.location.href = "/"; + }else{ + localStorage.removeItem("teacherId") + localStorage.removeItem("studentId") + localStorage.removeItem("paperId") + } } else { console.error("Failed to submit response:", submitResponse.statusText); }