diff --git a/src/AddQuestion/AddQuestion.jsx b/src/AddQuestion/AddQuestion.jsx index 4658f5e..aec555b 100644 --- a/src/AddQuestion/AddQuestion.jsx +++ b/src/AddQuestion/AddQuestion.jsx @@ -1,13 +1,14 @@ import { useDropzone } from 'react-dropzone'; -import { FaTimes } from 'react-icons/fa'; +import { FaArrowLeft, FaTimes } from 'react-icons/fa'; import { IoCloudUploadOutline } from 'react-icons/io5'; import './AddQuestion.css'; import { useEffect, useState } from 'react'; import axios from 'axios'; import AlertModal from '../AlertModel/AlertModel'; import { useNavigate } from 'react-router-dom'; +import PropTypes from 'prop-types'; -const AddQuestion = () => { +const AddQuestion = ({ questionData, onBack }) => { const navigate = useNavigate(); const [isFlex, setIsFlex] = useState(true); const [tags, setTags] = useState([]); @@ -31,12 +32,30 @@ const AddQuestion = () => { const [divTag, setDivTag] = useState([]); const [images, setImages] = useState([]); + useEffect(() => { + if (questionData) { + // Populate form with existing data + setHeading(questionData.heading || ''); + setDescription(questionData.description || ''); + setDifficultyLevel(questionData.difficultyLevel || ''); + setMarks(questionData.marks || ''); + setOption(questionData.option || ''); + setMcqOptions(questionData.mcqOptions || ['']); + setMcqAnswer(questionData.mcqAnswer || ''); + setHours(questionData.expectedTime?.hours || ''); + setMinutes(questionData.expectedTime?.minutes || ''); + setTags(questionData.tags || []); + setDivTag(questionData.tags || []); + } + }, [questionData]); + useEffect(() => { document.title = 'Add Questions | EduWiz'; }, []); useEffect(() => { setExpectedTime({ hours, minutes }); + // console.log(expectedTime); }, [hours, minutes]); const onDrop = (acceptedFiles) => { @@ -130,8 +149,15 @@ const AddQuestion = () => { } setLoading(true); const teacherId = localStorage.getItem('teacherId'); + try { - const response = await axios.post('http://localhost:5000/question/addQuestion', { + const endpoint = questionData + ? `http://localhost:5000/question/updateQuestion/${questionData._id}` + : 'http://localhost:5000/question/addQuestion'; + + const method = questionData ? 'put' : 'post'; + + const response = await axios[method](endpoint, { teacherId, heading, description, @@ -143,10 +169,11 @@ const AddQuestion = () => { expectedTime, divTag, }); + if (response.status === 200) { setModalIsError(false); setModalIsOpen(true); - setModalMessage(response.data.message); + setModalMessage(questionData ? 'Question updated successfully!' : 'Question added successfully!'); } } catch (error) { setModalIsError(true); @@ -178,9 +205,15 @@ const AddQuestion = () => { return (
+
-
Question Upload
+
{questionData ? 'Edit Question' : 'Add Question'}
{ @@ -194,9 +227,8 @@ const AddQuestion = () => { type="text" placeholder="Enter Question Heading:" required - onChange={(e) => { - setHeading(e.target.value); - }} + value={heading} + onChange={(e) => setHeading(e.target.value)} />
@@ -206,6 +238,7 @@ const AddQuestion = () => {