-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateOrder.js
153 lines (138 loc) · 4.22 KB
/
UpdateOrder.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
import { useEffect, useState } from "react";
import { useParams, useNavigate } from "react-router-dom";
import supabase from "../config/supabaseClient";
const UpdateOrder = () => {
const { id } = useParams();
const navigate = useNavigate();
const [OrderName, setOrderName] = useState("");
const [Date, setDate] = useState("");
const [Status, setStatus] = useState("");
const [Notes, setNotes] = useState("");
const [Supplier_ID, setSupplier_ID] = useState("");
const [formError, setFormError] = useState(null);
const [fetchError, setFetchError] = useState(null);
const [Supplier, setSupplier] = useState(null);
useEffect(() => {
const fetchSupplier = async () => {
const { data, error } = await supabase.from("Supplier").select();
if (error) {
setSupplier(null);
setFetchError("Couldn't fetch the Suppliers data");
console.log(error);
}
if (data) {
setSupplier(data);
setFetchError(null);
}
};
fetchSupplier();
}, []);
const handleSubmit = async (e) => {
e.preventDefault();
if (!OrderName || !Date || !Status || !Notes || !Supplier_ID) {
setFormError("Please fill in all the fields correctly.");
return;
}
const { data, error } = await supabase
.from("Order")
.update([{ OrderName, Date, Status, Notes, Supplier_ID }])
.eq("ID", id);
if (error) {
setFormError("Please fill in all the fields correctly.");
}
if (data) {
setFormError(null);
}
navigate("/order");
};
useEffect(() => {
const fetchOrder = async () => {
const { data, error } = await supabase
.from("Order")
.select()
.eq("ID", id)
.single();
if (error) {
navigate("/order", { replace: true });
}
if (data) {
setOrderName(data.OrderName);
setDate(data.Date);
setStatus(data.Status);
setNotes(data.Notes);
setSupplier_ID(data.Supplier_ID);
}
};
fetchOrder();
}, [id, navigate]);
return (
<div className="page create">
{fetchError && <p>{fetchError}</p>}
{Supplier && (
<form onSubmit={handleSubmit}>
<label htmlFor="OrderName">Order Name:</label>
<input
type="text"
id="OrderName"
value={OrderName}
onChange={(e) => {
const value = e.target.value;
if (!/\d/.test(value) || value === "") {
setOrderName(value);
setFormError(null);
} else {
setFormError("Order Name cannot contain numbers");
}
}}
/>
<label htmlFor="Date">Date:</label>
<input
type="date"
id="Date"
value={Date}
onChange={(e) => setDate(e.target.value)}
/>
<label htmlFor="Status">Status:</label>
<input
type="text"
id="Status"
value={Status}
onChange={(e) => {
const value = e.target.value;
if (!/\d/.test(value) || value === "") {
setStatus(value);
setFormError(null);
} else {
setFormError("Status cannot contain numbers");
}
}}
/>
<label htmlFor="Notes">Notes:</label>
<input
type="text"
id="Notes"
value={Notes}
onChange={(e) => setNotes(e.target.value)}
/>
<div className="droplist">
<label htmlfor="supp"> Supplier Name: </label>
<select
name="supplier"
id="supp"
onChange={(e) => setSupplier_ID(e.target.value)}
value={Supplier_ID}
>
<option label=" Please specify: "></option>
{Supplier.map((supplier) => (
<option value={supplier.ID}>{supplier.Name} </option>
))}
</select>
</div>
<button style={{marginTop: '5px'}} className="add">Update Order Data</button>
{formError && <p className="error">{formError}</p>}
</form>
)}
</div>
);
};
export default UpdateOrder;