From 2678eeeea2118d4eeef95e97360324cf1785961f Mon Sep 17 00:00:00 2001 From: SimpPoseidon <120441879+TanishqMehrunkarIIPSDAVV@users.noreply.github.com> Date: Sat, 5 Oct 2024 18:56:07 +0530 Subject: [PATCH] edit question bug solved --- src/edit_ready_question/EditReadyQuestion.jsx | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/edit_ready_question/EditReadyQuestion.jsx b/src/edit_ready_question/EditReadyQuestion.jsx index 046cf9e..d725ea9 100644 --- a/src/edit_ready_question/EditReadyQuestion.jsx +++ b/src/edit_ready_question/EditReadyQuestion.jsx @@ -74,44 +74,52 @@ const EditReadyQuestion = () => { const handleEditQuestion = async () => { if (!questionheading || !questionDescription || !compilerReq || !marks) { - setModalMessage('Please fill in all the required fields.'); + setModalMessage("Please fill in all the required fields."); setIsError(true); setModalIsOpen(true); return; } - + if (parseInt(marks) > remainingMarks) { - setModalMessage(`You can assign a maximum of ${remainingMarks} marks to this question.`); + setModalMessage( + `You can assign a maximum of ${remainingMarks} marks to this question.` + ); setIsError(true); setModalIsOpen(true); return; } - + setLoading(true); try { - let imageUrl = ''; - - if (image) { + let imageUrl = image; + + // Only upload if the image is a new file (not a pre-existing URL) + if (image && typeof image !== "string") { const formData = new FormData(); - formData.append('file', image); - formData.append('upload_preset', 'question'); - - const uploadResponse = await axios.post('http://localhost:5000/paper/upload', formData, { - headers: { 'Content-Type': 'multipart/form-data' }, - }); - - imageUrl = uploadResponse.data.url; // Assuming the backend responds with the image URL + formData.append("file", image); + formData.append("upload_preset", "question"); + + const uploadResponse = await axios.post( + "http://localhost:5000/paper/upload", + formData, + { + headers: { "Content-Type": "multipart/form-data" }, + } + ); + + imageUrl = uploadResponse.data.url; } - + await editQuestion(imageUrl); } catch (error) { - console.error('Failed to add question:', error.message); - setModalMessage('Failed to add question. Please try again.'); + console.error("Failed to edit question:", error.message); + setModalMessage("Failed to edit question. Please try again."); setIsError(true); setModalIsOpen(true); setLoading(false); } }; + const editQuestion = async (imageUrl) => { const response = await axios.post('http://localhost:5000/paper/edit-ready-question', {