import React, { useState, useEffect } from 'react'; import axios from 'axios'; import 'bootstrap/dist/css/bootstrap.min.css'; import StarRating from './StarRating'; import { useNavigate } from 'react-router-dom'; const Restaurants = () => { const [restaurants, setRestaurants] = useState([]); const navigate = useNavigate(); useEffect(() => { const fetchRestaurants = async () => { try { const response = await axios.get('http://localhost:8080/api/restaurants'); setRestaurants(response.data); } catch (error) { console.error('Error fetching restaurants:', error); } }; fetchRestaurants(); }, []); const handleDetailClick = (restaurantId) => { navigate(`/restaurants/${restaurantId}`); } const handleTimeSlotClick = (table, timeSlot, restaurant) => { const tableNumber = table.id; const formattedTimeSlot = timeSlot; const restaurantId = restaurant.restaurantId; const encodedTableNumber = encodeURI(tableNumber); const encodedTimeSlot = encodeURIComponent(formattedTimeSlot); const encodedRestaurantId = encodeURIComponent(restaurantId); navigate(`/reservationConfirmation/${encodedTableNumber}/${encodedTimeSlot}/${encodedRestaurantId}`); } const renderTimeSlots = (tablesList, restaurant) => { const currentTime = new Date().getTime(); let renderedTimeSlots = {}; // Object to store rendered slots for each table capacity return tablesList.flatMap(table => { // Render capacity header when encountering a new capacity if (!renderedTimeSlots[table.capacity]) { renderedTimeSlots[table.capacity] = 0; return (

Table for: {table.capacity}

{table.timeSlots.map((timeSlot, index) => { const timeSlotTime = new Date(timeSlot).getTime(); const tableCapacity = table.capacity; // Check if the time slot is after the current time and limit to 3 slots if (timeSlotTime > currentTime && renderedTimeSlots[tableCapacity] < 3) { renderedTimeSlots[tableCapacity]++; const timeSlotDateTime = new Date(timeSlot); const formattedTime = timeSlotDateTime.toLocaleTimeString(); const formattedDateTime = timeSlotDateTime.toLocaleString(); // Format for both date and time return ( ); } else { return null; // Render nothing if the condition is not met } })}
); } else { // If capacity has been rendered, return null to avoid duplicate rendering return null; } }); } return (

Restaurants

{restaurants.map((restaurant) => (
{restaurant.name}

{restaurant.cuisineType}

{restaurant.operatingHours}

Ul. {restaurant.address}

{renderTimeSlots(restaurant.tablesList.flatMap((table) => table), restaurant)}
))}
); }; export default Restaurants;