import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Customers from './components/Customers';
import Layout from "./components/Layout";
import React, {useContext, useEffect, useState} from 'react';
import CustomerFormContainer from "./components/CustomerFormContainer";
import CustomerDetails from "./components/CustomerDetails";
import ErrorPage from "./components/ErrorPage";
import Restaurants from "./components/Restaurants";
import Reservations from "./components/Reservations";
import RestaurantDetails from "./components/RestaurantDetails";
import ReservationConfirmation from "./components/ReservationConfirmation";
import ReservationEdit from "./components/ReservationEdit";
import axios from "axios";
import { CuisineContext } from './components/CuisineContext';
const App = () => {
return (
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
}/>
);
}
const Home = () => {
const todayDate = new Date().toISOString().split('T')[0]; // Get today's date in 'YYYY-MM-DD' format
const [date, setDate] = useState(todayDate);
const [selectedTime, setSelectedTime] = useState('');
const [numPeople, setNumPeople] = useState(2);
const [searchValue, setSearchValue] = useState('');
const [timeSlots, setTimeSlots] = useState([]);
const cuisineTypes = useContext(CuisineContext);
useEffect(() => {
if (date) {
const selectedDate = new Date(date);
const today = new Date();
const isToday = selectedDate.toDateString() === today.toDateString();
// Determine the start hour and minute
let startHour = 9;
let startMinute = 0;
if (isToday) {
const currentHour = today.getHours();
const currentMinute = today.getMinutes();
// If current time is later than 09:00, start from the current hour and minute
if (currentHour > 9 || (currentHour === 9 && currentMinute >= 0)) {
startHour = currentHour;
startMinute = Math.ceil(currentMinute / 15) * 15;
}
}
// Create the start time and end time
const startTime = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate(), startHour, startMinute);
const endTime = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate(), 23, 30);
// Generate time slots from start time to end time in 15-minute intervals
const slots = [];
let currentTime = new Date(startTime);
while (currentTime <= endTime) {
const option = currentTime.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: false });
slots.push(option);
currentTime.setMinutes(currentTime.getMinutes() + 15); // Increment by 15 minutes
}
// Update the timeSlots state
setTimeSlots(slots);
}
}, [date]);
const handleDateChange = (e) => {
setDate(e.target.value);
};
const handleTimeChange = (e) => {
setSelectedTime(e.target.value);
};
const handleNumPeopleChange = (e) => {
setNumPeople(e.target.value);
};
const handleInputChange = (event) => {
setSearchValue(event.target.value);
};
const handleSubmit = async (e) => {
e.preventDefault();
const [year, month, day] = date.split("-");
let formattedDateTime;
if (selectedTime) {
const [selectedHours, selectedMinutes] = selectedTime.split(":");
// Check if selectedHours and selectedMinutes are valid numbers
if (!isNaN(selectedHours) && !isNaN(selectedMinutes)) {
const dateTime = new Date(Date.UTC(year, month - 1, day, selectedHours, selectedMinutes));
formattedDateTime = dateTime.toISOString().slice(0, 16).replace('T', ' ');
}
} else {
// Find the first available time slot after the current time
const now = new Date();
const currentTime = now.getHours() * 60 + now.getMinutes(); // Current time in minutes
const nextSlot = timeSlots.find(slot => {
const [hours, minutes] = slot.split(":");
const slotTime = parseInt(hours) * 60 + parseInt(minutes); // Time of the slot in minutes
return slotTime > currentTime;
});
// If no slot is found after the current time, use the first slot of the day
formattedDateTime = nextSlot ? `${date} ${nextSlot}` : `${date} ${timeSlots[0]}`;
}
const data = {
dateTime: formattedDateTime,
partySize: numPeople,
search: searchValue
};
console.log("Data to be submitted:");
console.log(data);
try {
const response = await axios.post('http://localhost:8080/api/search', data);
const filteredRestaurants = response.data;
console.log(filteredRestaurants);
// Handle response accordingly
} catch (error) {
console.error('Error:', error);
}
};
const handleSearchByCuisine = async (cuisine) => {
const cuisineName = cuisine.replace('Searching by cuisine: ', '');
try {
const response = await axios.post(`http://localhost:8080/api/search/shortcut/${cuisineName}`, cuisineName);
// Handle the response as needed
console.log(response.data); // Log the response data, for example
} catch (error) {
console.error('Error searching by cuisine:', error);
// Handle errors, such as displaying an error message to the user
}
};
// Rest of your component code...
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
const formattedDate = `${year}-${month}-${day}`;
return (
);
}
export default App;