import { useState } from "react" import { useNavigate, useParams } from "react-router-dom" import { ArrowLeft, Plus, X } from "lucide-react" import { toast } from "sonner" import { Button } from "../../components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "../../components/ui/card" import { Input } from "../../components/ui/input" import { Textarea } from "../../components/ui/textarea" import { Select } from "../../components/ui/select" type QuestionType = "multiple-choice" | "short-answer" | "true-false" interface Question { id: string question: string type: QuestionType options: string[] correctAnswer: string points: number category?: string difficulty?: string } // Mock data for editing const mockQuestion: Question = { id: "1", question: "", type: "multiple-choice", options: ["", "", "", ""], correctAnswer: "", points: 10, category: "", difficulty: "", } export function AddQuestionPage() { const navigate = useNavigate() const { id } = useParams<{ id?: string }>() const isEditing = !!id const [formData, setFormData] = useState( isEditing ? mockQuestion // In a real app, fetch the question by id : { id: Date.now().toString(), question: "", type: "multiple-choice", options: ["", "", "", ""], correctAnswer: "", points: 10, category: "", difficulty: "", }, ) const handleTypeChange = (type: QuestionType) => { setFormData((prev) => { if (type === "true-false") { return { ...prev, type, options: ["True", "False"], correctAnswer: prev.correctAnswer === "True" || prev.correctAnswer === "False" ? prev.correctAnswer : "", } } else if (type === "short-answer") { return { ...prev, type, options: [], correctAnswer: "", } } else { return { ...prev, type, options: prev.options.length > 0 ? prev.options : ["", "", "", ""], } } }) } const handleOptionChange = (index: number, value: string) => { setFormData((prev) => { const newOptions = [...prev.options] newOptions[index] = value return { ...prev, options: newOptions } }) } const addOption = () => { setFormData((prev) => ({ ...prev, options: [...prev.options, ""], })) } const removeOption = (index: number) => { setFormData((prev) => ({ ...prev, options: prev.options.filter((_, i) => i !== index), })) } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() // Validation if (!formData.question.trim()) { toast.error("Missing question", { description: "Please enter a question before saving.", }) return } if (formData.type === "multiple-choice" || formData.type === "true-false") { if (!formData.correctAnswer) { toast.error("Missing correct answer", { description: "Select the correct answer for this question.", }) return } if (formData.type === "multiple-choice") { const hasEmptyOptions = formData.options.some((opt) => !opt.trim()) if (hasEmptyOptions) { toast.error("Incomplete options", { description: "Fill in all answer options for this multiple choice question.", }) return } } } else if (formData.type === "short-answer") { if (!formData.correctAnswer.trim()) { toast.error("Missing correct answer", { description: "Enter the expected correct answer.", }) return } } // In a real app, save the question here console.log("Saving question:", formData) toast.success(isEditing ? "Question updated" : "Question created", { description: isEditing ? "The question has been updated successfully." : "Your new question has been created.", }) navigate("/content/questions") } return (
{/* Page Header */}

{isEditing ? "Edit Question" : "Add New Question"}

{isEditing ? "Update the question details below" : "Fill in the details to create a new question"}

Question Details {/* Question Type */}

{/* Question Text */}