import React, {useState, useEffect, useContext} from 'react'; import 'bootstrap/dist/css/bootstrap.min.css'; import StarRating from './StarRating'; import { useNavigate } from 'react-router-dom'; import {RestaurantContext} from "./RestaurantContext"; import {Alert} from "bootstrap"; const parseTime = (timeString) => { const [hours, minutes] = timeString.trim().split(':').map(Number); return new Date().setHours(hours, minutes, 0, 0); }; const roundToNextQuarter = (date) => { const minutes = date.getMinutes(); const roundedMinutes = Math.floor(minutes / 15) * 15; date.setMinutes(roundedMinutes, 0, 0); return date; }; const shouldMoveToNextDay = (currentTime, endTime) => { return (endTime - currentTime) <= 2 * 60 * 60 * 1000; }; const Restaurants = () => { const [restaurants, setRestaurants] = useState([]); const navigate = useNavigate(); const restaurantContext = useContext(RestaurantContext); useEffect(() => { setRestaurants(restaurantContext.restaurants); }, [restaurantContext]); const generateTimeSlots = (operatingHours) => { const timeSlots = []; const [startTimeStr, endTimeStr] = operatingHours.split('-').map((time) => time.trim()); const startTime = parseTime(startTimeStr); let endTime = parseTime(endTimeStr); const currentTime = new Date().getTime(); if (shouldMoveToNextDay(currentTime, endTime)) { endTime += 24 * 60 * 60 * 1000; } let currentTimeSlot = new Date(startTime); currentTimeSlot = roundToNextQuarter(currentTimeSlot); while (currentTimeSlot.getTime() < endTime) { timeSlots.push(currentTimeSlot.toISOString()); currentTimeSlot.setMinutes(currentTimeSlot.getMinutes() + 15); } return timeSlots; }; const handleDetailClick = (restaurantId) => { navigate(`/restaurants/${restaurantId}`); }; const handleTimeSlotClick = (table, timeSlot, restaurant) => { const tableNumber = table.id; const formattedTimeSlot = timeSlot; const restaurantId = restaurant.restaurantId; const encodedTableNumber = encodeURIComponent(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 = {}; if (tablesList.length === 0) { return
No tables available for reservations at this restaurant.
; } return tablesList.flatMap((table) => { const tableTimeSlots = generateTimeSlots(restaurant.operatingHours); if (!renderedTimeSlots[table.capacity]) { renderedTimeSlots[table.capacity] = 0; return ({restaurant.cuisineType}
{restaurant.operatingHours}
Ul. {restaurant.address}
{restaurant.tablesList && restaurant.tablesList.length > 0 ? (No tables available for reservations at this restaurant.
)}