[d24f17c] | 1 | import React, { useState, useEffect } from 'react';
|
---|
| 2 | import { useParams } from 'react-router-dom';
|
---|
| 3 | import axios from 'axios';
|
---|
| 4 | import { useNavigate } from 'react-router-dom';
|
---|
[8ca35dc] | 5 | import {jwtDecode} from "jwt-decode";
|
---|
[f5b256e] | 6 | import {request} from "../axios_helper";
|
---|
| 7 | import restaurants from "./Restaurants";
|
---|
[d24f17c] | 8 |
|
---|
| 9 | const ReservationConfirmation = () => {
|
---|
| 10 | const navigate = useNavigate();
|
---|
| 11 |
|
---|
| 12 | const [restaurant, setRestaurant] = useState({});
|
---|
[f5b256e] | 13 | const [user, setUser] = useState({});
|
---|
[d24f17c] | 14 | const [table, setTable] = useState({});
|
---|
| 15 | const [reservationDateTime, setReservationDateTime] = useState('');
|
---|
| 16 | const [partySize, setPartySize] = useState('');
|
---|
| 17 | const [specialRequests, setSpecialRequests] = useState('');
|
---|
| 18 | const { tableNumber, timeSlot, restaurantId } = useParams();
|
---|
| 19 |
|
---|
[f5b256e] | 20 | const adjustedTimeSlot = new Date(new Date(timeSlot).getTime() + 60 * 60 * 1000).toISOString();
|
---|
[d24f17c] | 21 | useEffect(() => {
|
---|
[f5b256e] | 22 | const fetchDetails = async () => {
|
---|
[d24f17c] | 23 | try {
|
---|
[8ca35dc] | 24 | const tableResponse = await axios.get(`http://localhost:8081/api/tables/${tableNumber}`);
|
---|
[d24f17c] | 25 | setTable(tableResponse.data);
|
---|
| 26 |
|
---|
[8ca35dc] | 27 | const restaurantResponse = await axios.get(`http://localhost:8081/api/restaurants/${restaurantId}`);
|
---|
[d24f17c] | 28 | setRestaurant(restaurantResponse.data);
|
---|
[f5b256e] | 29 |
|
---|
| 30 | const token = localStorage.getItem("token");
|
---|
| 31 | if (!token) {
|
---|
| 32 | console.error("No token found");
|
---|
| 33 | return;
|
---|
| 34 | }
|
---|
| 35 | const decodedToken = jwtDecode(token);
|
---|
| 36 | const userId = decodedToken.iss;
|
---|
| 37 |
|
---|
| 38 | const userResponse = await axios.get(`http://localhost:8081/api/user/${userId}`);
|
---|
| 39 | setUser(userResponse.data);
|
---|
[d24f17c] | 40 | } catch (error) {
|
---|
[f5b256e] | 41 | console.error('Error fetching table or restaurant details:', error);
|
---|
[d24f17c] | 42 | }
|
---|
| 43 | };
|
---|
[f5b256e] | 44 | fetchDetails();
|
---|
[d24f17c] | 45 | }, [tableNumber, restaurantId]);
|
---|
| 46 |
|
---|
| 47 | const handleSubmit = async (e) => {
|
---|
| 48 | e.preventDefault();
|
---|
[8ca35dc] | 49 |
|
---|
[f5b256e] | 50 | const payload = {
|
---|
| 51 | reservationID: 0,
|
---|
| 52 | userEmail: user.email,
|
---|
| 53 | rating: parseFloat(restaurant.rating) || null,
|
---|
| 54 | tableNumber: parseInt(table.id, 10),
|
---|
| 55 | restaurant: restaurant,
|
---|
| 56 | reservationDateTime: adjustedTimeSlot,
|
---|
| 57 | partySize: parseInt(partySize, 10),
|
---|
| 58 | status: 'Reserved',
|
---|
| 59 | specialRequests: specialRequests.trim(),
|
---|
| 60 | paymentStatus: 'Pending',
|
---|
| 61 | };
|
---|
[d24f17c] | 62 |
|
---|
[8ca35dc] | 63 |
|
---|
[f5b256e] | 64 | try {
|
---|
| 65 | const response = await axios.post('http://localhost:8081/api/reservations', payload);
|
---|
| 66 | console.log('Reservation created successfully:', response.data);
|
---|
| 67 | navigate("/reservations")
|
---|
[d24f17c] | 68 | } catch (error) {
|
---|
[f5b256e] | 69 | if (error.response) {
|
---|
| 70 | alert('The selected time slot is no longer available. Please choose another time.');
|
---|
| 71 | } else {
|
---|
| 72 | alert('Network error. Please check your internet connection.');
|
---|
| 73 | }
|
---|
[d24f17c] | 74 | }
|
---|
| 75 | };
|
---|
| 76 |
|
---|
[f5b256e] | 77 | const calculateCheckOutTime = (checkInTime) => {
|
---|
| 78 | const checkIn = new Date(checkInTime);
|
---|
| 79 | checkIn.setHours(checkIn.getHours() + 2);
|
---|
| 80 | return checkIn.toISOString();
|
---|
| 81 | };
|
---|
| 82 |
|
---|
[d24f17c] | 83 | const initialRemainingTime = localStorage.getItem('remainingTime') || 300;
|
---|
[f5b256e] | 84 | const [remainingTime, setRemainingTime] = useState(parseInt(initialRemainingTime, 10));
|
---|
[d24f17c] | 85 |
|
---|
| 86 | useEffect(() => {
|
---|
| 87 | const timer = setInterval(() => {
|
---|
| 88 | setRemainingTime((prevTime) => {
|
---|
| 89 | const newTime = prevTime - 1;
|
---|
[f5b256e] | 90 | localStorage.setItem('remainingTime', newTime.toString());
|
---|
[d24f17c] | 91 | return newTime;
|
---|
| 92 | });
|
---|
| 93 | }, 1000);
|
---|
| 94 |
|
---|
| 95 | return () => clearInterval(timer);
|
---|
| 96 | }, []);
|
---|
| 97 |
|
---|
| 98 | useEffect(() => {
|
---|
| 99 | if (remainingTime <= 0) {
|
---|
| 100 | localStorage.removeItem('remainingTime');
|
---|
[f5b256e] | 101 | alert("Time has expired. Please try reserving again.");
|
---|
| 102 | navigate('/restaurants'); // Redirect or take necessary action
|
---|
[d24f17c] | 103 | }
|
---|
[f5b256e] | 104 | }, [remainingTime, navigate]);
|
---|
[d24f17c] | 105 |
|
---|
| 106 | const formatTime = (timeInSeconds) => {
|
---|
| 107 | const minutes = Math.floor(timeInSeconds / 60);
|
---|
| 108 | const seconds = timeInSeconds % 60;
|
---|
| 109 | return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
|
---|
| 110 | };
|
---|
| 111 |
|
---|
[f5b256e] | 112 | const formatTimeSlot = (timeSlot) => {
|
---|
| 113 | const utcDate = new Date(timeSlot);
|
---|
| 114 | const localDate = new Date(utcDate.toLocaleString("en-US", { timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone }));
|
---|
| 115 | const formattedDate = localDate.toLocaleDateString();
|
---|
| 116 | const formattedTime = localDate.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
---|
| 117 | return `${formattedDate} - ${formattedTime}`;
|
---|
| 118 | };
|
---|
| 119 |
|
---|
[d24f17c] | 120 | return (
|
---|
| 121 | <div className="container mt-5">
|
---|
| 122 | <div className="row justify-content-center">
|
---|
| 123 | <div className="col-md-6">
|
---|
| 124 | <div className="card">
|
---|
| 125 | <div className="card-header">
|
---|
| 126 | <h3 className="text-center">Reservation Confirmation</h3>
|
---|
| 127 | <p>Remaining Time: {formatTime(remainingTime)}</p>
|
---|
| 128 | </div>
|
---|
| 129 | <form onSubmit={handleSubmit}>
|
---|
| 130 | <div className="card-body">
|
---|
| 131 | <h5 className="card-title">Reservation Details</h5>
|
---|
| 132 | <p className="card-text">
|
---|
[f5b256e] | 133 | <strong>Restaurant:</strong> {restaurant.name || 'Loading...'} <br />
|
---|
| 134 | <strong>Cuisine type:</strong> {restaurant.cuisineType || 'Loading...'} <br />
|
---|
| 135 | <strong>Selected Time Slot:</strong> {formatTimeSlot(timeSlot)} <br />
|
---|
| 136 | <strong>Party size:</strong>{' '}
|
---|
| 137 | <input
|
---|
| 138 | type="number"
|
---|
| 139 | max={table.capacity}
|
---|
| 140 | value={partySize}
|
---|
| 141 | onChange={(e) => setPartySize(e.target.value)}
|
---|
| 142 | />
|
---|
| 143 | <strong>Table size:</strong> {table.capacity} <br />
|
---|
| 144 | <strong>Special Requests:</strong>{' '}
|
---|
| 145 | <input
|
---|
| 146 | type="text"
|
---|
| 147 | value={specialRequests}
|
---|
| 148 | onChange={(e) => setSpecialRequests(e.target.value)}
|
---|
| 149 | />
|
---|
| 150 | <br />
|
---|
[d24f17c] | 151 | </p>
|
---|
| 152 | <p className="card-text text-success">
|
---|
[f5b256e] | 153 | <strong>
|
---|
| 154 | Check-in Time: Grace period of 15 minutes +/- the slot. For more information, call the restaurant.
|
---|
| 155 | </strong>
|
---|
| 156 | <br />
|
---|
[d24f17c] | 157 | </p>
|
---|
| 158 | </div>
|
---|
| 159 | <div className="card-footer">
|
---|
| 160 | <button type="submit" className="btn btn-primary">Submit</button>
|
---|
[f5b256e] | 161 | <a href="/restaurants" className="btn btn-secondary mx-2">Back to Restaurants</a>
|
---|
| 162 | <a href="/" className="btn btn-secondary">Back to Home</a>
|
---|
[d24f17c] | 163 | </div>
|
---|
| 164 | </form>
|
---|
| 165 | </div>
|
---|
| 166 | </div>
|
---|
| 167 | </div>
|
---|
| 168 | </div>
|
---|
| 169 | );
|
---|
| 170 | };
|
---|
| 171 |
|
---|
[f5b256e] | 172 | export default ReservationConfirmation; |
---|