[d24f17c] | 1 | import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
|
---|
| 2 | import Customers from './components/Customers';
|
---|
| 3 | import Layout from "./components/Layout";
|
---|
| 4 | import React, {useEffect, useState} from 'react';
|
---|
| 5 | import CustomerFormContainer from "./components/CustomerFormContainer";
|
---|
| 6 | import CustomerDetails from "./components/CustomerDetails";
|
---|
| 7 | import ErrorPage from "./components/ErrorPage";
|
---|
| 8 | import Restaurants from "./components/Restaurants";
|
---|
| 9 | import Reservations from "./components/Reservations";
|
---|
| 10 | import RestaurantDetails from "./components/RestaurantDetails";
|
---|
| 11 | import ReservationConfirmation from "./components/ReservationConfirmation";
|
---|
| 12 | import ReservationEdit from "./components/ReservationEdit";
|
---|
[65b6638] | 13 | import axios from "axios";
|
---|
[d24f17c] | 14 |
|
---|
| 15 | const App = () => {
|
---|
| 16 | return (
|
---|
| 17 | <Router>
|
---|
| 18 | <Layout>
|
---|
| 19 | <Routes>
|
---|
| 20 | <Route path="/" element={<Home />} />
|
---|
| 21 | <Route path="/customers" element={<Customers />} />
|
---|
| 22 | <Route path="/customers/add" element={<CustomerFormContainer/>} />
|
---|
| 23 | <Route path="/customers/:id" element={<CustomerDetails />} />
|
---|
| 24 | <Route path="/customers/edit/:id" element={<CustomerFormContainer/>} />
|
---|
| 25 | <Route path="/restaurants" element={<Restaurants />} />
|
---|
| 26 | <Route path="/restaurants/:id" element={<RestaurantDetails />} />
|
---|
| 27 | <Route path="/reservations" element={<Reservations />} />
|
---|
| 28 | <Route path="/reservationConfirmation/:tableNumber/:timeSlot/:restaurantId" element={<ReservationConfirmation />} />
|
---|
| 29 | <Route path="/reservations/reservationEdit/:reservationId" element={<ReservationEdit />} />
|
---|
| 30 | <Route path="/error" element={<ErrorPage/>}/>
|
---|
| 31 | </Routes>
|
---|
| 32 | </Layout>
|
---|
| 33 | </Router>
|
---|
| 34 | );
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | const Home = () => {
|
---|
| 39 | const todayDate = new Date().toISOString().split('T')[0]; // Get today's date in 'YYYY-MM-DD' format
|
---|
| 40 |
|
---|
| 41 | const [date, setDate] = useState(todayDate);
|
---|
| 42 | const [selectedTime, setSelectedTime] = useState('');
|
---|
| 43 | const [numPeople, setNumPeople] = useState(2);
|
---|
| 44 | const [searchValue, setSearchValue] = useState('');
|
---|
| 45 | const [timeSlots, setTimeSlots] = useState([]);
|
---|
| 46 |
|
---|
| 47 | useEffect(() => {
|
---|
| 48 | if (date) {
|
---|
| 49 | const selectedDate = new Date(date);
|
---|
| 50 | const today = new Date();
|
---|
| 51 | const isToday = selectedDate.toDateString() === today.toDateString();
|
---|
| 52 |
|
---|
[65b6638] | 53 | // Determine the start hour and minute
|
---|
| 54 | let startHour = 9;
|
---|
| 55 | let startMinute = 0;
|
---|
| 56 | if (isToday) {
|
---|
| 57 | const currentHour = today.getHours();
|
---|
| 58 | const currentMinute = today.getMinutes();
|
---|
| 59 | // If current time is later than 09:00, start from the current hour and minute
|
---|
| 60 | if (currentHour > 9 || (currentHour === 9 && currentMinute >= 0)) {
|
---|
| 61 | startHour = currentHour;
|
---|
| 62 | startMinute = Math.ceil(currentMinute / 15) * 15;
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
[d24f17c] | 65 |
|
---|
[65b6638] | 66 | // Create the start time and end time
|
---|
| 67 | const startTime = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate(), startHour, startMinute);
|
---|
[d24f17c] | 68 | const endTime = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate(), 23, 30);
|
---|
| 69 |
|
---|
[65b6638] | 70 | // Generate time slots from start time to end time in 15-minute intervals
|
---|
[d24f17c] | 71 | const slots = [];
|
---|
[65b6638] | 72 | let currentTime = new Date(startTime);
|
---|
[d24f17c] | 73 | while (currentTime <= endTime) {
|
---|
| 74 | const option = currentTime.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: false });
|
---|
| 75 | slots.push(option);
|
---|
| 76 | currentTime.setMinutes(currentTime.getMinutes() + 15); // Increment by 15 minutes
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[65b6638] | 79 | // Update the timeSlots state
|
---|
[d24f17c] | 80 | setTimeSlots(slots);
|
---|
| 81 | }
|
---|
| 82 | }, [date]);
|
---|
| 83 |
|
---|
[65b6638] | 84 |
|
---|
[d24f17c] | 85 | const handleDateChange = (e) => {
|
---|
| 86 | setDate(e.target.value);
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | const handleTimeChange = (e) => {
|
---|
| 90 | setSelectedTime(e.target.value);
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 | const handleNumPeopleChange = (e) => {
|
---|
| 94 | setNumPeople(e.target.value);
|
---|
| 95 | };
|
---|
| 96 |
|
---|
| 97 | const handleInputChange = (event) => {
|
---|
| 98 | setSearchValue(event.target.value);
|
---|
| 99 | };
|
---|
| 100 |
|
---|
[65b6638] | 101 | const handleSubmit = async (e) => {
|
---|
[d24f17c] | 102 | e.preventDefault();
|
---|
[65b6638] | 103 | const [year, month, day] = date.split("-");
|
---|
| 104 |
|
---|
| 105 | let formattedDateTime;
|
---|
| 106 | const [selectedHours, selectedMinutes] = selectedTime.split(":");
|
---|
| 107 | // Check if selectedHours and selectedMinutes are valid numbers
|
---|
| 108 | if (!isNaN(selectedHours) && !isNaN(selectedMinutes)) {
|
---|
| 109 | const dateTime = new Date(Date.UTC(year, month - 1, day, selectedHours, selectedMinutes));
|
---|
| 110 | formattedDateTime = dateTime.toISOString().slice(0, 16).replace('T', ' ');
|
---|
| 111 | } else {
|
---|
| 112 | // Default values if selectedTime is not valid
|
---|
| 113 | const now = new Date();
|
---|
| 114 | let defaultTime;
|
---|
| 115 | if (now.getHours() >= 9 && now.getHours() <= 23) {
|
---|
| 116 | defaultTime = now;
|
---|
| 117 | } else {
|
---|
| 118 | defaultTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 9, 0); // Set to 09:00 if current time is before 09:00
|
---|
| 119 | }
|
---|
| 120 | const dateTime = new Date(Date.UTC(year, month - 1, day, defaultTime.getHours(), defaultTime.getMinutes()));
|
---|
| 121 | formattedDateTime = dateTime.toISOString().slice(0, 16).replace('T', ' ');
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | const data = {
|
---|
| 125 | dateTime: formattedDateTime,
|
---|
| 126 | partySize: numPeople,
|
---|
| 127 | search: searchValue
|
---|
| 128 | };
|
---|
| 129 |
|
---|
| 130 | console.log("pecatam data pod mene")
|
---|
| 131 | console.log(data)
|
---|
| 132 |
|
---|
| 133 | try {
|
---|
| 134 | const response = await axios.post('http://localhost:8080/api/search', data);
|
---|
| 135 | const filteredRestaurants = response.data;
|
---|
| 136 | console.log(filteredRestaurants);
|
---|
| 137 | // Handle response accordingly
|
---|
| 138 | } catch (error) {
|
---|
| 139 | console.error('Error:', error);
|
---|
| 140 | }
|
---|
[d24f17c] | 141 | };
|
---|
| 142 |
|
---|
[65b6638] | 143 |
|
---|
| 144 |
|
---|
| 145 | // Rest of your component code...
|
---|
| 146 |
|
---|
| 147 | const today = new Date();
|
---|
| 148 | const year = today.getFullYear();
|
---|
| 149 | const month = String(today.getMonth() + 1).padStart(2, '0');
|
---|
| 150 | const day = String(today.getDate()).padStart(2, '0');
|
---|
| 151 | const formattedDate = `${year}-${month}-${day}`;
|
---|
| 152 |
|
---|
[d24f17c] | 153 |
|
---|
| 154 | return (
|
---|
| 155 | <div className="container">
|
---|
| 156 | <h2>Home</h2>
|
---|
| 157 | <p>Welcome to My Awesome App!</p>
|
---|
| 158 | <form className="row g-2 align-items-center" onSubmit={handleSubmit}>
|
---|
| 159 | <div className="col-auto">
|
---|
| 160 | <input className="form-control me-2" type="date" value={date} onChange={handleDateChange}
|
---|
[65b6638] | 161 | min={formattedDate}/>
|
---|
[d24f17c] | 162 | </div>
|
---|
| 163 | <div className="col-auto">
|
---|
| 164 | <select className="form-select" onChange={handleTimeChange}>
|
---|
| 165 | {timeSlots.map((slot, index) => (
|
---|
| 166 | <option key={index} value={slot}>{slot}</option>
|
---|
| 167 | ))}
|
---|
| 168 | </select>
|
---|
| 169 | </div>
|
---|
| 170 | <div className="col-auto">
|
---|
| 171 | <select className="form-select" value={numPeople} onChange={handleNumPeopleChange}>
|
---|
| 172 | {[...Array(20).keys()].map((num) => (
|
---|
| 173 | <option key={num + 1} value={num + 1}>{num + 1}</option>
|
---|
| 174 | ))}
|
---|
| 175 | </select>
|
---|
| 176 | </div>
|
---|
| 177 | <div className="col-auto">
|
---|
| 178 | <input
|
---|
| 179 | className="form-control me-2"
|
---|
| 180 | type="search"
|
---|
| 181 | name="search"
|
---|
| 182 | placeholder="Restaurant or Cuisine"
|
---|
| 183 | aria-label="Search"
|
---|
| 184 | value={searchValue} // Set the value of the input field
|
---|
| 185 | onChange={handleInputChange} // Call the event handler on change
|
---|
| 186 | />
|
---|
| 187 | </div>
|
---|
| 188 | <div className="col-auto">
|
---|
| 189 | <button className="btn btn-outline-success" type="submit">Search</button>
|
---|
| 190 | </div>
|
---|
| 191 | </form>
|
---|
| 192 | </div>
|
---|
| 193 | );
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 |
|
---|
| 197 | export default App;
|
---|