-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateSupplier.js
119 lines (99 loc) · 2.82 KB
/
UpdateSupplier.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
import { useEffect, useState } from "react"
import { useNavigate, useParams } from "react-router-dom"
import supabase from "../config/supabaseClient"
const UpdateSupplier = () => {
const { ID } = useParams()
const navigate = useNavigate()
const[Name, setName] = useState('')
const[Address, setAddress] = useState('')
const[Email, setEmail] = useState('')
const[PhoneNumber, setPhoneNumber] = useState('')
const[formError, setFormError] = useState(null)
const handleSubmit = async(e) =>{
e.preventDefault()
if(!Name || !Address || !Email || !PhoneNumber){
setFormError('Please enter all the fields correctly')
return
}
const {data, error} = await supabase
.from('Supplier')
.update({Name, Address, Email, PhoneNumber})
.eq("ID", ID)
if(error){
console.log(error)
setFormError('Please enter all the fields correctly')
}
if(data){
console.log(data)
setFormError(null)
}
navigate('/supplier')
}
useEffect(() =>{
const fetchSuppliers = async() =>{
const {data, error} = await supabase
.from('Supplier')
.select()
.eq("ID", ID)
.single()
if(error){
navigate('/supplier', {replace: true})
}
if(data){
setName(data.Name)
setAddress(data.Address)
setEmail(data.Email)
setPhoneNumber(data.PhoneNumber)
console.log(data)
}
}
fetchSuppliers()
}, [ID, navigate])
return (
<div className="page update">
<form onSubmit={handleSubmit}>
<label htmlFor="Name">Name:</label>
<input
type="text"
id="name"
value={Name}
onChange={(e) => {
const value = e.target.value;
if (!/\d/.test(value) || value === '') {
setName(value);
setFormError(null);
} else {
setFormError('Name cannot contain numbers');
}
}}
/>
<label htmlFor="Address">Address:</label>
<input
type="text"
id="address"
value={Address}
onChange={(e) => setAddress(e.target.value)}
/>
<label htmlFor="Email">Email:</label>
<input
type="text"
id="email"
value={Email}
onChange={(e) => setEmail(e.target.value)}
/>
<label htmlFor="Phone Number">Phone Number:</label>
<input
type="text"
id="phone"
maxLength={11}
pattern="\d{11}"
value={PhoneNumber}
onChange={(e) => setPhoneNumber(e.target.value)}
/>
<button className="add">Update Supplier Data</button>
{formError && <p className="error">{formError}</p>}
</form>
</div>
)
}
export default UpdateSupplier