1 | import React, { useState, useEffect } from 'react';
|
---|
2 | import { useNavigate } from 'react-router-dom';
|
---|
3 | import axios from 'axios';
|
---|
4 | import 'bootstrap/dist/css/bootstrap.min.css';
|
---|
5 | import { useParams } from 'react-router-dom';
|
---|
6 | import StarRating from "./StarRating";
|
---|
7 | import MenuList from "./MenuList";
|
---|
8 |
|
---|
9 |
|
---|
10 | const RestaurantDetails = () => {
|
---|
11 | const navigate = useNavigate();
|
---|
12 | const { id } = useParams();
|
---|
13 | const [preOrderedItems, setPreOrderedItems] = useState([]);
|
---|
14 |
|
---|
15 | const [restaurant, setRestaurant] = useState(null);
|
---|
16 | const [selectedTableId, setSelectedTableId] = useState('');
|
---|
17 | const [selectedDate, setSelectedDate] = useState('');
|
---|
18 | const [selectedTime, setSelectedTime] = useState('');
|
---|
19 | const [timeOptions, setTimeOptions] = useState([]);
|
---|
20 | const [selectedTable, setSelectedTable] = useState(null);
|
---|
21 |
|
---|
22 | useEffect(() => {
|
---|
23 | const fetchRestaurantDetails = async () => {
|
---|
24 | try {
|
---|
25 | if (!id) return;
|
---|
26 | const response = await axios.get(`http://localhost:8081/api/restaurants/${id}`);
|
---|
27 | setRestaurant(response.data);
|
---|
28 | } catch (error) {
|
---|
29 | console.error('Error fetching restaurant details:', error);
|
---|
30 | }
|
---|
31 | };
|
---|
32 |
|
---|
33 | fetchRestaurantDetails();
|
---|
34 | }, [id]);
|
---|
35 |
|
---|
36 | useEffect(() => {
|
---|
37 | if (!selectedTableId) return;
|
---|
38 |
|
---|
39 | const fetchTableDetails = async () => {
|
---|
40 | try {
|
---|
41 | const response = await axios.get(`http://localhost:8081/api/tables/${selectedTableId}`);
|
---|
42 | setSelectedTable(response.data);
|
---|
43 | } catch (error) {
|
---|
44 | console.error('Error fetching table details:', error);
|
---|
45 | }
|
---|
46 | };
|
---|
47 |
|
---|
48 | fetchTableDetails();
|
---|
49 | }, [selectedTableId]);
|
---|
50 |
|
---|
51 | const today = new Date();
|
---|
52 | const year = today.getFullYear();
|
---|
53 | const month = String(today.getMonth() + 1).padStart(2, '0');
|
---|
54 | const day = String(today.getDate()).padStart(2, '0');
|
---|
55 | const formattedDate = `${year}-${month}-${day}`;
|
---|
56 | const parseOperatingHours = (operatingHours) => {
|
---|
57 | const [start, end] = operatingHours.split('-');
|
---|
58 | const [startHour, startMinute] = start.split(':').map(Number);
|
---|
59 | const [endHour, endMinute] = end.split(':').map(Number);
|
---|
60 |
|
---|
61 | const startTime = new Date();
|
---|
62 | startTime.setHours(startHour, startMinute, 0, 0);
|
---|
63 |
|
---|
64 | const endTime = new Date();
|
---|
65 | endTime.setHours(endHour < startHour ? endHour + 24 : endHour, endMinute, 0, 0);
|
---|
66 |
|
---|
67 | return { startTime, endTime };
|
---|
68 | };
|
---|
69 |
|
---|
70 | const generateTimeOptions = (operatingHours) => {
|
---|
71 | const { startTime, endTime } = parseOperatingHours(operatingHours);
|
---|
72 | const now = new Date();
|
---|
73 |
|
---|
74 | const selectedDateObj = new Date(selectedDate);
|
---|
75 | const isToday = selectedDateObj.toDateString() === now.toDateString();
|
---|
76 |
|
---|
77 | let currentTime = isToday ? roundToNext15Minutes(new Date()) : new Date(startTime);
|
---|
78 |
|
---|
79 | const options = [];
|
---|
80 | while (currentTime <= endTime) {
|
---|
81 | options.push(currentTime.toTimeString().slice(0, 5));
|
---|
82 | currentTime = new Date(currentTime.getTime() + 15 * 60 * 1000);
|
---|
83 | }
|
---|
84 |
|
---|
85 | return options;
|
---|
86 | };
|
---|
87 |
|
---|
88 | useEffect(() => {
|
---|
89 | if (restaurant && selectedDate) {
|
---|
90 | const options = generateTimeOptions(restaurant.operatingHours);
|
---|
91 | setTimeOptions(options);
|
---|
92 | }
|
---|
93 | }, [restaurant, selectedDate]);
|
---|
94 |
|
---|
95 | const handleTableSelect = (event) => {
|
---|
96 | setSelectedTableId(event.target.value);
|
---|
97 | };
|
---|
98 |
|
---|
99 | const handleReservationConfirmation = () => {
|
---|
100 | const encodedTableId = encodeURIComponent(selectedTableId);
|
---|
101 | const encodedDateTime = encodeURIComponent(`${selectedDate}T${selectedTime}`);
|
---|
102 | const encodedRestaurantId = encodeURIComponent(restaurant.restaurantId);
|
---|
103 |
|
---|
104 | const totalPrice = preOrderedItems.reduce((acc, item) => acc + item.price * item.quantity, 0).toFixed(2);
|
---|
105 |
|
---|
106 | navigate(`/reservationConfirmation/${encodedTableId}/${encodedDateTime}/${encodedRestaurantId}`, {
|
---|
107 | state: {
|
---|
108 | preOrderedItems: preOrderedItems,
|
---|
109 | totalPrice: totalPrice,
|
---|
110 | }
|
---|
111 | });
|
---|
112 | };
|
---|
113 |
|
---|
114 |
|
---|
115 | const roundToNext15Minutes = (date) => {
|
---|
116 | const minutes = date.getMinutes();
|
---|
117 | const remainder = minutes % 15;
|
---|
118 | if (remainder === 0) return date;
|
---|
119 |
|
---|
120 | date.setMinutes(minutes + 15 - remainder);
|
---|
121 | date.setSeconds(0, 0);
|
---|
122 | return date;
|
---|
123 | };
|
---|
124 |
|
---|
125 | return (
|
---|
126 | <div className="container">
|
---|
127 | {restaurant && (
|
---|
128 | <>
|
---|
129 | <h2 className="card-title">
|
---|
130 | {restaurant.name} <StarRating key={restaurant.id} rating={restaurant.rating} />
|
---|
131 | </h2>
|
---|
132 | <div className="restaurant-details">
|
---|
133 | <p>Operating hours: {restaurant.operatingHours}</p>
|
---|
134 | <p>Cuisine: {restaurant.cuisineType}</p>
|
---|
135 | <p>Address: {restaurant.address}</p>
|
---|
136 | <p>Phone: {restaurant.phone}</p>
|
---|
137 | <p>
|
---|
138 | Website: <a href={restaurant.website}>{restaurant.website}</a>
|
---|
139 | </p>
|
---|
140 | <p>Social Media Links: {restaurant.socialMediaLinks}</p>
|
---|
141 |
|
---|
142 | <label>Select Table:</label>
|
---|
143 | <select
|
---|
144 | className="form-select"
|
---|
145 | aria-label="Select Table"
|
---|
146 | onChange={handleTableSelect}
|
---|
147 | value={selectedTableId}
|
---|
148 | >
|
---|
149 | <option value="">Select Table</option>
|
---|
150 | {restaurant.tablesList.map((table) => (
|
---|
151 | <option key={table.id} value={table.id}>
|
---|
152 | {`Capacity: ${table.capacity} - ${table.location}`}
|
---|
153 | </option>
|
---|
154 | ))}
|
---|
155 | </select>
|
---|
156 |
|
---|
157 | {selectedTable && (
|
---|
158 | <>
|
---|
159 | <label>Select Date:</label>
|
---|
160 | <input
|
---|
161 | type="date"
|
---|
162 | className="form-control mt-2"
|
---|
163 | onChange={(event) => setSelectedDate(event.target.value)}
|
---|
164 | value={selectedDate}
|
---|
165 | min={formattedDate}
|
---|
166 | onKeyDown={(e) => e.preventDefault()}
|
---|
167 | />
|
---|
168 | {!selectedDate && <p style={{ color: "red" }}>Please select a valid date.</p>}
|
---|
169 |
|
---|
170 | <label>Select Time:</label>
|
---|
171 | <select
|
---|
172 | className="form-select mt-2"
|
---|
173 | onChange={(event) => setSelectedTime(event.target.value)}
|
---|
174 | value={selectedTime}
|
---|
175 | disabled={!selectedDate}
|
---|
176 | >
|
---|
177 | <option value="">Select Time</option>
|
---|
178 | {timeOptions.map((time, index) => (
|
---|
179 | <option key={index} value={time}>
|
---|
180 | {time}
|
---|
181 | </option>
|
---|
182 | ))}
|
---|
183 | </select>
|
---|
184 | </>
|
---|
185 | )}
|
---|
186 | <MenuList
|
---|
187 | restaurantId={restaurant.restaurantId}
|
---|
188 | setPreOrderedItems={setPreOrderedItems}
|
---|
189 | preOrderedItems={preOrderedItems}
|
---|
190 | />
|
---|
191 | <br />
|
---|
192 | <button
|
---|
193 | className="btn btn-primary"
|
---|
194 | onClick={handleReservationConfirmation}
|
---|
195 | disabled={!selectedTableId || !selectedDate || !selectedTime}>
|
---|
196 | Confirm Reservation
|
---|
197 | </button>
|
---|
198 | </div>
|
---|
199 | </>
|
---|
200 | )}
|
---|
201 | </div>
|
---|
202 | );
|
---|
203 | };
|
---|
204 |
|
---|
205 | export default RestaurantDetails; |
---|