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