-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoctor.js
125 lines (118 loc) · 3.73 KB
/
Doctor.js
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
import supabase from '../config/supabaseClient';
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { useParams, useNavigate } from "react-router-dom";
import CreateDoctor from "./CreateDoctor";
import { Routes, Route } from "react-router-dom";
const Doctor = () => {
const { ID } = useParams();
const navigate = useNavigate();
const [fetchError, setFetchError] = useState(null);
const [Doctor, setDoctor] = useState(null);
const [searchTerm, setSearchTerm] = useState("");
const [filteredDoctor, setFilteredDoctor] = useState(null);
const fetchDoctors = async () => {
const { data, error } = await supabase
.from('Doctor')
.select();
if (error) {
setDoctor(null);
setFetchError("Couldn't fetch the Doctors data");
console.log(error);
}
if (data) {
setDoctor(data);
setFetchError(null);
}
};
useEffect(() => {
fetchDoctors()
}, [])
const handleDelete = async (doctorID) => {
const confirmed = window.confirm("Are you sure you want to delete?");
if (confirmed) {
await supabase.from('Doctor').delete().eq('ID', doctorID);
fetchDoctors(); // Fetch the updated supplier data after deletion
}
};
useEffect(() => {
if (Doctor) {
const filteredData = Doctor.filter((op) =>
`${op.FirstName} ${op.LastName}`.toLowerCase().includes(searchTerm.toLowerCase())
);
setFilteredDoctor(filteredData);
}
}, [searchTerm, Doctor]);
const handleCreateDoctor = () => {
navigate("/createDoctor");
};
return (
<div className="page home">
<div className="createDoctor">
<h1 className="tablesubj">Doctors Info</h1>
<button className="createButton" onClick={handleCreateDoctor}>
Create New
</button>
<label id="searchLabel">
<input
style={{textAlign: "center"}}
type="text"
placeholder="Search by Name"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
</label>
<Routes>
<Route path="/CreateDoctor" element={<CreateDoctor />} />
</Routes>
</div>
{fetchError && (<p>{fetchError}</p>)}
{filteredDoctor && Doctor && (
<table className="table">
<thead>
<tr>
<th >Doctor ID</th>
<th >First Name</th>
<th >Last Name</th>
<th >Gender</th>
<th >Birth Date</th>
<th >Specialty</th>
<th >Schedule</th>
<th >Email</th>
<th >Phone Number</th>
<th >Edit</th>
<th >Delete</th>
</tr>
</thead>
<tbody>
{filteredDoctor.map(doctor => (
<tr key={doctor.ID}>
<td >{doctor.ID}</td>
<td >{doctor.FirstName}</td>
<td >{doctor.LastName}</td>
<td >{doctor.Gender}</td>
<td >{doctor.BirthDate}</td>
<td >{doctor.Speciality}</td>
<td >{doctor.Schedule}</td>
<td >{doctor.Email}</td>
<td >{'0'+doctor.PhoneNumber}</td>
<td className="buttons">
<Link to={'/doctor' + doctor.ID}>
<i className="material-icons">edit</i>
</Link>
</td>
<td className="buttons">
<i
className="material-icons"style={{cursor: "pointer"}} onClick={() => handleDelete(doctor.ID)
}
>delete</i>
</td>
</tr>
))}
</tbody>
</table>
)}
</div>
);
};
export default Doctor;