Skip to content

Commit 23ed5f4

Browse files
committed
Add Edit Feature
1 parent ff7a75d commit 23ed5f4

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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;

frontend/src/components/questiontable.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
2+
import EditQuestionForm from './editquestionform';
23

34
export interface Question {
45
id: number;
@@ -18,6 +19,8 @@ interface QuestionTableProps {
1819
}
1920

2021
const QuestionTable: React.FC<QuestionTableProps> = ({ searchTerm, category, complexity, questions, setQuestions }) => {
22+
23+
const [editingQuestion, setEditingQuestion] = useState<Question | null>(null);
2124

2225
const filteredQuestions = questions.filter((question) => {
2326
const matchesSearchTerm = question.title.toLowerCase().includes(searchTerm.toLowerCase());
@@ -30,7 +33,18 @@ const QuestionTable: React.FC<QuestionTableProps> = ({ searchTerm, category, com
3033
setQuestions((prevQuestions) => prevQuestions.filter(question => question.id !== id));
3134
};
3235

36+
const handleUpdate = (updatedQuestion: Question) => {
37+
setQuestions((prevQuestions) =>
38+
prevQuestions.map(question => (question.id === updatedQuestion.id ? updatedQuestion : question))
39+
);
40+
};
41+
42+
const closeEditForm = () => {
43+
setEditingQuestion(null);
44+
};
45+
3346
return (
47+
<div>
3448
<table className="min-w-full table-auto">
3549
<thead>
3650
<tr>
@@ -57,9 +71,14 @@ const QuestionTable: React.FC<QuestionTableProps> = ({ searchTerm, category, com
5771
</a>
5872
</td>
5973
<td className="border px-4 py-2">
74+
<div className="flex space-x-2">
6075
<button onClick={() => handleDelete(question.id)} className="bg-red-500 text-white rounded px-2 py-1">
6176
Delete
6277
</button>
78+
<button onClick={() => setEditingQuestion(question)} className="bg-red-500 text-white rounded px-2 py-1">
79+
Edit
80+
</button>
81+
</div>
6382
</td>
6483
</tr>
6584
))}
@@ -72,6 +91,14 @@ const QuestionTable: React.FC<QuestionTableProps> = ({ searchTerm, category, com
7291
)}
7392
</tbody>
7493
</table>
94+
{editingQuestion && (
95+
<EditQuestionForm
96+
question={editingQuestion}
97+
onUpdate={handleUpdate}
98+
onClose={closeEditForm}
99+
/>
100+
)}
101+
</div>
75102
);
76103
};
77104

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)