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