[d24f17c] | 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 |
|
---|
| 8 | const RestaurantDetails = () => {
|
---|
| 9 | const navigate = useNavigate();
|
---|
| 10 |
|
---|
| 11 | const { id } = useParams();
|
---|
| 12 | const [restaurant, setRestaurant] = useState(null);
|
---|
| 13 | const [selectedTableId, setSelectedTableId] = useState('');
|
---|
| 14 | const [selectedTimeSlot, setSelectedTimeSlot] = useState('');
|
---|
| 15 | const [selectedTable, setSelectedTable] = useState(null);
|
---|
| 16 | const [filteredTimeSlots, setFilteredTimeSlots] = useState([]);
|
---|
| 17 | const [selectedCapacity, setSelectedCapacity] = useState(''); // Define selectedCapacity state
|
---|
| 18 |
|
---|
| 19 | useEffect(() => {
|
---|
| 20 | const fetchRestaurantDetails = async () => {
|
---|
| 21 | try {
|
---|
| 22 | if (!id) return;
|
---|
[8ca35dc] | 23 | const response = await axios.get(`http://localhost:8081/api/restaurants/${id}`);
|
---|
[d24f17c] | 24 | setRestaurant(response.data);
|
---|
| 25 | } catch (error) {
|
---|
| 26 | console.error('Error fetching restaurant details:', error);
|
---|
| 27 | }
|
---|
| 28 | };
|
---|
| 29 |
|
---|
| 30 | fetchRestaurantDetails();
|
---|
| 31 | }, [id]);
|
---|
| 32 |
|
---|
| 33 | useEffect(() => {
|
---|
| 34 | if (!selectedTableId) return; // If no table is selected, return early
|
---|
| 35 |
|
---|
| 36 | const fetchTableDetails = async () => {
|
---|
| 37 | try {
|
---|
[8ca35dc] | 38 | const response = await axios.get(`http://localhost:8081/api/tables/${selectedTableId}`);
|
---|
[d24f17c] | 39 | setSelectedTable(response.data);
|
---|
| 40 | } catch (error) {
|
---|
| 41 | console.error('Error fetching table details:', error);
|
---|
| 42 | }
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | fetchTableDetails();
|
---|
| 46 | }, [selectedTableId]);
|
---|
| 47 |
|
---|
| 48 | useEffect(() => {
|
---|
| 49 | if (!selectedTable || !restaurant) return; // If table or restaurant is not loaded, return early
|
---|
| 50 |
|
---|
| 51 | // Filter time slots based on the selected table
|
---|
| 52 | const currentTime = new Date();
|
---|
| 53 | const filteredSlots = selectedTable.timeSlots.filter(timeSlot => new Date(timeSlot) >= currentTime);
|
---|
| 54 | setFilteredTimeSlots(filteredSlots);
|
---|
| 55 | }, [selectedTable, restaurant]);
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | const formatTimeSlot = (timeSlot) => {
|
---|
| 59 | const date = new Date(timeSlot);
|
---|
| 60 | const formattedDate = date.toLocaleDateString();
|
---|
| 61 | const formattedTime = date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
---|
| 62 | return `${formattedDate} - ${formattedTime}`;
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | const handleTableSelect = (event) => {
|
---|
| 66 | const selectedTableId = event.target.value;
|
---|
| 67 | setSelectedTableId(selectedTableId);
|
---|
| 68 | };
|
---|
| 69 |
|
---|
| 70 | const handleTimeSlotSelect = (event) => {
|
---|
| 71 | const selectedTimeSlot = event.target.value;
|
---|
| 72 | setSelectedTimeSlot(selectedTimeSlot);
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | const handleReservationConfirmation = (restaurant) => {
|
---|
| 76 | const tableNumber = selectedTableId;
|
---|
| 77 | const formattedTimeSlot = selectedTimeSlot;
|
---|
| 78 | const restaurantId = restaurant.restaurantId;
|
---|
| 79 |
|
---|
| 80 | const encodedTableNumber = encodeURIComponent(tableNumber);
|
---|
| 81 | const encodedTimeSlot = encodeURIComponent(formattedTimeSlot);
|
---|
| 82 | const encodedRestaurantId = encodeURIComponent(restaurantId);
|
---|
| 83 |
|
---|
| 84 | navigate(`/reservationConfirmation/${encodedTableNumber}/${encodedTimeSlot}/${encodedRestaurantId}`);
|
---|
| 85 | };
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | return (
|
---|
| 89 | <div className="container">
|
---|
| 90 | {restaurant && (
|
---|
| 91 | <>
|
---|
| 92 | <h2 className="card-title">
|
---|
| 93 | {restaurant.name} <StarRating key={restaurant.id} rating={restaurant.rating}/>
|
---|
| 94 | </h2>
|
---|
| 95 | <div className="restaurant-details">
|
---|
| 96 | <p>Operating hours: {restaurant.operatingHours}</p>
|
---|
| 97 | <p>Cuisine: {restaurant.cuisineType}</p>
|
---|
| 98 | <p>Address: {restaurant.address}</p>
|
---|
| 99 | <p>Phone: {restaurant.phone}</p>
|
---|
| 100 | <p>Website: <a href={restaurant.website}>{restaurant.website}</a></p>
|
---|
| 101 | <p>Social Media Links: {restaurant.socialMediaLinks}</p>
|
---|
| 102 |
|
---|
| 103 | <label>Select Table:</label>
|
---|
| 104 | <select className="form-select" aria-label="Select Table" onChange={handleTableSelect}
|
---|
| 105 | value={selectedTableId}>
|
---|
| 106 | <option value="">Select Table</option>
|
---|
| 107 | {restaurant.tablesList.map((table, index) => (
|
---|
| 108 | <option key={index}
|
---|
| 109 | value={table.id}>{`Capacity: ${table.capacity} - ${table.location}`}</option>
|
---|
| 110 | ))}
|
---|
| 111 | </select>
|
---|
| 112 | {selectedTable && (
|
---|
| 113 | <>
|
---|
| 114 | <label>Select Time Slot:</label>
|
---|
| 115 | <select className="form-select mt-2" aria-label="Select Time Slot" onChange={handleTimeSlotSelect}>
|
---|
| 116 | <option value="">Select Time Slot</option>
|
---|
| 117 | {filteredTimeSlots.map((timeSlot, index) => (
|
---|
| 118 | <option key={index} value={timeSlot}>{formatTimeSlot(timeSlot)}</option>
|
---|
| 119 | ))}
|
---|
| 120 | </select>
|
---|
| 121 | </>
|
---|
| 122 | )}
|
---|
| 123 | <br/>
|
---|
| 124 | {/* Add a button to trigger reservation confirmation */}
|
---|
| 125 | <button className="btn btn-primary" onClick={() => handleReservationConfirmation(restaurant)}>
|
---|
| 126 | Confirm Reservation
|
---|
| 127 | </button>
|
---|
| 128 | </div>
|
---|
| 129 | </>
|
---|
| 130 | )}
|
---|
| 131 | </div>
|
---|
| 132 | );
|
---|
| 133 | };
|
---|
| 134 |
|
---|
| 135 | export default RestaurantDetails;
|
---|