-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateOperation.js
222 lines (200 loc) · 5.94 KB
/
CreateOperation.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
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
222
import { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import supabase from "../config/supabaseClient";
const CreateOperation = () => {
const navigate = useNavigate();
//const [ID, setID] = useState("");
const [OperationName, setOperationName] = useState("");
const [Room, setRoom] = useState("");
const [Date, setDate] = useState("");
const [Time, setTime] = useState("");
const [Notes, setNotes] = useState("");
const [Patient_ID, setPatient_ID] = useState("");
const [Doctor_ID, setDoctor_ID] = useState("");
const [Prosthetic_ID, setProsthetic_ID] = useState("");
const [formError, setFormError] = useState(null);
const [fetchError, setFetchError] = useState(null);
const [Patient, setPatient] = useState(null);
const [Doctor, setDoctor] = useState(null);
const [Prosthetics, setProsthetics] = useState(null);
useEffect(() => {
const fetchPatient = async () => {
const { data, error } = await supabase.from("Patient").select();
if (error) {
setPatient(null);
setFetchError("Couldn't fetch the Patient data");
console.log(error);
}
if (data) {
setPatient(data);
setFetchError(null);
}
};
fetchPatient();
}, []);
useEffect(() => {
const fetchDoctor = async () => {
const { data, error } = await supabase.from("Doctor").select();
if (error) {
setDoctor(null);
setFetchError("Couldn't fetch the Doctor data");
console.log(error);
}
if (data) {
setDoctor(data);
setFetchError(null);
}
};
fetchDoctor();
}, []);
useEffect(() => {
const fetchProsthetics = async () => {
const { data, error } = await supabase.from("Prosthetics").select();
if (error) {
setProsthetics(null);
setFetchError("Couldn't fetch the Prosthetic data");
console.log(error);
}
if (data) {
setProsthetics(data);
setFetchError(null);
}
};
fetchProsthetics();
}, []);
const handleSubmit = async (e) => {
e.preventDefault();
if (
!OperationName ||
!Room ||
!Date ||
!Time ||
!Prosthetic_ID ||
!Patient_ID ||
!Doctor_ID
) {
setFormError("Please enter all the fields correctly");
return;
}
const { data, error } = await supabase.from("Operation").insert([
{
OperationName,
Room,
Date,
Time,
Notes,
Prosthetic_ID,
Patient_ID,
Doctor_ID,
},
]);
if (error) {
console.log(error);
setFormError("Please enter all the fields correctly");
}
if (data) {
console.log(data);
setFormError(null);
}
navigate("/operation");
};
return (
<div className="page create">
{fetchError && <p>{fetchError}</p>}
{Prosthetics && Doctor && Patient && (
<form onSubmit={handleSubmit}>
<label htmlFor="OperationName">Operation Name:</label>
<input
type="text"
id="OperationName"
value={OperationName}
onChange={(e) => {
const value = e.target.value;
if (!/\d/.test(value) || value === "") {
setOperationName(value);
setFormError(null);
} else {
setFormError("Operation Name cannot contain numbers");
}
}}
/>
<label htmlFor="Room">Room:</label>
<input
type="text"
id="room"
value={Room}
onChange={(e) => setRoom(e.target.value)}
/>
<label htmlFor="Date">Date:</label>
<input
type="date"
id="date"
value={Date}
onChange={(e) => setDate(e.target.value)}
/>
<label htmlFor="Time">Time:</label>
<input
type="time"
id="time"
value={Time}
onChange={(e) => setTime(e.target.value)}
/>
<label htmlFor="Notes">Notes:</label>
<input
type="text"
id="notes"
value={Notes}
onChange={(e) => setNotes(e.target.value)}
/>
<div className="droplist">
<label htmlFor="pat"> Patient ID: </label>
<select
name="patient"
id="pat"
onChange={(e) => setPatient_ID(e.target.value)}
value={Patient_ID}
>
<option label=" Please specify: "></option>
{Patient.map((patient) => (
<option value={patient.ID}>{patient.ID} </option>
))}
</select>
</div>
<div className="droplist">
<label htmlFor="doc"> Doctor ID: </label>
<select
name="doctor"
id="doc"
onChange={(e) => setDoctor_ID(e.target.value)}
value={Doctor_ID}
>
<option label=" Please specify: "></option>
{Doctor.map((doctor) => (
<option value={doctor.ID}>{doctor.ID} </option>
))}
</select>
</div>
<div className="droplist">
<label htmlFor="proth"> Prosthetic Name: </label>
<select
name="prosthetic"
id="proth"
onChange={(e) => setProsthetic_ID(e.target.value)}
value={Prosthetic_ID}
>
<option label=" Please specify: "></option>
{Prosthetics.map((prosthetics) => (
<option value={prosthetics.ID}>
{prosthetics.ProstheticName}
</option>
))}
</select>
</div>
<button className="add">Add an Operation</button>
{formError && <p className="error">{formError}</p>}
</form>
)}
</div>
);
};
export default CreateOperation;