-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeerForm.js
137 lines (130 loc) · 3.72 KB
/
DeerForm.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
import { useState } from "react";
import { useNavigate } from "react-router-dom";
export const DeerForm = () => {
const [deer, setDeer] = useState({
userId: 0,
name: "",
description: "",
dateFirstSeen: new Date().toISOString(),
food: "",
handFeed: false,
});
const localDeerUser = localStorage.getItem("deeruser")
const deerUserObject = JSON.parse(localDeerUser)
const navigate = useNavigate();
const handleSaveButtonClick = (event) => {
event.preventDefault();
const deerToSendToApi = {
userId: deerUserObject.id,
name: deer.name,
description: deer.description,
dateFirstSeen: deer.dateFirstSeen,
food: deer.food,
handFeed: deer.handFeed,
};
fetch("http://localhost:8088/deer", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(deerToSendToApi),
})
.then((response) => response.json())
.then((savedDeer) => {
console.log("Saved deer:", savedDeer);
navigate("/deer");
});
};
return (
<form className="deerForm">
<h2 className="deerForm__title">Add a New Deer Profile</h2>
<fieldset>
<div className="form-group">
<label htmlFor="name">Deer Name:</label>
<input
required
autoFocus
type="text"
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
required
className="form-control"
placeholder="Description"
value={deer.description}
onChange={(evt) => {
const copy = { ...deer };
copy.description = evt.target.value;
setDeer(copy);
}}
/>
</div>
</fieldset>
<fieldset>
<div className="form-group">
<label htmlFor="dateFirstSeen">Date First Seen:</label>
<input
required
type="date"
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="food">Food:</label>
<input
required
type="text"
className="form-control"
placeholder="Food"
value={deer.food}
onChange={(evt) => {
const copy = { ...deer };
copy.food = evt.target.value;
setDeer(copy);
}}
/>
</div>
</fieldset>
<fieldset>
<div className="form-group">
<label htmlFor="handFeed">Hand Feed:</label>
<input
type="checkbox"
value={deer.handFeed}
onChange={(evt) => {
const copy = { ...deer };
copy.handFeed = evt.target.checked;
setDeer(copy);
}}
/>
</div>
</fieldset>
<button
onClick={(clickEvent) => handleSaveButtonClick(clickEvent)}
>
Save
</button>
</form>
)
}