-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateProsthetic.js
197 lines (173 loc) · 6.24 KB
/
UpdateProsthetic.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
import { useEffect, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import supabase from "../config/supabaseClient";
const UpdateProsthetic = () => {
const { id } = useParams();
const navigate = useNavigate();
const [ProstheticName, setProstheticName] = useState("");
const [ProstheticType, setProstheticType] = useState("");
const [ProstheticDescription, setProstheticDescription] = useState("");
const [ProstheticMaterial, setProstheticMaterial] = useState("");
const [ProstheticWeight, setProstheticWeight] = useState("");
const [ProstheticNotes, setProstheticNotes] = useState("");
const [ProstheticPrice, setProstheticPrice] = useState("");
const [ProstheticSize, setProstheticSize] = useState("");
const [Order_ID, setOrder_ID] = useState("");
const [formError, setFormError] = useState(null);
const [fetchError, setFetchError] = useState(null);
const [Order, setOrder] = useState(null);
useEffect(() => {
const fetchOrder = async () => {
const { data, error } = await supabase
.from("Order")
.select();
if (error) {
setOrder(null);
setFetchError("Couldn't fetch the Order data");
console.log(error);
}
if (data) {
setOrder(data);
setFetchError(null);
}
};
fetchOrder();
}, []);
const handleSubmit = async (e) => {
e.preventDefault();
if(!ProstheticName || !ProstheticType || !ProstheticDescription || !ProstheticMaterial
|| !ProstheticWeight || !ProstheticNotes || !ProstheticPrice || !ProstheticSize || !Order_ID) {
setFormError("Please enter all the fields correctly");
return;
}
const { data, error } = await supabase
.from("Prosthetics")
.update([{ ProstheticName, ProstheticType, ProstheticDescription, ProstheticMaterial,
ProstheticWeight, ProstheticNotes, ProstheticPrice, ProstheticSize, Order_ID }])
.eq("ID", id);
if (error) {
console.log(error);
setFormError("Please enter all the fields correctly");
}
if (data) {
console.log(data);
setFormError(null);
}
navigate("/prosthetics");
};
useEffect(() => {
const fetchProsthetics = async () => {
const { data, error } = await supabase
.from("Prosthetics")
.select()
.eq("ID", id)
.single();
if (error) {
navigate("/prosthetics", { replace: true });
}
if (data) {
setProstheticName(data.ProstheticName)
setProstheticType(data.ProstheticType)
setProstheticDescription(data.ProstheticDescription)
setProstheticMaterial(data.ProstheticMaterial)
setProstheticWeight(data.ProstheticWeight)
setProstheticNotes(data.ProstheticNotes)
setProstheticPrice(data.ProstheticPrice)
setProstheticSize(data.ProstheticSize)
setOrder_ID(data.Order_ID);
}
};
fetchProsthetics();
}, [id, navigate]);
return (
<div className="page create">
{fetchError && <p>{fetchError}</p>}
{Order && (
<form onSubmit={handleSubmit}>
<label htmlFor="ProstheticName">Prosthetic Name:</label>
<input
type="text"
id="ProstheticName"
value={ProstheticName}
onChange={(e) => {
const value = e.target.value;
if (!/\d/.test(value) || value === "") {
setProstheticName(value);
setFormError(null);
} else {
setFormError("Prosthetic Name cannot contain numbers");
}
}}
/>
<label htmlFor="ProstheticType">Prosthetic Type:</label>
<input
type="text"
id="ProstheticType"
value={ProstheticType}
onChange={(e) => setProstheticType(e.target.value)}
/>
<label htmlFor="ProstheticDescription">Prosthetic Description:</label>
<input
type="text"
id="ProstheticDescription"
value={ProstheticDescription}
onChange={(e) => setProstheticDescription(e.target.value)}
/>
<label htmlFor="ProstheticMaterial">Prosthetic Material:</label>
<input
type="text"
id="ProstheticMaterial"
value={ProstheticMaterial}
onChange={(e) => setProstheticMaterial(e.target.value)}
/>
<label htmlFor="ProstheticWeight">Prosthetic Weight (in KG):</label>
<input
type="number"
id="ProstheticWeight"
value={ProstheticWeight}
onChange={(e) => setProstheticWeight(e.target.value)}
/>
<label htmlFor="ProstheticNotes">Prosthetic Notes:</label>
<input
type="text"
id="ProstheticNotes"
value={ProstheticNotes}
onChange={(e) => setProstheticNotes(e.target.value)}
/>
<label htmlFor="ProstheticPrice">Prosthetic Price (in EGP):</label>
<input
type="number"
id="ProstheticPrice"
value={ProstheticPrice}
onChange={(e) => setProstheticPrice(e.target.value)}
/>
<label htmlFor="ProstheticSize">Prosthetic Size:</label>
<input
type="text"
id="ProstheticSize"
value={ProstheticSize}
onChange={(e) => setProstheticSize(e.target.value)}
/>
<div className="droplist">
<label htmlfor="ord"> Order ID: </label>
<select
name="order"
style={{marginBottom: "16px"}}
id="ord"
onChange={(e) => setOrder_ID(e.target.value)}
value={Order_ID}
>
<option label=" Please specify: "></option>
{Order.map((order) => (
<option value={order.ID}>{order.ID} </option>
))}
</select>
</div>
<button className="add">Update Prosthetic Data</button>
{formError && <p className="error">{formError}</p>}
</form>
)}
</div>
);
};
export default UpdateProsthetic;