Skip to content

Commit

Permalink
bugs resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartikey2011yadav committed Oct 2, 2024
1 parent bb69d56 commit ffcfb07
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
38 changes: 27 additions & 11 deletions src/Navbar/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -248,10 +253,21 @@ const Navbar = () => {
</div>
{sidebarOpen && <div className="overlay" onClick={toggleSidebar}></div>}
<AlertModal
isOpen={modalIsOpen}
isOpen={submitModalIsOpen}
isConfirm={true}
onConfirm={() => onSubmit()}
onClose={() => setModalIsOpen(false)}
onClose={() => setSubmitModalIsOpen(false)}
message={modalMessage}
iserror={false}
/>
<AlertModal
isOpen={timeOutModalIsOpen}
isConfirm={false}
onClose={() => {
setTimeOutModalIsOpen(false);
localStorage.clear();
navigate("/");
}}
message={modalMessage}
iserror={false}
/>
Expand Down
19 changes: 13 additions & 6 deletions src/SubmitFunction/Submit.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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,
Expand All @@ -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);
}
Expand Down

0 comments on commit ffcfb07

Please sign in to comment.