source: my-react-app/src/components/ReservationEdit.js@ db39d9e

main
Last change on this file since db39d9e was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.4 KB
Line 
1import React, { useState, useEffect } from 'react';
2import axios from 'axios';
3import {useNavigate, useParams} from 'react-router-dom';
4import StarRating from "./StarRating";
5
6const ReservationEdit = () => {
7 const navigate = useNavigate();
8
9 const [isLoading, setIsLoading] = useState(true);
10 const { reservationId } = useParams(); // Extract reservationId from URL params
11 const [formData, setFormData] = useState({});
12 const [table, setTable] = useState({});
13 const [restaurant, setRestaurant] = useState({});
14 const [timeSlots, setTimeSlots] = useState([]);
15 const [filteredTimeSlots, setFilteredTimeSlots] = useState([]);
16
17 useEffect(() => {
18 const fetchReservation = async () => {
19 try {
20 setIsLoading(true);
21 const response = await axios.get(`http://localhost:8080/api/reservations/${reservationId}`);
22 setFormData(response.data); // Set form data with reservation data
23 setTable(response.data.table);
24 setRestaurant(response.data.restaurant);
25 setTimeSlots(response.data.table.timeSlots);
26 setIsLoading(false);
27 } catch (error) {
28 console.error('Error fetching reservation:', error);
29 }
30 };
31 fetchReservation();
32 }, [reservationId]);
33
34 useEffect(() => {
35 if (!table || !restaurant) return; // If table or restaurant is not loaded, return early
36
37 const currentTime = new Date();
38
39 const filteredSlots = timeSlots.filter(timeSlot => new Date(timeSlot) >= currentTime);
40 setFilteredTimeSlots(filteredSlots);
41 }, [table, restaurant]);
42
43 const handleInputChange = (e) => {
44 const { name, value } = e.target;
45
46 // Check if the changed input is the time slot select element
47 if (name === 'selectedTimeSlot') {
48 // Update the formData with the selected time slot value
49 setFormData({ ...formData, checkInTime: value });
50 } else {
51 // For other input fields, update formData as usual
52 setFormData({ ...formData, [name]: value });
53 }
54 };
55
56 const handleSubmit = async (e) => {
57 e.preventDefault();
58 try {
59 // Send updated reservation data to the server
60 await axios.post(`http://localhost:8080/api/reservations/${reservationId}`, formData);
61 // Redirect or show success message
62 navigate(`/reservations`)
63 } catch (error) {
64 console.error('Error updating reservation:', error);
65 }
66 };
67
68 const formatTimeSlot = (timeSlot) => {
69 const date = new Date(timeSlot);
70 const formattedDate = date.toLocaleDateString();
71 const formattedTime = date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
72 return `${formattedDate} - ${formattedTime}`;
73 };
74
75 return (
76 <div className="container">
77 {isLoading ? ( // Conditional rendering based on loading state
78 <p>Loading...</p>
79 ) : (
80 <>
81 <h1>Edit Reservation</h1>
82 <div className="card-body">
83 <h2>
84 {restaurant.name} <StarRating key={restaurant.id} rating={restaurant.rating}/>
85 </h2>
86 <p className="card-text">{restaurant.cuisineType}</p>
87 <p className="card-text">{restaurant.operatingHours}</p>
88 <p className="card-text">Ul. {restaurant.address}</p>
89 <br/>
90 </div>
91 <form onSubmit={handleSubmit}>
92 <div className="mb-3">
93 <label htmlFor="checkInTime" className="form-label">Check-in Time</label>
94 <select className="form-select mt-2" aria-label="Select Time Slot"
95 name="selectedTimeSlot" // Add name attribute
96 onChange={handleInputChange}>
97 <option value="">Select Time Slot</option>
98 {filteredTimeSlots.map((timeSlot, index) => (
99 <option key={index} value={timeSlot}>{formatTimeSlot(timeSlot)}</option>
100 ))}
101 </select>
102 </div>
103 <div className="mb-3">
104 <label htmlFor="partySize" className="form-label">Party Size</label>
105 <input type="number" className="form-control" id="partySize" name="partySize"
106 max={table.capacity}
107 value={formData.partySize || ''} onChange={handleInputChange}/>
108 </div>
109 <div className="mb-3">
110 <label htmlFor="specialRequests" className="form-label">Special Requests</label>
111 <input type="text" className="form-control" id="specialRequests" name="specialRequests"
112 value={formData.specialRequests || ''} onChange={handleInputChange}/>
113 </div>
114 <button type="submit" className="btn btn-primary">Submit</button>
115 </form>
116 </>
117 )}
118 </div>
119 );
120
121};
122
123export default ReservationEdit;
Note: See TracBrowser for help on using the repository browser.