import React, {useState, useEffect, useContext} from 'react'; import axios from 'axios'; import 'bootstrap/dist/css/bootstrap.min.css'; import {useNavigate} from "react-router-dom"; import {jwtDecode} from "jwt-decode"; import {RestaurantContext} from "./RestaurantContext"; const Reservations = () => { const navigate = useNavigate(); const { restaurants } = useContext(RestaurantContext); const [reservations, setReservations] = useState([]); useEffect(() => { const fetchReservations = async () => { try { const token = localStorage.getItem("token"); if (!token) { console.error("No token found"); return; } const decodedToken = jwtDecode(token); const userId = decodedToken.iss; const response = await axios.get(`http://localhost:8081/api/reservations/by/${userId}`); setReservations(response.data); } catch (error) { console.error('Error fetching reservations:', error); } }; fetchReservations(); }, []); const handleEditReservation = async (reservationId) => { if(reservationId!=null) { navigate(`/reservations/reservationEdit/${reservationId}`); } } const handleCancelReservation = async (reservationID) => { try { await axios.delete(`http://localhost:8081/api/reservations/delete/${reservationID}`); setReservations(reservations.filter(reservation => reservation.reservationID !== reservationID)); alert('Reservation canceled successfully!'); } catch (error) { console.error('Error canceling reservation:', error); alert('An error occurred while canceling the reservation. Please try again later.'); } }; return (
{reservations.length === 0 ? (

No active reservations

Looking for a place to dine? Check out our restaurants.

) : ( reservations.map(reservation => (
Reservation ID: {reservation.reservationID}

Restaurant: { restaurants.find(r => r.restaurantId === reservation.restaurantId)?.name || "Not specified" }

Table Number: {reservation?.tableNumber || "Not specified"}

Reservation Date: {reservation.checkInTime ? new Date(reservation.checkInTime).toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) : "Not specified"}

Reservation Time: {reservation.checkInTime ? new Date(reservation.checkInTime).toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }) : "Not specified"}

Reservation made on: {reservation.reservationDateTime ? new Date(reservation.reservationDateTime).toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }) : "Not specified"} {reservation.reservationDateTime ? new Date(reservation.reservationDateTime).toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) : "Not specified"}

Party Size: {reservation.partySize || "Not specified"}

{reservation.preOrderedItems && reservation.preOrderedItems.length > 0 ? (
Pre-Ordered Items:
    {reservation.preOrderedItems.map((item, index) => (
  • {item?.preorderedItemName} × {item.quantity} ${(item.price * item.quantity).toFixed(2)}
  • ))}
Grand Total: ${reservation.preOrderedItems.reduce((acc, item) => acc + (item.quantity * item.price), 0).toFixed(2)}
) : (

No pre-ordered items.

)}

Special Requests: {reservation.specialRequests || "None"}

Status: {reservation.status || "Pending"}

Grace period of 15 minutes +-

)) )}
); }; export default Reservations;