File size: 7,367 Bytes
733e040 f720420 733e040 8cd242f 733e040 8cd242f 733e040 8cd242f 733e040 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | 'use client';
import { useState, useEffect } from 'react';
import { GeneratedQuestion, QuestionType } from '@/types/quiz';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
export interface QuestionEditModalProps {
question: GeneratedQuestion | null;
questionTypes: QuestionType[];
isOpen: boolean;
onClose: () => void;
onSave: (updatedQuestion: GeneratedQuestion) => void;
}
export default function QuestionEditModal({
question,
questionTypes,
isOpen,
onClose,
onSave,
}: QuestionEditModalProps) {
const [editedQuestion, setEditedQuestion] = useState<GeneratedQuestion | null>(null);
const [isSaving, setIsSaving] = useState(false);
// Initialize form when question changes
useEffect(() => {
if (question) {
setEditedQuestion({ ...question });
}
}, [question]);
const handleSave = async () => {
if (!editedQuestion) return;
// Basic validation
if (!editedQuestion.stem.trim()) {
alert('Please enter a question');
return;
}
if (!editedQuestion.content.Options.A.trim() ||
!editedQuestion.content.Options.B.trim() ||
!editedQuestion.content.Options.C.trim() ||
!editedQuestion.content.Options.D.trim()) {
alert('Please fill in all answer options');
return;
}
setIsSaving(true);
try {
// Call the update API
const response = await fetch('/api/update-question', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(editedQuestion),
});
if (response.ok) {
const updatedQuestion = await response.json();
onSave(updatedQuestion);
onClose();
} else {
const errorData = await response.json();
alert(`Failed to update question: ${errorData.error || 'Unknown error'}`);
}
} catch (error) {
console.error('Error updating question:', error);
alert('Failed to update question. Please try again.');
} finally {
setIsSaving(false);
}
};
const handleCancel = () => {
setEditedQuestion(question ? { ...question } : null);
onClose();
};
const updateQuestionField = (field: string, value: string | number | boolean) => {
if (!editedQuestion) return;
setEditedQuestion(prev => {
if (!prev) return null;
if (field === 'stem') {
return { ...prev, stem: String(value) };
} else if (field.startsWith('content.Options.')) {
const optionKey = field.replace('content.Options.', '');
return {
...prev,
content: {
...prev.content,
Options: {
...prev.content.Options,
[optionKey]: String(value)
}
}
};
} else if (field.startsWith('content.')) {
const contentField = field.replace('content.', '');
return {
...prev,
content: {
...prev.content,
[contentField]: String(value)
}
};
}
return prev;
});
};
if (!editedQuestion) return null;
return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent className="max-w-4xl max-h-[90vh] overflow-y-auto">
<DialogHeader>
<DialogTitle>Edit Question</DialogTitle>
</DialogHeader>
<div className="space-y-6 py-4">
{/* Question Type - Read Only */}
<div className="space-y-2">
<Label htmlFor="question-type">Question Type</Label>
<div className="flex items-center space-x-2 p-3 bg-gray-50 dark:bg-gray-800 rounded-md border w-full">
<span className="text-lg flex-shrink-0">
{questionTypes.find(t => t.id === editedQuestion.type)?.icon || '📝'}
</span>
<span className="text-sm font-medium break-words">
{questionTypes.find(t => t.id === editedQuestion.type)?.name || 'Unknown Type'}
</span>
</div>
</div>
{/* Points - Read Only */}
<div className="space-y-2">
<Label htmlFor="question-points">Points</Label>
<div className="flex items-center space-x-2 p-3 bg-gray-50 dark:bg-gray-800 rounded-md border w-20">
<span className="text-sm font-medium">{editedQuestion.points} pts</span>
</div>
</div>
{/* Question Stem */}
<div className="space-y-2">
<Label htmlFor="question-stem">Question</Label>
<Textarea
id="question-stem"
value={editedQuestion.stem}
onChange={(e) => updateQuestionField('stem', e.target.value)}
placeholder="Enter your question here..."
className="min-h-[120px] w-full resize-y"
rows={4}
/>
</div>
{/* Options */}
<div className="space-y-4">
<Label>Options</Label>
<div className="grid grid-cols-1 gap-4">
{Object.entries(editedQuestion.content.Options).map(([key, value]) => (
<div key={key} className="flex items-start space-x-3">
<Label className="w-8 text-sm font-medium mt-2">{key})</Label>
<Textarea
value={value}
onChange={(e) => updateQuestionField(`content.Options.${key}`, e.target.value)}
placeholder={`Option ${key}`}
className="flex-1 min-h-[40px] resize-y"
rows={2}
/>
</div>
))}
</div>
</div>
{/* Correct Answer */}
<div className="space-y-2">
<Label htmlFor="correct-answer">Correct Answer</Label>
<Select
value={editedQuestion.content.Answer}
onValueChange={(value) => updateQuestionField('content.Answer', value)}
>
<SelectTrigger className="w-full">
<SelectValue placeholder="Select correct answer" />
</SelectTrigger>
<SelectContent className="max-w-full">
{Object.keys(editedQuestion.content.Options).map((key) => (
<SelectItem key={key} value={key} className="whitespace-normal">
<span className="font-medium">{key})</span> {editedQuestion.content.Options[key as keyof typeof editedQuestion.content.Options]}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
<DialogFooter>
<Button
variant="outline"
onClick={handleCancel}
disabled={isSaving}
>
Cancel
</Button>
<Button
onClick={handleSave}
disabled={isSaving}
>
{isSaving ? 'Saving...' : 'Save Changes'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
|