1 | import React, {useState, useEffect, useContext} from 'react';
|
---|
2 | import 'bootstrap/dist/css/bootstrap.min.css';
|
---|
3 | import StarRating from './StarRating';
|
---|
4 | import { useNavigate } from 'react-router-dom';
|
---|
5 | import {RestaurantContext} from "./RestaurantContext";
|
---|
6 |
|
---|
7 | const Restaurants = () => {
|
---|
8 | const [restaurants, setRestaurants] = useState([]);
|
---|
9 | const navigate = useNavigate();
|
---|
10 | const restaurantContext = useContext(RestaurantContext);
|
---|
11 |
|
---|
12 | useEffect(() => {
|
---|
13 | setRestaurants(restaurantContext.restaurants);
|
---|
14 | }, [restaurantContext]);
|
---|
15 |
|
---|
16 | const handleDetailClick = (restaurantId) => {
|
---|
17 | navigate(`/restaurants/${restaurantId}`);
|
---|
18 | }
|
---|
19 |
|
---|
20 | const handleTimeSlotClick = (table, timeSlot, restaurant) => {
|
---|
21 | const tableNumber = table.id;
|
---|
22 | const formattedTimeSlot = timeSlot;
|
---|
23 | const restaurantId = restaurant.restaurantId;
|
---|
24 |
|
---|
25 | const encodedTableNumber = encodeURI(tableNumber);
|
---|
26 | const encodedTimeSlot = encodeURIComponent(formattedTimeSlot);
|
---|
27 | const encodedRestaurantId = encodeURIComponent(restaurantId);
|
---|
28 |
|
---|
29 | navigate(`/reservationConfirmation/${encodedTableNumber}/${encodedTimeSlot}/${encodedRestaurantId}`);
|
---|
30 | }
|
---|
31 |
|
---|
32 | const renderTimeSlots = (tablesList, restaurant) => {
|
---|
33 | const currentTime = new Date().getTime();
|
---|
34 | let renderedTimeSlots = {}; // Object to store rendered slots for each table capacity
|
---|
35 |
|
---|
36 | return tablesList.flatMap(table => {
|
---|
37 | // Render capacity header when encountering a new capacity
|
---|
38 | if (!renderedTimeSlots[table.capacity]) {
|
---|
39 | renderedTimeSlots[table.capacity] = 0;
|
---|
40 | return (
|
---|
41 | <div key={table.capacity}>
|
---|
42 | <h3>Table for: {table.capacity}</h3>
|
---|
43 | {table.timeSlots.map((timeSlot, index) => {
|
---|
44 | const timeSlotTime = new Date(timeSlot).getTime();
|
---|
45 | const tableCapacity = table.capacity;
|
---|
46 |
|
---|
47 | // Check if the time slot is after the current time and limit to 3 slots
|
---|
48 | if (timeSlotTime > currentTime && renderedTimeSlots[tableCapacity] < 3) {
|
---|
49 | renderedTimeSlots[tableCapacity]++;
|
---|
50 | const timeSlotDateTime = new Date(timeSlot);
|
---|
51 | const formattedTime = timeSlotDateTime.toLocaleTimeString();
|
---|
52 | const formattedDateTime = timeSlotDateTime.toLocaleString(); // Format for both date and time
|
---|
53 |
|
---|
54 | return (
|
---|
55 | <button key={index} className="btn btn-primary me-2 mb-2" onClick={() => handleTimeSlotClick(table, timeSlot, restaurant)}>
|
---|
56 | {formattedDateTime} {/* Display both date and time */}
|
---|
57 | </button>
|
---|
58 | );
|
---|
59 | } else {
|
---|
60 | return null; // Render nothing if the condition is not met
|
---|
61 | }
|
---|
62 | })}
|
---|
63 | </div>
|
---|
64 | );
|
---|
65 | } else {
|
---|
66 | // If capacity has been rendered, return null to avoid duplicate rendering
|
---|
67 | return null;
|
---|
68 | }
|
---|
69 | });
|
---|
70 |
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | return (
|
---|
75 | <div className="container">
|
---|
76 | <h2>Restaurants</h2>
|
---|
77 | <div className="row">
|
---|
78 | {restaurants.map((restaurant) => (
|
---|
79 | <div key={restaurant.id} className="col-md-4 mb-4">
|
---|
80 | <div className="card">
|
---|
81 | <div className="card-body">
|
---|
82 | <h5 className="card-title">
|
---|
83 | {restaurant.name} <StarRating key={restaurant.id} rating={restaurant.rating}/>
|
---|
84 | </h5>
|
---|
85 | <p className="card-text">{restaurant.cuisineType}</p>
|
---|
86 | <p className="card-text">{restaurant.operatingHours}</p>
|
---|
87 | <p className="card-text">Ul. {restaurant.address}</p>
|
---|
88 | <div className="d-flex flex-wrap">
|
---|
89 | {renderTimeSlots(restaurant.tablesList.flatMap((table) => table), restaurant)}
|
---|
90 | </div>
|
---|
91 | </div>
|
---|
92 | <button onClick={() => handleDetailClick(restaurant.restaurantId)}
|
---|
93 | className="btn btn-primary">View Details
|
---|
94 | </button>
|
---|
95 | </div>
|
---|
96 | </div>
|
---|
97 | ))}
|
---|
98 | </div>
|
---|
99 | </div>
|
---|
100 | );
|
---|
101 | };
|
---|
102 |
|
---|
103 | export default Restaurants; |
---|