|
1 |
| -import React, { useState } from 'react'; |
2 |
| -import EditQuestionForm from './editquestionform'; |
| 1 | +import axios from "axios"; |
| 2 | +import React, { useState } from "react"; |
| 3 | +import EditQuestionForm from "./editquestionform"; |
3 | 4 |
|
4 | 5 | export interface Question {
|
5 |
| - id: number; |
6 |
| - title: string; |
7 |
| - description: string; |
8 |
| - categories: string; |
9 |
| - complexity: string; |
10 |
| - link: string; |
| 6 | + id: string; |
| 7 | + title: string; |
| 8 | + description: string; |
| 9 | + categories: string; |
| 10 | + complexity: string; |
| 11 | + link: string; |
11 | 12 | }
|
12 | 13 |
|
13 | 14 | interface QuestionTableProps {
|
14 |
| - searchTerm: string; |
15 |
| - category: string; |
16 |
| - complexity: string; |
17 |
| - questions: Question[]; |
18 |
| - setQuestions: React.Dispatch<React.SetStateAction<Question[]>>; |
| 15 | + searchTerm: string; |
| 16 | + category: string; |
| 17 | + complexity: string; |
| 18 | + questions: Question[]; |
| 19 | + setQuestions: React.Dispatch<React.SetStateAction<Question[]>>; |
19 | 20 | }
|
20 | 21 |
|
21 |
| -const QuestionTable: React.FC<QuestionTableProps> = ({ searchTerm, category, complexity, questions, setQuestions }) => { |
| 22 | +const QuestionTable: React.FC<QuestionTableProps> = ({ |
| 23 | + searchTerm, |
| 24 | + category, |
| 25 | + complexity, |
| 26 | + questions, |
| 27 | + setQuestions, |
| 28 | +}) => { |
| 29 | + const [editingQuestion, setEditingQuestion] = useState<Question | null>(null); |
22 | 30 |
|
23 |
| - const [editingQuestion, setEditingQuestion] = useState<Question | null>(null); |
24 |
| - |
25 |
| - const filteredQuestions = questions.filter((question) => { |
26 |
| - const matchesSearchTerm = question.title.toLowerCase().includes(searchTerm.toLowerCase()); |
27 |
| - const matchesCategory = category ? question.categories.includes(category) : true; |
28 |
| - const matchesComplexity = complexity ? question.complexity === complexity : true; |
29 |
| - return matchesSearchTerm && matchesCategory && matchesComplexity; |
30 |
| - }); |
| 31 | + const filteredQuestions = questions.filter((question) => { |
| 32 | + const matchesSearchTerm = question.title |
| 33 | + .toLowerCase() |
| 34 | + .includes(searchTerm.toLowerCase()); |
| 35 | + const matchesCategory = category |
| 36 | + ? question.categories.includes(category) |
| 37 | + : true; |
| 38 | + const matchesComplexity = complexity |
| 39 | + ? question.complexity === complexity |
| 40 | + : true; |
| 41 | + return matchesSearchTerm && matchesCategory && matchesComplexity; |
| 42 | + }); |
31 | 43 |
|
32 |
| - const handleDelete = (id: number) => { |
33 |
| - setQuestions((prevQuestions) => prevQuestions.filter(question => question.id !== id)); |
34 |
| - }; |
| 44 | + const handleDelete = async (id: string) => { |
| 45 | + try { |
| 46 | + console.log("id is ", id); |
| 47 | + await axios.delete(`http://localhost:3002/questions/${id}`); |
| 48 | + setQuestions((prevQuestions) => |
| 49 | + prevQuestions.filter((question) => question.id !== id) |
| 50 | + ); |
| 51 | + } catch (error) { |
| 52 | + alert("Error deleting question: " + error); |
| 53 | + } |
| 54 | + setQuestions((prevQuestions) => |
| 55 | + prevQuestions.filter((question) => question.id !== id) |
| 56 | + ); |
| 57 | + }; |
35 | 58 |
|
36 |
| - const handleUpdate = (updatedQuestion: Question) => { |
37 |
| - setQuestions((prevQuestions) => |
38 |
| - prevQuestions.map(question => (question.id === updatedQuestion.id ? updatedQuestion : question)) |
39 |
| - ); |
40 |
| - }; |
| 59 | + const handleUpdate = async (updatedQuestion: Question) => { |
| 60 | + try { |
| 61 | + console.log("updated question is ", updatedQuestion) |
| 62 | + const response = await axios.patch( |
| 63 | + `http://localhost:3002/questions/${updatedQuestion.id}`, |
| 64 | + updatedQuestion |
| 65 | + ); |
| 66 | + setQuestions((prevQuestions) => |
| 67 | + prevQuestions.map((question) => |
| 68 | + question.id === updatedQuestion.id ? response.data : question |
| 69 | + ) |
| 70 | + ); |
| 71 | + } catch (error) { |
| 72 | + alert("Error updating question: " + error); |
| 73 | + } |
| 74 | + }; |
41 | 75 |
|
42 |
| - const closeEditForm = () => { |
43 |
| - setEditingQuestion(null); |
44 |
| - }; |
| 76 | + const closeEditForm = () => { |
| 77 | + setEditingQuestion(null); |
| 78 | + }; |
45 | 79 |
|
46 |
| - return ( |
47 |
| - <div> |
48 |
| - <table className="min-w-full table-auto"> |
49 |
| - <thead> |
50 |
| - <tr> |
51 |
| - <th className="border px-4 py-2">ID</th> |
52 |
| - <th className="border px-4 py-2">Title</th> |
53 |
| - <th className="border px-4 py-2">Description</th> |
54 |
| - <th className="border px-4 py-2">Categories</th> |
55 |
| - <th className="border px-4 py-2">Complexity</th> |
56 |
| - <th className="border px-4 py-2">Link</th> |
57 |
| - <th className="border px-4 py-2">Actions</th> |
58 |
| - </tr> |
59 |
| - </thead> |
60 |
| - <tbody> |
61 |
| - {filteredQuestions.map((question) => ( |
62 |
| - <tr key={question.id}> |
63 |
| - <td className="border px-4 py-2">{question.id}</td> |
64 |
| - <td className="border px-4 py-2">{question.title}</td> |
65 |
| - <td className="border px-4 py-2">{question.description}</td> |
66 |
| - <td className="border px-4 py-2">{question.categories}</td> |
67 |
| - <td className="border px-4 py-2">{question.complexity}</td> |
68 |
| - <td className="border px-4 py-2"> |
69 |
| - <a href={question.link} target="_blank" rel="noopener noreferrer" className="text-blue-500"> |
70 |
| - View |
71 |
| - </a> |
72 |
| - </td> |
73 |
| - <td className="border px-4 py-2"> |
74 |
| - <div className="flex space-x-2"> |
75 |
| - <button onClick={() => handleDelete(question.id)} className="bg-red-500 text-white rounded px-2 py-1"> |
76 |
| - Delete |
77 |
| - </button> |
78 |
| - <button onClick={() => setEditingQuestion(question)} className="bg-red-500 text-white rounded px-2 py-1"> |
79 |
| - Edit |
80 |
| - </button> |
81 |
| - </div> |
82 |
| - </td> |
83 |
| - </tr> |
84 |
| - ))} |
85 |
| - {filteredQuestions.length === 0 && ( |
86 |
| - <tr> |
87 |
| - <td colSpan={7} className="border border-gray-300 px-4 py-2 text-center"> |
88 |
| - No questions found. |
89 |
| - </td> |
90 |
| - </tr> |
91 |
| - )} |
92 |
| - </tbody> |
93 |
| - </table> |
94 |
| - {editingQuestion && ( |
95 |
| - <EditQuestionForm |
96 |
| - question={editingQuestion} |
97 |
| - onUpdate={handleUpdate} |
98 |
| - onClose={closeEditForm} |
99 |
| - /> |
100 |
| - )} |
| 80 | + return ( |
| 81 | + <div className="bg-gray-800 text-white"> |
| 82 | + <table className="min-w-full table-auto"> |
| 83 | + <thead className="bg-gray-700"> |
| 84 | + <tr> |
| 85 | + <th className="border px-4 py-2">ID</th> |
| 86 | + <th className="border px-4 py-2">Title</th> |
| 87 | + <th className="border px-4 py-2">Description</th> |
| 88 | + <th className="border px-4 py-2">Categories</th> |
| 89 | + <th className="border px-4 py-2">Complexity</th> |
| 90 | + <th className="border px-4 py-2">Link</th> |
| 91 | + <th className="border px-4 py-2">Actions</th> |
| 92 | + </tr> |
| 93 | + </thead> |
| 94 | + <tbody className="bg-gray-800"> |
| 95 | + {filteredQuestions.map((question) => ( |
| 96 | + <tr key={question.id}> |
| 97 | + <td className="border px-4 py-2">{question.id}</td> |
| 98 | + <td className="border px-4 py-2">{question.title}</td> |
| 99 | + <td className="border px-4 py-2">{question.description}</td> |
| 100 | + <td className="border px-4 py-2">{question.categories}</td> |
| 101 | + <td className="border px-4 py-2">{question.complexity}</td> |
| 102 | + <td className="border px-4 py-2"> |
| 103 | + <a |
| 104 | + href={question.link} |
| 105 | + target="_blank" |
| 106 | + rel="noopener noreferrer" |
| 107 | + className="text-blue-500" |
| 108 | + > |
| 109 | + View |
| 110 | + </a> |
| 111 | + </td> |
| 112 | + <td className="border px-4 py-2"> |
| 113 | + <div className="flex space-x-2"> |
| 114 | + <button |
| 115 | + onClick={() => handleDelete(question.id)} |
| 116 | + className="bg-red-500 text-white rounded px-2 py-1" |
| 117 | + > |
| 118 | + Delete |
| 119 | + </button> |
| 120 | + <button |
| 121 | + onClick={() => setEditingQuestion(question)} |
| 122 | + className="bg-red-500 text-white rounded px-2 py-1" |
| 123 | + > |
| 124 | + Edit |
| 125 | + </button> |
| 126 | + </div> |
| 127 | + </td> |
| 128 | + </tr> |
| 129 | + ))} |
| 130 | + {filteredQuestions.length === 0 && ( |
| 131 | + <tr> |
| 132 | + <td |
| 133 | + colSpan={7} |
| 134 | + className="border border-gray-300 px-4 py-2 text-center" |
| 135 | + > |
| 136 | + No questions found. |
| 137 | + </td> |
| 138 | + </tr> |
| 139 | + )} |
| 140 | + </tbody> |
| 141 | + </table> |
| 142 | + {editingQuestion && ( |
| 143 | + <EditQuestionForm |
| 144 | + question={editingQuestion} |
| 145 | + onUpdate={handleUpdate} |
| 146 | + onClose={closeEditForm} |
| 147 | + /> |
| 148 | + )} |
101 | 149 | </div>
|
102 |
| - ); |
| 150 | + ); |
103 | 151 | };
|
104 | 152 |
|
105 | 153 | export default QuestionTable;
|
0 commit comments