import React, { useState, useEffect } from 'react'; import { useParams } from 'react-router-dom'; import axios from 'axios'; import { useNavigate } from 'react-router-dom'; const ReservationConfirmation = () => { const navigate = useNavigate(); const [restaurant, setRestaurant] = useState({}); const [table, setTable] = useState({}); const [reservationDateTime, setReservationDateTime] = useState(''); const [partySize, setPartySize] = useState(''); const [specialRequests, setSpecialRequests] = useState(''); const { tableNumber, timeSlot, restaurantId } = useParams(); useEffect(() => { const fetchTableDetails = async () => { try { const tableResponse = await axios.get(`http://localhost:8080/api/tables/${tableNumber}`); setTable(tableResponse.data); const restaurantResponse = await axios.get(`http://localhost:8080/api/restaurants/${restaurantId}`); setRestaurant(restaurantResponse.data); } catch (error) { console.error('Error fetching table details:', error); } }; fetchTableDetails(); }, [tableNumber, restaurantId]); const handleSubmit = async (e) => { e.preventDefault(); // Handle form submission here try { // Check if restaurant and table are valid if (!restaurant || !table) { console.error("Restaurant or table is missing."); return; } const response = await axios.post(`http://localhost:8080/api/reservations`, { restaurant: restaurant, // Assuming restaurant has an 'id' property table: table, // Assuming table has an 'id' property checkInTime: timeSlot, // Fill in as needed partySize: partySize, specialRequests: specialRequests }); // Handle successful reservation creation // console.log("Reservation created:", response.data); navigate("/reservations") } catch (error) { console.error("Error creating reservation:", error); } }; const initialRemainingTime = localStorage.getItem('remainingTime') || 300; const [remainingTime, setRemainingTime] = useState(parseInt(initialRemainingTime)); useEffect(() => { const timer = setInterval(() => { setRemainingTime((prevTime) => { const newTime = prevTime - 1; localStorage.setItem('remainingTime', newTime.toString()); // Update remaining time in localStorage return newTime; }); }, 1000); return () => clearInterval(timer); }, []); useEffect(() => { // Reset remaining time if it reaches zero if (remainingTime <= 0) { localStorage.removeItem('remainingTime'); // Optionally, handle releasing the hold on the table } }, [remainingTime]); const formatTime = (timeInSeconds) => { const minutes = Math.floor(timeInSeconds / 60); const seconds = timeInSeconds % 60; return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; }; return (
Remaining Time: {formatTime(remainingTime)}