generated from HackYourFuture-CPH/hyf-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReservationForm.jsx
60 lines (55 loc) · 1.41 KB
/
ReservationForm.jsx
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
import React, { useState } from "react";
const ReservationForm = ({ mealId }) => {
const [formData, setFormData] = useState({
name: "",
email: "",
phonenumber: "",
});
const handleChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
};
const handleSubmit = async (e) => {
e.preventDefault();
try {
const response = await fetch("http://localhost:3001/api/reservations/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...formData, meal_id: mealId }),
});
if (response.ok) {
alert("Reservation successful!");
} else {
alert("Reservation failed.");
}
} catch (error) {
alert("Error submitting reservation");
}
};
return (
<form onSubmit={handleSubmit}>
<input
type="text"
name="name"
placeholder="Name"
onChange={handleChange}
required
/>
<input
type="email"
name="email"
placeholder="Email"
onChange={handleChange}
required
/>
<input
type="tel"
name="phonenumber"
placeholder="Phone Number"
onChange={handleChange}
required
/>
<button type="submit">Book Seat</button>
</form>
);
};
export default ReservationForm;