Skip to content

Commit

Permalink
Merge pull request #47 from TanishqMehrunkarIIPSDAVV/edit-question-bug
Browse files Browse the repository at this point in the history
edit question bug solved
  • Loading branch information
nishant0708 authored Oct 7, 2024
2 parents 654b156 + 2678eee commit 5c08ea3
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/edit_ready_question/EditReadyQuestion.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down

0 comments on commit 5c08ea3

Please sign in to comment.