import React, { useState } from "react"; const ExamBuilder = () => { const [paper, setPaper] = useState({ id: "", title: "", data: { metadata: { type: "single", marking: "", duration: 0, quantity: 0, }, questions: [ { id: 1, type: "single", options: { a: "", b: "", c: "", d: "", }, question: "", solution: "", correctAnswer: "", }, ], }, }); const handleChange = (e) => { const { name, value } = e.target; setPaper((prev) => ({ ...prev, [name]: value, })); }; const handleMetadataChange = (e) => { const { name, value } = e.target; setPaper((prev) => ({ ...prev, data: { ...prev.data, metadata: { ...prev.data.metadata, [name]: value, }, }, })); }; const handleQuestionChange = (index, field, value) => { const updatedQuestions = paper.data.questions.map((q, i) => i === index ? { ...q, [field]: value } : q ); setPaper((prev) => ({ ...prev, data: { ...prev.data, questions: updatedQuestions }, })); }; const handleOptionChange = (index, optionKey, value) => { const updatedQuestions = paper.data.questions.map((q, i) => i === index ? { ...q, options: { ...q.options, [optionKey]: value } } : q ); setPaper((prev) => ({ ...prev, data: { ...prev.data, questions: updatedQuestions }, })); }; const addQuestion = () => { setPaper((prev) => ({ ...prev, data: { ...prev.data, questions: [ ...prev.data.questions, { id: prev.data.questions.length + 1, type: "single", options: { a: "", b: "", c: "", d: "" }, question: "", solution: "", correctAnswer: "", }, ], }, })); }; const handleSubmit = (e) => { e.preventDefault(); console.log("Exam Paper Submitted: ", paper); }; return (

Exam Builder

General Information

Metadata

Questions

{paper.data.questions.map((question, index) => (

Question {index + 1}

handleQuestionChange(index, "question", e.target.value) } placeholder="Question" className="bg-white w-full rounded-full py-3 px-6 mb-2" />
{Object.keys(question.options).map((key) => (

{key}

handleOptionChange(index, key, e.target.value) } placeholder={`Option ${key.toUpperCase()}`} className="bg-white w-full rounded-full py-3 px-6" />
))}
handleQuestionChange(index, "correctAnswer", e.target.value) } placeholder="Correct answer (a, b, c, d)" className="bg-white w-full rounded-full py-3 px-6 mt-2" /> handleQuestionChange(index, "solution", e.target.value) } placeholder="Solution" className="bg-white w-full rounded-full py-3 px-6 mt-2" />
))}
); }; export default ExamBuilder; { /* */ }