1 | import React, { useState, useEffect } from 'react';
|
---|
2 | import axios from 'axios';
|
---|
3 | import 'bootstrap/dist/css/bootstrap.min.css';
|
---|
4 | import {useNavigate} from "react-router-dom";
|
---|
5 | import {jwtDecode} from "jwt-decode";
|
---|
6 |
|
---|
7 |
|
---|
8 | const Reservations = () => {
|
---|
9 | const navigate = useNavigate();
|
---|
10 |
|
---|
11 | const [reservations, setReservations] = useState([]);
|
---|
12 |
|
---|
13 | useEffect(() => {
|
---|
14 | const fetchReservations = async () => {
|
---|
15 | try {
|
---|
16 | const token = localStorage.getItem("token");
|
---|
17 | if (!token) {
|
---|
18 | console.error("No token found");
|
---|
19 | return;
|
---|
20 | }
|
---|
21 | const decodedToken = jwtDecode(token);
|
---|
22 | console.log(decodedToken)
|
---|
23 | const userId = decodedToken.iss;
|
---|
24 |
|
---|
25 | const response = await axios.get(`http://localhost:8081/api/reservations/by/${userId}`);
|
---|
26 | setReservations(response.data);
|
---|
27 | } catch (error) {
|
---|
28 | console.error('Error fetching reservations:', error);
|
---|
29 | }
|
---|
30 | };
|
---|
31 |
|
---|
32 | fetchReservations();
|
---|
33 | }, []);
|
---|
34 |
|
---|
35 | const handleEditReservation = async (reservationId) => {
|
---|
36 | if(reservationId!=null) {
|
---|
37 | navigate(`/reservations/reservationEdit/${reservationId}`);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | const handleCancelReservation = async (reservationID) => {
|
---|
42 | try {
|
---|
43 | await axios.delete(`http://localhost:8081/api/reservations/delete/${reservationID}`);
|
---|
44 | setReservations(reservations.filter(reservation => reservation.reservationID !== reservationID));
|
---|
45 | alert('Reservation canceled successfully!');
|
---|
46 | } catch (error) {
|
---|
47 | console.error('Error canceling reservation:', error);
|
---|
48 | alert('An error occurred while canceling the reservation. Please try again later.');
|
---|
49 | }
|
---|
50 | };
|
---|
51 |
|
---|
52 |
|
---|
53 | return (
|
---|
54 | <div className="container">
|
---|
55 | <h2>Reservations</h2>
|
---|
56 | <div className="row">
|
---|
57 | {reservations.map(reservation => (
|
---|
58 | <div key={reservation.reservationID} className="col-md-4 mb-4">
|
---|
59 | <div className="card">
|
---|
60 | <div className="card-body">
|
---|
61 | <div className="card-title">Reservation ID: {reservation.reservationID}</div>
|
---|
62 | <div className="card-text">Restaurant: {reservation.restaurant.name}</div>
|
---|
63 | <div className="card-text">Table Number: {reservation.table.id}</div>
|
---|
64 | {/* Format reservation date and time */}
|
---|
65 | <div className="card-text">Reservation
|
---|
66 | Date: {new Date(reservation.checkInTime).toLocaleDateString('en-US', {
|
---|
67 | weekday: 'long',
|
---|
68 | year: 'numeric',
|
---|
69 | month: 'long',
|
---|
70 | day: 'numeric'
|
---|
71 | })}</div>
|
---|
72 | <div className="card-text">Reservation
|
---|
73 | Time: {new Date(reservation.checkInTime).toLocaleTimeString('en-US', {
|
---|
74 | hour: 'numeric',
|
---|
75 | minute: 'numeric',
|
---|
76 | hour12: true
|
---|
77 | })}</div>
|
---|
78 | {/* End of formatted date and time */}
|
---|
79 | <div className="card-text">Party Size: {reservation.partySize}</div>
|
---|
80 | <div className="card-text text-danger">Special
|
---|
81 | Requests: {reservation.specialRequests}</div>
|
---|
82 | <div className="card-text">Status: {reservation.status}</div>
|
---|
83 | <div className="card-text text-danger">Grace period of 15 minutes +-</div>
|
---|
84 | <br/>
|
---|
85 | <div className="row">
|
---|
86 | <div className="col">
|
---|
87 | <button className="danger text-bg-warning border-0"
|
---|
88 | onClick={() => handleEditReservation(reservation.reservationID)}>Edit
|
---|
89 | Reservation
|
---|
90 | </button>
|
---|
91 | </div>
|
---|
92 | <div className="col">
|
---|
93 | <button className="danger text-bg-danger border-0"
|
---|
94 | onClick={() => handleCancelReservation(reservation.reservationID)}>Cancel
|
---|
95 | Reservation
|
---|
96 | </button>
|
---|
97 | </div>
|
---|
98 | </div>
|
---|
99 | </div>
|
---|
100 | </div>
|
---|
101 | </div>
|
---|
102 | ))}
|
---|
103 | </div>
|
---|
104 | </div>
|
---|
105 | );
|
---|
106 | p
|
---|
107 |
|
---|
108 | };
|
---|
109 |
|
---|
110 | export default Reservations;
|
---|