|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Question } from './questiontable'; // Adjust the import based on your file structure |
| 3 | + |
| 4 | +interface EditQuestionFormProps { |
| 5 | + question: Question; |
| 6 | + onUpdate: (updatedQuestion: Question) => void; |
| 7 | + onClose: () => void; |
| 8 | +} |
| 9 | + |
| 10 | +const EditQuestionForm: React.FC<EditQuestionFormProps> = ({ question, onUpdate, onClose }) => { |
| 11 | + const [title, setTitle] = useState(question.title); |
| 12 | + const [description, setDescription] = useState(question.description); |
| 13 | + const [categories, setCategories] = useState(question.categories); |
| 14 | + const [complexity, setComplexity] = useState(question.complexity); |
| 15 | + const [link, setLink] = useState(question.link); |
| 16 | + |
| 17 | + const handleSubmit = (e: React.FormEvent) => { |
| 18 | + e.preventDefault(); |
| 19 | + const updatedQuestion = { |
| 20 | + ...question, |
| 21 | + title, |
| 22 | + description, |
| 23 | + categories, |
| 24 | + complexity, |
| 25 | + link, |
| 26 | + }; |
| 27 | + onUpdate(updatedQuestion); |
| 28 | + onClose(); |
| 29 | + }; |
| 30 | + |
| 31 | + return ( |
| 32 | + <div className="fixed top-0 left-0 right-0 bottom-0 flex items-center justify-center bg-black bg-opacity-50"> |
| 33 | + <form className="bg-white text-black p-5 rounded flex flex-col space-y-4" onSubmit={handleSubmit}> |
| 34 | + <h1 className="text-xl font-bold">Edit Question</h1> |
| 35 | + <label className="flex flex-row gap-4"> |
| 36 | + <span className="text-lg font-semibold">Title:</span> |
| 37 | + <input type="text" className="rounded border border-gray-300 p-2" value={title} onChange={(e) => setTitle(e.target.value)} required /> |
| 38 | + </label> |
| 39 | + <label className="flex flex-row gap-4"> |
| 40 | + <span className="text-lg font-semibold">Description:</span> |
| 41 | + <textarea value={description} className="rounded border border-gray-300 p-2" onChange={(e) => setDescription(e.target.value)} required /> |
| 42 | + </label> |
| 43 | + <label className="flex flex-row gap-4"> |
| 44 | + <span className="text-lg font-semibold">Categories:</span> |
| 45 | + <input type="text" className="rounded border border-gray-300 p-2" value={categories} onChange={(e) => setCategories(e.target.value)} required /> |
| 46 | + </label> |
| 47 | + <label className="flex flex-row gap-4"> |
| 48 | + <span className="text-lg font-semibold">Complexity:</span> |
| 49 | + <input type="text" className="rounded border border-gray-300 p-2" value={complexity} onChange={(e) => setComplexity(e.target.value)} required /> |
| 50 | + </label> |
| 51 | + <label className="flex flex-row gap-4"> |
| 52 | + <span className="text-lg font-semibold">Link:</span> |
| 53 | + <input type="url" className="rounded border border-gray-300 p-2" value={link} onChange={(e) => setLink(e.target.value)} required /> |
| 54 | + </label> |
| 55 | + <button type="submit" className="bg-blue-500 text-white rounded px-2 py-1">Update</button> |
| 56 | + <button type="button" onClick={onClose} className="bg-blue-500 text-white rounded px-2 py-1">Cancel</button> |
| 57 | + </form> |
| 58 | + </div> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +export default EditQuestionForm; |
0 commit comments