-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeerEdit.js
126 lines (119 loc) · 3.37 KB
/
DeerEdit.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
import { useState, useEffect } from "react";
import { useParams, useNavigate } from "react-router-dom";
import "./DeerForm.css";
export const DeerEdit = () => {
const [deer, setDeer] = useState({
id: "",
name: "",
description: "",
dateFirstSeen: "",
handFeed: false,
foodPreferred: "",
userId: JSON.parse(localStorage.getItem("deeruser"))?.id,
});
const { id } = useParams();
const navigate = useNavigate();
useEffect(() => {
fetch(`http://localhost:8088/deer/${id}`)
.then((response) => response.json())
.then((data) => {
setDeer(data);
});
}, [id]);
const handleSaveButtonClick = (event) => {
event.preventDefault();
return fetch(`http://localhost:8088/deer/${deer.id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(deer),
})
.then((response) => response.json())
.then(() => {
navigate("/deer");
});
};
return (
<>
<form className="deerEditForm">
<h2 className="deerEditForm__title">Edit Deer</h2>
<fieldset>
<div className="form-group">
<label htmlFor="name">Deer Name:</label>
<input
type="text"
id="name"
required
autoFocus
className="form-control"
placeholder="Deer Name"
value={deer.name}
onChange={(evt) => {
const copy = { ...deer };
copy.name = evt.target.value;
setDeer(copy);
}}
/>
</div>
</fieldset>
<fieldset>
<div className="form-group">
<label htmlFor="description">Description:</label>
<textarea
id="description"
required
className="form-control"
placeholder="Description"
value={deer.description}
onChange={(evt) => {
const copy = { ...deer };
copy.description = evt.target.value;
setDeer(copy);
}}
></textarea>
</div>
</fieldset>
<fieldset>
<div className="form-group">
<label htmlFor="dateFirstSeen">Date First Seen:</label>
<input
type="date"
id="dateFirstSeen"
required
className="form-control"
placeholder="Date First Seen"
value={deer.dateFirstSeen}
onChange={(evt) => {
const copy = { ...deer };
copy.dateFirstSeen = evt.target.value;
setDeer(copy);}}
/>
</div>
</fieldset>
<fieldset>
<div className="form-group">
<label htmlFor="handFeed">Hand Feed:</label>
<input
type="checkbox"
id="handFeed"
className="form-control"
value={deer.handFeed}
onChange={(evt) => {
const copy = { ...deer };
copy.handFeed = evt.target.checked;
setDeer(copy);
}}
/>
</div>
</fieldset>
<button
onClick={(clickEvent) => handleSaveButtonClick(clickEvent)}
className="btn btn-primary"
>
Save Edits
</button>
</form>
</>
);
}