From a479b5ba292c71e9a49284f0497301efc55a4672 Mon Sep 17 00:00:00 2001 From: zakie khan <121384625+zakie2003@users.noreply.github.com> Date: Fri, 6 Sep 2024 21:52:39 +0530 Subject: [PATCH 1/5] Edit Delete function added --- src/App.js | 3 + src/Edit_paper/Editpaper.jsx | 224 ++++++++++++++++++ .../QuestionPaperDashboard.jsx | 33 ++- src/papers/papers.jsx | 79 +++++- 4 files changed, 337 insertions(+), 2 deletions(-) create mode 100644 src/Edit_paper/Editpaper.jsx diff --git a/src/App.js b/src/App.js index 94bcb71..611da48 100644 --- a/src/App.js +++ b/src/App.js @@ -10,6 +10,7 @@ import Reset_Password from './Reset_Password/Reset_Password'; import axios from 'axios'; import Question from './question/question'; import QuestionPaperDashboard from './QuestionPaperDashboard/QuestionPaperDashboard'; +import Editpaper from './Edit_paper/Editpaper'; const App = () => { const [isAuthenticated, setIsAuthenticated] = useState(false); @@ -61,7 +62,9 @@ const App = () => { <> } /> } /> + } /> } /> + } /> }/> )} diff --git a/src/Edit_paper/Editpaper.jsx b/src/Edit_paper/Editpaper.jsx new file mode 100644 index 0000000..dfebc28 --- /dev/null +++ b/src/Edit_paper/Editpaper.jsx @@ -0,0 +1,224 @@ +import React, { useState } from 'react'; +import ReactDatePicker from 'react-datepicker'; +import 'react-datepicker/dist/react-datepicker.css'; +import '../papers/papers.css'; +import PropTypes from 'prop-types'; +import Navbar from '../Navbar/Navbar'; +import axios from 'axios'; +import { useNavigate,useLocation } from 'react-router-dom'; +import AlertModal from '../AlertModal/AlertModal'; // Import AlertModal + +const Editpaper = () => { + const location = useLocation(); // Taking Current Values + const current_values = location.state; + + const [date, setDate] = useState(current_values.date); + const [time, setTime] = useState(current_values.time); + const [duration, setDuration] = useState({ hours: current_values.duration.hours, minutes: current_values.duration.minutes }); + const [marks, setMarks] = useState(current_values.marks); + const [testType, setTestType] = useState(current_values.testType); + const [className, setClassName] = useState(current_values.className); + const [semester, setSemester] = useState(current_values.semester); + const [subject, setSubject] = useState(current_values.subject); + const [subjectCode, setSubjectCode] = useState(current_values.subjectCode); + const teacherId = localStorage.getItem("teacherId"); + + const [modalIsOpen, setModalIsOpen] = useState(false); + const [modalMessage, setModalMessage] = useState(''); + const [isError, setIsError] = useState(false); + const navigate = useNavigate(); + const handleDurationChange = (field, value) => { + if (value >= 0) { + setDuration({ ...duration, [field]: value }); + } + }; + + const handleMarksChange = (e) => { + const value = e.target.value.replace(/[^0-9]/g, ''); + setMarks(value); + }; + + const handleSubmit = async (e) => { + e.preventDefault(); + + const paperData = { + _id:current_values._id, + className, + semester, + subject, + marks, + duration: { + hours: duration.hours, + minutes: duration.minutes, + }, + subjectCode, + time, + date, + testType, + teacherId, + }; + + try { + await axios.post('http://localhost:5000/paper/edit-paper', paperData); + // console.log('Paper created successfully:', response.data); + + setModalMessage('Paper edited successfully!'); + setIsError(false); + setModalIsOpen(true); + + + // const { paperId } = response.data; + + // Navigate to the QuestionPaperDashboard with the created paper's ID + navigate(`/teacherDashboard`); + } catch (error) { + console.error('Error creating paper:', error); + + setModalMessage('Failed to create paper. Please try again.'); + setIsError(true); + setModalIsOpen(true); + } + }; + + return ( + <> + +
+
+
+ + + + + + + +
+ + + setSubject(e.target.value)} + /> + + +
+ + + + + +
+ handleDurationChange('hours', e.target.value)} + /> + : + handleDurationChange('minutes', e.target.value)} + /> +
+
+
+ +
+ + setSubjectCode(e.target.value)} + /> + + + + + + setTime(e.target.value)} + /> + +
+ +
+ + setDate(date)} + dateFormat="dd/MM/yyyy" + className="create_paper_input" + placeholderText="DD / MM / YYYY" + /> + + + + + +
+ + +
+
+ + {/* Alert Modal */} + setModalIsOpen(false)} + message={modalMessage} + iserror={isError} + /> + + ); +}; + +const FormGroup = ({ label, children, className }) => { + return ( +
+ + {children} +
+ ); +}; + +FormGroup.propTypes = { + label: PropTypes.string.isRequired, + children: PropTypes.node.isRequired, + className: PropTypes.string, +}; + +export default Editpaper; diff --git a/src/QuestionPaperDashboard/QuestionPaperDashboard.jsx b/src/QuestionPaperDashboard/QuestionPaperDashboard.jsx index f3dc159..f684407 100644 --- a/src/QuestionPaperDashboard/QuestionPaperDashboard.jsx +++ b/src/QuestionPaperDashboard/QuestionPaperDashboard.jsx @@ -14,6 +14,7 @@ const QuestionPaperDashboard = () => { const { className, semester, subject, marks } = state || {}; const navigate = useNavigate(); const { paperId } = useParams(); + const [reload,setReload]= useState(false); const [questions, setQuestions] = useState([]); const [loading, setLoading] = useState(true); @@ -40,7 +41,7 @@ const QuestionPaperDashboard = () => { }; fetchQuestions(); - }, [paperId]); + }, [paperId,reload]); if (loading) { return
Loading...
; @@ -51,6 +52,34 @@ const QuestionPaperDashboard = () => { 0 ); + + const deleteQuestion=async (question)=> + { + console.log(question); + try + { + await axios.post('http://localhost:5000/paper/delete-question', { _id: question._id }) + setQuestions((prevQuestions) => prevQuestions.filter(q => q._id !== question._id)); + + if (questions.length === 1) { + setQuestions([]); + } + + setReload(prev => !prev); + } + catch(error) + { + console.error('Error creating paper:', error); + } + } + + const duplicateQuestion= async (question)=> + { + console.log(question); + await axios.post(`http://localhost:5000/paper/duplicate-question`, { question}) + setReload(prev => !prev); + + } const handleSubmit = () => { // Here, you would handle the submit logic. // Trigger a success modal on successful submission @@ -98,6 +127,8 @@ const QuestionPaperDashboard = () => { {question.questionDescription} + + {question.image ? (
question diff --git a/src/papers/papers.jsx b/src/papers/papers.jsx index 1b37df9..6f8c632 100644 --- a/src/papers/papers.jsx +++ b/src/papers/papers.jsx @@ -8,6 +8,8 @@ import Nothing from "../Assets/nothing.svg"; function Papers() { const navigate = useNavigate(); const [exams, setExams] = useState([]); + const [reload,setReload]= useState(false); + const teacherId = localStorage.getItem("teacherId"); // Assuming teacherId is stored in localStorage useEffect(() => { @@ -24,12 +26,63 @@ function Papers() { }; fetchPapers(); - }, [teacherId]); + }, [teacherId,reload]); const handleCreateNew = () => { navigate("/create-paper"); }; + const handleEditNew=(exam)=> + { + navigate( + "/edit-paper", + { + state: + { + _id:exam._id, + className:exam.className, + semester:exam.semester, + subject:exam.subject, + subjectCode:exam.subjectCode, + date:exam.date, + duration:exam.duration, + testType:exam.testType, + marks:exam.marks, + time:exam.time + } + } + ); + } + + + const deletePaper=async (paper)=> + { + + try + { + await axios.post('http://localhost:5000/paper/delete-paper', { _id: paper._id }) + setExams((prevQuestions) => prevQuestions.filter(q => q._id !== paper._id)); + + if (paper.length === 1) { + setExams([]); + } + + setReload(prev => !prev); + + } + catch(error) + { + console.error('Error creating paper:', error); + } + } + + const duplicatePaper = async (paper)=> + { + await axios.post("http://localhost:5000/paper/duplicate-paper",paper); + setReload(prev => !prev); + } + + const getFormattedDateTime = (date, time) => { const [hours, minutes] = time.split(":").map(Number); const dateTime = new Date(date); @@ -90,6 +143,30 @@ function Papers() { {exam.duration.minutes} mins
Marks : {exam.marks}
+ + + ))} From f43d67f49cc6691729b2568bc4738a734a9ab96a Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Sat, 7 Sep 2024 01:03:48 +0530 Subject: [PATCH 2/5] updated --- src/AlertModal/AlertModal.css | 1 + src/Login/Login.jsx | 2 +- src/papers/papers.jsx | 159 ++++++++++++++++++---------------- src/question/question.jsx | 3 +- 4 files changed, 90 insertions(+), 75 deletions(-) diff --git a/src/AlertModal/AlertModal.css b/src/AlertModal/AlertModal.css index c97d6f6..5c74f6b 100644 --- a/src/AlertModal/AlertModal.css +++ b/src/AlertModal/AlertModal.css @@ -6,6 +6,7 @@ top: 0; left: 0; right: 0; + z-index: 2; bottom: 0; display: flex; backdrop-filter: blur(12px); diff --git a/src/Login/Login.jsx b/src/Login/Login.jsx index 6f24fd7..e46c0af 100644 --- a/src/Login/Login.jsx +++ b/src/Login/Login.jsx @@ -59,7 +59,7 @@ function Login() { const handleLogin = (e) => { e.preventDefault(); - setIsLoading(true); // Start loading + setIsLoading(true); axios .post("http://localhost:5000/teacher/login", { email, password }) diff --git a/src/papers/papers.jsx b/src/papers/papers.jsx index 6f8c632..1219253 100644 --- a/src/papers/papers.jsx +++ b/src/papers/papers.jsx @@ -4,13 +4,18 @@ import "./papers.css"; import { useNavigate } from "react-router-dom"; import axios from "axios"; import Nothing from "../Assets/nothing.svg"; +import AlertModal from "../AlertModal/AlertModal"; + function Papers() { const navigate = useNavigate(); const [exams, setExams] = useState([]); - const [reload,setReload]= useState(false); - - const teacherId = localStorage.getItem("teacherId"); // Assuming teacherId is stored in localStorage + const [reload, setReload] = useState(false); + const [modalIsOpen, setModalIsOpen] = useState(false); + const [modalMessage, setModalMessage] = useState(""); + const [isError, setIsError] = useState(false); + + const teacherId = localStorage.getItem("teacherId"); useEffect(() => { const fetchPapers = async () => { @@ -26,62 +31,72 @@ function Papers() { }; fetchPapers(); - }, [teacherId,reload]); + }, [teacherId, reload]); const handleCreateNew = () => { navigate("/create-paper"); }; - const handleEditNew=(exam)=> - { + const handleEditNew = (exam) => { navigate( "/edit-paper", { - state: - { - _id:exam._id, - className:exam.className, - semester:exam.semester, - subject:exam.subject, - subjectCode:exam.subjectCode, - date:exam.date, - duration:exam.duration, - testType:exam.testType, - marks:exam.marks, - time:exam.time - } + state: { + _id: exam._id, + className: exam.className, + semester: exam.semester, + subject: exam.subject, + subjectCode: exam.subjectCode, + date: exam.date, + duration: exam.duration, + testType: exam.testType, + marks: exam.marks, + time: exam.time, + }, } ); - } - - - const deletePaper=async (paper)=> - { + }; - try - { - await axios.post('http://localhost:5000/paper/delete-paper', { _id: paper._id }) + const deletePaper = async (paper) => { + try { + await axios.post("http://localhost:5000/paper/delete-paper", { _id: paper._id }); setExams((prevQuestions) => prevQuestions.filter(q => q._id !== paper._id)); if (paper.length === 1) { - setExams([]); + setExams([]); } - - setReload(prev => !prev); - + setReload((prev) => !prev); + + // Show success modal + setModalMessage("Paper deleted successfully."); + setIsError(false); + setModalIsOpen(true); + } catch (error) { + console.error("Error deleting paper:", error); + // Show error modal + setModalMessage("Failed to delete paper."); + setIsError(true); + setModalIsOpen(true); } - catch(error) - { - console.error('Error creating paper:', error); - } - } - - const duplicatePaper = async (paper)=> - { - await axios.post("http://localhost:5000/paper/duplicate-paper",paper); - setReload(prev => !prev); - } + }; + const duplicatePaper = async (paper) => { + try { + await axios.post("http://localhost:5000/paper/duplicate-paper", paper); + setReload((prev) => !prev); + + // Show success modal + setModalMessage("Paper duplicated successfully."); + setIsError(false); + setModalIsOpen(true); + } catch (error) { + console.error("Error duplicating paper:", error); + // Show error modal + setModalMessage("Failed to duplicate paper."); + setIsError(true); + setModalIsOpen(true); + } + }; const getFormattedDateTime = (date, time) => { const [hours, minutes] = time.split(":").map(Number); @@ -143,28 +158,22 @@ function Papers() { {exam.duration.minutes} mins
Marks : {exam.marks}
- - - @@ -174,19 +183,23 @@ function Papers() { ) : (
-
- Nothing -

No Paper's Found

- -
-
+
+ Nothing +

No Paper's Found

+ +
+ )} + + setModalIsOpen(false)} + message={modalMessage} + iserror={isError} + /> ); } diff --git a/src/question/question.jsx b/src/question/question.jsx index 0886028..15fde64 100644 --- a/src/question/question.jsx +++ b/src/question/question.jsx @@ -101,7 +101,8 @@ const Question = () => { const { getRootProps, getInputProps } = useDropzone({ onDrop, - maxSize: 10485760, // 10MB limit + maxSize: 10485760, // 10MB limit, + }); return ( From b3abd5fa1883faf9a2abbff8ac32e8f0fd93901e Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Sat, 7 Sep 2024 01:18:22 +0530 Subject: [PATCH 3/5] fixed signup_modal --- src/AlertModal/AlertModal.jsx | 29 +++++++++++++++++++++-------- src/Sign_up/SignUp.jsx | 18 ++++++++++-------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/AlertModal/AlertModal.jsx b/src/AlertModal/AlertModal.jsx index 5f55334..a90ab05 100644 --- a/src/AlertModal/AlertModal.jsx +++ b/src/AlertModal/AlertModal.jsx @@ -7,6 +7,7 @@ // iserror={true} if there is an error then iserror will be true else false // /> + import React from 'react'; import PropTypes from 'prop-types'; import Modal from 'react-modal'; @@ -14,10 +15,16 @@ import './AlertModal.css'; import cross from "../Assets/cross-mark.svg"; import tick from "../Assets/accept-check-good-mark-ok-tick.svg"; +const AlertModal = ({ isOpen, onClose, onConfirm, message, iserror }) => { + var image = iserror ? cross : tick; + const handleClose = () => { + if (onConfirm) { + onConfirm(); // Trigger the callback for any action + } + onClose(); // Always close the modal + }; -const AlertModal = ({ isOpen, onClose, message,iserror }) => { - var image = iserror? cross : tick return ( { alt="Success" className="alert_success-icon" /> -

- {iserror?"failed":"success"} -

-

{message}

{/* Message passed as a prop */} - +

{iserror ? "Failed" : "Success"}

+

{message}

+
); }; + // Define prop types AlertModal.propTypes = { isOpen: PropTypes.bool.isRequired, // isOpen should be a boolean onClose: PropTypes.func.isRequired, // onClose should be a function message: PropTypes.string.isRequired, // message should be a string - iserror: PropTypes.bool.isRequired + iserror: PropTypes.bool.isRequired, // iserror should be a boolean + onConfirm: PropTypes.func // onConfirm is optional and should be a function }; +// Set default prop for optional onConfirm +AlertModal.defaultProps = { + onConfirm: null +}; export default AlertModal; diff --git a/src/Sign_up/SignUp.jsx b/src/Sign_up/SignUp.jsx index 70f9602..e938236 100644 --- a/src/Sign_up/SignUp.jsx +++ b/src/Sign_up/SignUp.jsx @@ -72,9 +72,7 @@ const SignUp = () => { }); }; - const handleSignUp = async (e) => { - e.preventDefault(); - + const handleSignUp = async () => { setLoading(true); // Set loading state try { const response = await fetch("http://localhost:5000/teacher/signup", { @@ -96,7 +94,6 @@ const SignUp = () => { setAlertMessage("Your account has been created successfully."); setIsErrorAlert(false); setIsAlertOpen(true); - setTimeout(() => navigate("/verify_passcode"), 2000); // Redirect after showing success message } else { setAlertMessage(data.error || "Failed to sign up"); setIsErrorAlert(true); @@ -122,18 +119,23 @@ const SignUp = () => { } if (isFirstClick) { - setIsWarningOpen(true); + setIsWarningOpen(true); // Show warning modal setIsFirstClick(false); // Disable modal for subsequent clicks } else { - handleSignUp(e); // Proceed with the signup process directly after the first click + handleSignUp(); // Proceed with the signup process directly after the first click } }; const handleWarningConfirm = () => { - setIsWarningOpen(false); + setIsWarningOpen(false); // Close warning modal handleSignUp(); // Proceed with sign-up after confirming the warning }; + const handleAlertConfirm = () => { + setIsAlertOpen(false); // Close alert modal + navigate("/verify_passcode"); // Redirect to passcode verification after confirmation + }; + return (
@@ -238,7 +240,7 @@ const SignUp = () => { {/* Alert Modal for success/error messages */} setIsAlertOpen(false)} + onClose={handleAlertConfirm} // Trigger redirect and close modal when the alert is confirmed message={alertMessage} iserror={isErrorAlert} /> From c82a06b69fadc30506e711fa66da486b1932b349 Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Sat, 7 Sep 2024 01:19:14 +0530 Subject: [PATCH 4/5] fixed signup_modal --- src/Sign_up/SignUp.jsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Sign_up/SignUp.jsx b/src/Sign_up/SignUp.jsx index e938236..8eab23c 100644 --- a/src/Sign_up/SignUp.jsx +++ b/src/Sign_up/SignUp.jsx @@ -119,21 +119,21 @@ const SignUp = () => { } if (isFirstClick) { - setIsWarningOpen(true); // Show warning modal - setIsFirstClick(false); // Disable modal for subsequent clicks + setIsWarningOpen(true); + setIsFirstClick(false); } else { - handleSignUp(); // Proceed with the signup process directly after the first click + handleSignUp(); } }; const handleWarningConfirm = () => { - setIsWarningOpen(false); // Close warning modal - handleSignUp(); // Proceed with sign-up after confirming the warning + setIsWarningOpen(false); + handleSignUp(); }; const handleAlertConfirm = () => { - setIsAlertOpen(false); // Close alert modal - navigate("/verify_passcode"); // Redirect to passcode verification after confirmation + setIsAlertOpen(false); + navigate("/verify_passcode"); }; return ( @@ -240,7 +240,7 @@ const SignUp = () => { {/* Alert Modal for success/error messages */} From 6f10f9cbfdc1e74ca79df52bbdd45b8eb8c6e3ef Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Sat, 7 Sep 2024 03:44:56 +0530 Subject: [PATCH 5/5] Fixed bugs and ready papers and navbar --- src/App.js | 2 + src/Navbar/Navbar.css | 26 ++++-- src/Navbar/Navbar.jsx | 89 ++++++++++++------ .../QuestionPaperDashboard.jsx | 77 ++++++++++++---- .../ReadyPaperDashboard.jsx | 90 +++++++++++++++++++ src/papers/papers.jsx | 13 +-- src/question/question.jsx | 2 + 7 files changed, 236 insertions(+), 63 deletions(-) create mode 100644 src/ReadyPaperDashboard/ReadyPaperDashboard.jsx diff --git a/src/App.js b/src/App.js index 611da48..c6490f9 100644 --- a/src/App.js +++ b/src/App.js @@ -11,6 +11,7 @@ import axios from 'axios'; import Question from './question/question'; import QuestionPaperDashboard from './QuestionPaperDashboard/QuestionPaperDashboard'; import Editpaper from './Edit_paper/Editpaper'; +import ReadyPaperDashboard from './ReadyPaperDashboard/ReadyPaperDashboard'; const App = () => { const [isAuthenticated, setIsAuthenticated] = useState(false); @@ -66,6 +67,7 @@ const App = () => { } /> } /> }/> + }/> )} diff --git a/src/Navbar/Navbar.css b/src/Navbar/Navbar.css index 7e8943f..7d9a2c8 100644 --- a/src/Navbar/Navbar.css +++ b/src/Navbar/Navbar.css @@ -32,7 +32,7 @@ .navbar-links, .navbar-logout { - width: 100px; + width: 160px; height: 40px; display: flex; align-items: center; @@ -50,21 +50,24 @@ .navbar-links.active { background-color: #4caf50; - border-radius: 10px; - border: 2px solid white; + padding: 0px 0px 0px 5px; color: #000000; } .navbar-logout { + width: 120px; + display: flex; + align-items: center; + justify-content: center; + gap: 5px; margin-top: 10px; - background-color: #282828; + background-color: #1a1a1a; + transition: 0.3s all; } .navbar-logout:hover { - border-radius: 10px; - background-color: red; - border: 2px solid #1a1a1a; - transition: all 1s; + background-color: #181818; + color: red; } .navbar-menu { @@ -109,7 +112,12 @@ .navbar-displayed { display: none; } + +.navbar-sidebar-ul{ + margin: 0px; + padding: 0px 20px; +} .navbar-logout-menu, .navbar-links { background-color: #313030; @@ -118,7 +126,7 @@ border: none; border-radius: 0; border-bottom: 1px solid rgba(255, 255, 255, 0.253); - margin-left: -15px; + } .navbar-logout-menu { diff --git a/src/Navbar/Navbar.jsx b/src/Navbar/Navbar.jsx index 9c1cdc2..df90b41 100644 --- a/src/Navbar/Navbar.jsx +++ b/src/Navbar/Navbar.jsx @@ -1,13 +1,13 @@ import React, { useState } from "react"; import { FaBars, FaTimes } from "react-icons/fa"; -import { useNavigate } from "react-router-dom"; +import { useNavigate, useLocation } from "react-router-dom"; +import { FaPowerOff } from "react-icons/fa"; import "./Navbar.css"; const Navbar = () => { - const [activeLink, setActiveLink] = useState(null); const [open, setOpen] = useState(false); const navigate = useNavigate(); - + const location = useLocation(); const responsive = () => { const sidebar = document.getElementsByClassName("navbar-sidebar")[0]; if (!open) { @@ -19,10 +19,6 @@ const Navbar = () => { } }; - const handleLinkClick = (linkName) => { - setActiveLink(linkName); - }; - const handleLogout = () => { localStorage.removeItem("sessionId"); navigate("/"); @@ -42,30 +38,36 @@ const Navbar = () => {
Nishant Kaushal
+ +

navigate(`/teacherDashboard`)} + > + Dashboard +

{ - handleLinkClick("navbar-papers"); - }} + onClick={() => navigate(`/ready_papers`)} > - Papers + Ready Papers

{ - handleLinkClick("navbar-ready"); - }} + onClick={() => navigate(`/completed_papers`)} > - Ready for Evaluation + Completed Papers

+

- Logout + Logout

@@ -78,19 +80,56 @@ const Navbar = () => {
-
    +
      +
    • +

      { + navigate(`/teacherDashboard`); + responsive(); // close sidebar after click + }} + > + Dashboard +

      +
    • -

      Papers

      +

      { + navigate(`/ready_papers`); + responsive(); // close sidebar after click + }} + > + Ready Papers +

    • -

      Ready for Evaluation

      +

      { + navigate(`/completed_papers`); + responsive(); // close sidebar after click + }} + > + Completed Papers +

    • +
    • { + handleLogout(); + responsive(); // close sidebar after logout + }} > - Logout + Logout

    diff --git a/src/QuestionPaperDashboard/QuestionPaperDashboard.jsx b/src/QuestionPaperDashboard/QuestionPaperDashboard.jsx index f684407..efe9c0c 100644 --- a/src/QuestionPaperDashboard/QuestionPaperDashboard.jsx +++ b/src/QuestionPaperDashboard/QuestionPaperDashboard.jsx @@ -2,7 +2,7 @@ import React, { useState, useEffect } from "react"; import "./QuestionPaperDashboard.css"; import Navbar from "../Navbar/Navbar"; import { FaPlus } from "react-icons/fa"; -import { useNavigate, useParams, useLocation } from "react-router-dom"; +import { useNavigate, useParams } from "react-router-dom"; import axios from "axios"; import defaultImage from "../Assets/no-image-420x370-1.jpg"; import Nothing from "../Assets/nothing.svg"; @@ -10,13 +10,14 @@ import AlertModal from "../AlertModal/AlertModal"; const QuestionPaperDashboard = () => { - const { state } = useLocation(); - const { className, semester, subject, marks } = state || {}; + + const navigate = useNavigate(); const { paperId } = useParams(); const [reload,setReload]= useState(false); const [questions, setQuestions] = useState([]); const [loading, setLoading] = useState(true); + const [paperdetails,setpaperdetails]=useState([]); // State for the modal const [modalIsOpen, setModalIsOpen] = useState(false); @@ -39,7 +40,22 @@ const QuestionPaperDashboard = () => { setLoading(false); } }; - + const fetchpaperdetails = async () => { + try { + const res = await axios.post( + "http://localhost:5000/paper/getPapersdetails", + { paperId } + ); + setpaperdetails(res.data[0]); + } catch (error) { + console.error("Failed to fetch paperdetails:", error); + + + } finally { + setLoading(false); + } + }; + fetchpaperdetails(); fetchQuestions(); }, [paperId,reload]); @@ -80,13 +96,36 @@ const QuestionPaperDashboard = () => { setReload(prev => !prev); } - const handleSubmit = () => { - // Here, you would handle the submit logic. - // Trigger a success modal on successful submission - setModalMessage("Your question paper has been submitted successfully!"); - setIsError(false); - setModalIsOpen(true); - }; + const handleSubmit = async () => { + try { + + const response = await axios.post("http://localhost:5000/paper/submitpaper", { paperId }); + + + if (response.data.success) { + setModalMessage("Your question paper has been submitted successfully!"); + setIsError(false); + setTimeout(() => { + navigate("/ready_papers"); + }, 2000); + } else { + setModalMessage(response.data.message || "Failed to submit the paper."); + setIsError(true); + } + } catch (error) { + const errorMessage = + error.response && error.response.data && error.response.data.message + ? error.response.data.message + : "Failed to submit the paper."; + + setModalMessage(errorMessage); + setIsError(true); + } finally { + // Open the modal regardless of the outcome + setModalIsOpen(true); + } + }; + return ( <> @@ -96,17 +135,17 @@ const QuestionPaperDashboard = () => { <>

    - {className} {semester} ({subject}) + {paperdetails.className} {paperdetails.semester} ({paperdetails.subject})

    - Total Marks:  {totalMarks}/{marks} + Total Marks:  {totalMarks}/{paperdetails.marks}

    - {totalMarks > marks && ( + {totalMarks > paperdetails.marks && (

    The total marks ({totalMarks}) exceed the allowed marks ( - {marks}). Please remove {totalMarks - marks}{" "} + {paperdetails.marks}). Please remove {totalMarks - paperdetails.marks}{" "} marks to submit the paper.

    @@ -143,12 +182,12 @@ const QuestionPaperDashboard = () => { ))}
    - {totalMarks < marks && ( + {totalMarks < paperdetails.marks && ( )} - {totalMarks === marks && ( + {totalMarks === paperdetails.marks && (