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';
|
---|
5 |
|
---|
6 | const ReservationConfirmation = () => {
|
---|
7 | const navigate = useNavigate();
|
---|
8 |
|
---|
9 | const [restaurant, setRestaurant] = useState({});
|
---|
10 | const [table, setTable] = useState({});
|
---|
11 | const [reservationDateTime, setReservationDateTime] = useState('');
|
---|
12 | const [partySize, setPartySize] = useState('');
|
---|
13 | const [specialRequests, setSpecialRequests] = useState('');
|
---|
14 | const { tableNumber, timeSlot, restaurantId } = useParams();
|
---|
15 |
|
---|
16 | useEffect(() => {
|
---|
17 | const fetchTableDetails = async () => {
|
---|
18 | try {
|
---|
19 | const tableResponse = await axios.get(`http://localhost:8080/api/tables/${tableNumber}`);
|
---|
20 | setTable(tableResponse.data);
|
---|
21 |
|
---|
22 | const restaurantResponse = await axios.get(`http://localhost:8080/api/restaurants/${restaurantId}`);
|
---|
23 | setRestaurant(restaurantResponse.data);
|
---|
24 | } catch (error) {
|
---|
25 | console.error('Error fetching table details:', error);
|
---|
26 | }
|
---|
27 | };
|
---|
28 | fetchTableDetails();
|
---|
29 | }, [tableNumber, restaurantId]);
|
---|
30 |
|
---|
31 | const handleSubmit = async (e) => {
|
---|
32 | e.preventDefault();
|
---|
33 | // Handle form submission here
|
---|
34 | try {
|
---|
35 | // Check if restaurant and table are valid
|
---|
36 | if (!restaurant || !table) {
|
---|
37 | console.error("Restaurant or table is missing.");
|
---|
38 | return;
|
---|
39 | }
|
---|
40 |
|
---|
41 | const response = await axios.post(`http://localhost:8080/api/reservations`, {
|
---|
42 | restaurant: restaurant, // Assuming restaurant has an 'id' property
|
---|
43 | table: table, // Assuming table has an 'id' property
|
---|
44 | checkInTime: timeSlot, // Fill in as needed
|
---|
45 | partySize: partySize,
|
---|
46 | specialRequests: specialRequests
|
---|
47 | });
|
---|
48 | // Handle successful reservation creation
|
---|
49 | // console.log("Reservation created:", response.data);
|
---|
50 | navigate("/reservations")
|
---|
51 | } catch (error) {
|
---|
52 | console.error("Error creating reservation:", error);
|
---|
53 | }
|
---|
54 | };
|
---|
55 |
|
---|
56 | const initialRemainingTime = localStorage.getItem('remainingTime') || 300;
|
---|
57 | const [remainingTime, setRemainingTime] = useState(parseInt(initialRemainingTime));
|
---|
58 |
|
---|
59 | useEffect(() => {
|
---|
60 | const timer = setInterval(() => {
|
---|
61 | setRemainingTime((prevTime) => {
|
---|
62 | const newTime = prevTime - 1;
|
---|
63 | localStorage.setItem('remainingTime', newTime.toString()); // Update remaining time in localStorage
|
---|
64 | return newTime;
|
---|
65 | });
|
---|
66 | }, 1000);
|
---|
67 |
|
---|
68 | return () => clearInterval(timer);
|
---|
69 | }, []);
|
---|
70 |
|
---|
71 | useEffect(() => {
|
---|
72 | // Reset remaining time if it reaches zero
|
---|
73 | if (remainingTime <= 0) {
|
---|
74 | localStorage.removeItem('remainingTime');
|
---|
75 | // Optionally, handle releasing the hold on the table
|
---|
76 | }
|
---|
77 | }, [remainingTime]);
|
---|
78 |
|
---|
79 | const formatTime = (timeInSeconds) => {
|
---|
80 | const minutes = Math.floor(timeInSeconds / 60);
|
---|
81 | const seconds = timeInSeconds % 60;
|
---|
82 | return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
|
---|
83 | };
|
---|
84 |
|
---|
85 | return (
|
---|
86 | <div className="container mt-5">
|
---|
87 | <div className="row justify-content-center">
|
---|
88 | <div className="col-md-6">
|
---|
89 | <div className="card">
|
---|
90 | <div className="card-header">
|
---|
91 | <h3 className="text-center">Reservation Confirmation</h3>
|
---|
92 | <p>Remaining Time: {formatTime(remainingTime)}</p>
|
---|
93 | </div>
|
---|
94 | <form onSubmit={handleSubmit}>
|
---|
95 | <div className="card-body">
|
---|
96 | <h5 className="card-title">Reservation Details</h5>
|
---|
97 | <p className="card-text">
|
---|
98 | <strong>Restaurant:</strong> {restaurant.name || 'Loading...'} <br/>
|
---|
99 | <strong>Cuisine type:</strong> {restaurant.cuisineType || 'Loading...'} <br/>
|
---|
100 | <strong>Selected Time Slot:</strong> {timeSlot} <br/>
|
---|
101 | <strong>Party size:</strong> <input type="number" max={table.capacity}
|
---|
102 | value={partySize}
|
---|
103 | onChange={(e) => setPartySize(e.target.value)}/>
|
---|
104 | <strong>Table size:</strong> {table.capacity} <br/>
|
---|
105 | <strong>Special Requests:</strong> <input type="text" value={specialRequests}
|
---|
106 | onChange={(e) => setSpecialRequests(e.target.value)}/><br/>
|
---|
107 | </p>
|
---|
108 | <p className="card-text text-success">
|
---|
109 | <strong>Check-in Time: grace period of 15 minutes +- the slot, for more call the
|
---|
110 | restaurant</strong><br/>
|
---|
111 | </p>
|
---|
112 | </div>
|
---|
113 | <div className="card-footer">
|
---|
114 | <button type="submit" className="btn btn-primary">Submit</button>
|
---|
115 | </div>
|
---|
116 | <div className="card-footer">
|
---|
117 | <a href="/restaurants" className="btn btn-primary">Back to Restaurants</a>
|
---|
118 | </div>
|
---|
119 | <div className="card-footer">
|
---|
120 | <a href="/" className="btn btn-primary">Back to Home</a>
|
---|
121 | </div>
|
---|
122 | </form>
|
---|
123 | </div>
|
---|
124 | </div>
|
---|
125 |
|
---|
126 | </div>
|
---|
127 | </div>
|
---|
128 | );
|
---|
129 | };
|
---|
130 |
|
---|
131 | export default ReservationConfirmation;
|
---|