Changeset 9293100 for my-react-app
- Timestamp:
- 03/03/24 13:59:59 (15 months ago)
- Branches:
- main
- Children:
- e35d0e9
- Parents:
- c63036a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
my-react-app/src/App.js
rc63036a r9293100 13 13 import axios from "axios"; 14 14 import { CuisineContext } from './components/CuisineContext'; 15 import RestaurantInfo from "./components/RestaurantInfo"; 15 16 16 17 const App = () => { … … 42 43 const [date, setDate] = useState(todayDate); 43 44 const [selectedTime, setSelectedTime] = useState(''); 45 const [formatedDateTime, setFormatedDateTime] = useState('') 44 46 const [numPeople, setNumPeople] = useState(2); 45 47 const [searchValue, setSearchValue] = useState(''); 46 48 const [timeSlots, setTimeSlots] = useState([]); 47 49 50 let [filteredRestaurants, setFilteredRestaurants] = useState([]); 48 51 const cuisineTypes = useContext(CuisineContext); 52 const [showCuisineSearch, setShowCuisineSearch] = useState(true); 49 53 50 54 useEffect(() => { … … 112 116 const dateTime = new Date(Date.UTC(year, month - 1, day, selectedHours, selectedMinutes)); 113 117 formattedDateTime = dateTime.toISOString().slice(0, 16).replace('T', ' '); 118 setFormatedDateTime(formattedDateTime); 114 119 } 115 120 } else { … … 139 144 const response = await axios.post('http://localhost:8080/api/search', data); 140 145 const filteredRestaurants = response.data; 146 setFilteredRestaurants(filteredRestaurants); 141 147 console.log(filteredRestaurants); 148 setShowCuisineSearch(false); 142 149 // Handle response accordingly 143 150 } catch (error) { … … 150 157 try { 151 158 const response = await axios.post(`http://localhost:8080/api/search/shortcut/${cuisineName}`, cuisineName); 152 // Handle the response as needed153 console.log(response.data); // Log the response data, for example159 console.log(response.data); 160 setFilteredRestaurants(response.data) 154 161 } catch (error) { 155 162 console.error('Error searching by cuisine:', error); 156 // Handle errors, such as displaying an error message to the user 157 } 158 }; 163 } 164 setShowCuisineSearch(false); 165 }; 166 167 const renderTimeSlots = (tablesList, restaurant) => { 168 const [year, month, day] = date.split("-"); 169 const [hours, minutes] = selectedTime.split(":"); 170 const dateTime = new Date(year, month - 1, day, hours, minutes); // month is zero-based 171 172 let timestamp = dateTime.getTime(); 173 if (isNaN(timestamp)) { 174 timestamp = Date.now(); 175 } 176 177 let renderedTimeSlots = {}; // Object to store rendered slots for each table capacity 178 179 return tablesList.flatMap(table => { 180 // Render capacity header when encountering a new capacity 181 if (!renderedTimeSlots[table.capacity]) { 182 renderedTimeSlots[table.capacity] = 0; 183 return ( 184 <div key={table.capacity}> 185 <h3>Table for: {table.capacity}</h3> 186 {table.timeSlots.map((timeSlot, index) => { 187 const timeSlotTime = new Date(timeSlot).getTime(); 188 const tableCapacity = table.capacity; 189 // Check if the time slot is after the current time, numPeople is less than or equal to tableCapacity, and limit to 5 slots 190 if (timeSlotTime > timestamp && numPeople <= tableCapacity && renderedTimeSlots[tableCapacity] < 5) { 191 renderedTimeSlots[tableCapacity]++; 192 const timeSlotDateTime = new Date(timeSlot); 193 const formattedDateTime = timeSlotDateTime.toLocaleString(); // Format for both date and time 194 195 return ( 196 <button key={index} className="btn btn-primary me-2 mb-2" onClick={() => handleTimeSlotClick(table, timeSlot, restaurant)}> 197 {formattedDateTime} {/* Display both date and time */} 198 </button> 199 ); 200 } else { 201 return null; // Render nothing if the condition is not met 202 } 203 })} 204 </div> 205 ); 206 } else { 207 // If capacity has been rendered, return null to avoid duplicate rendering 208 return null; 209 } 210 }); 211 } 212 159 213 160 214 … … 204 258 <button className="btn btn-outline-success" type="submit">Search</button> 205 259 </div> 206 <form> 260 261 <div> 262 {filteredRestaurants.map((restaurant) => ( 263 <div key={restaurant.id} className="card mb-3"> 264 <div className="card-body"> 265 <RestaurantInfo key={restaurant.id} restaurant={restaurant}/> 266 <p>Available time slots</p> 267 <div className="d-flex flex-wrap"> 268 {renderTimeSlots(restaurant.tablesList.flatMap((table) => table), restaurant)} 269 </div> 270 </div> 271 </div> 272 ))} 273 </div> 274 275 276 {showCuisineSearch && ( 207 277 <div className="mb-3"> 208 278 <h2 className="display-2">Search by cuisine type</h2> 209 <ul className="list-group"> 210 {cuisineTypes.map((cuisine, index) => ( 211 <li key={index} className="list-group-item"> 212 <button type="button" className="btn btn-outline-primary" 213 onClick={() => handleSearchByCuisine(cuisine)}> 214 {cuisine} 215 </button> 216 </li> 217 ))} 218 </ul> 219 </div> 220 </form> 221 222 279 <ul className="list-group"> 280 {cuisineTypes.map((cuisine, index) => ( 281 <li key={index} className="list-group-item"> 282 <button type="button" className="btn btn-outline-primary" 283 onClick={() => handleSearchByCuisine(cuisine)}> 284 {cuisine} 285 </button> 286 </li> 287 ))} 288 </ul> 289 </div>)} 223 290 </form> 224 291 </div>
Note:
See TracChangeset
for help on using the changeset viewer.