source: my-react-app/src/App.js@ 65b6638

main
Last change on this file since 65b6638 was 65b6638, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

RetaurantServiceImpl problemi
isAvailable od tableEntity...

  • Property mode set to 100644
File size: 8.0 KB
Line 
1import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
2import Customers from './components/Customers';
3import Layout from "./components/Layout";
4import React, {useEffect, useState} from 'react';
5import CustomerFormContainer from "./components/CustomerFormContainer";
6import CustomerDetails from "./components/CustomerDetails";
7import ErrorPage from "./components/ErrorPage";
8import Restaurants from "./components/Restaurants";
9import Reservations from "./components/Reservations";
10import RestaurantDetails from "./components/RestaurantDetails";
11import ReservationConfirmation from "./components/ReservationConfirmation";
12import ReservationEdit from "./components/ReservationEdit";
13import axios from "axios";
14
15const 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
38const 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
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 }
65
66 // Create the start time and end time
67 const startTime = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate(), startHour, startMinute);
68 const endTime = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate(), 23, 30);
69
70 // Generate time slots from start time to end time in 15-minute intervals
71 const slots = [];
72 let currentTime = new Date(startTime);
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
79 // Update the timeSlots state
80 setTimeSlots(slots);
81 }
82 }, [date]);
83
84
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
101 const handleSubmit = async (e) => {
102 e.preventDefault();
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 }
141 };
142
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
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}
161 min={formattedDate}/>
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
197export default App;
Note: See TracBrowser for help on using the repository browser.