Changeset cfc16a3 for my-react-app
- Timestamp:
- 03/03/24 10:52:49 (15 months ago)
- Branches:
- main
- Children:
- c63036a
- Parents:
- 75f5086
- Location:
- my-react-app/src
- Files:
-
- 3 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
my-react-app/src/App.js
r75f5086 rcfc16a3 2 2 import Customers from './components/Customers'; 3 3 import Layout from "./components/Layout"; 4 import React, {use Effect, useState} from 'react';4 import React, {useContext, useEffect, useState} from 'react'; 5 5 import CustomerFormContainer from "./components/CustomerFormContainer"; 6 6 import CustomerDetails from "./components/CustomerDetails"; … … 12 12 import ReservationEdit from "./components/ReservationEdit"; 13 13 import axios from "axios"; 14 import { CuisineContext } from './components/CuisineContext'; 14 15 15 16 const App = () => { … … 45 46 const [timeSlots, setTimeSlots] = useState([]); 46 47 48 const cuisineTypes = useContext(CuisineContext); 49 47 50 useEffect(() => { 48 51 if (date) { … … 82 85 }, [date]); 83 86 84 85 87 const handleDateChange = (e) => { 86 88 setDate(e.target.value); … … 102 104 e.preventDefault(); 103 105 const [year, month, day] = date.split("-"); 104 105 106 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', ' '); 107 108 if (selectedTime) { 109 const [selectedHours, selectedMinutes] = selectedTime.split(":"); 110 // Check if selectedHours and selectedMinutes are valid numbers 111 if (!isNaN(selectedHours) && !isNaN(selectedMinutes)) { 112 const dateTime = new Date(Date.UTC(year, month - 1, day, selectedHours, selectedMinutes)); 113 formattedDateTime = dateTime.toISOString().slice(0, 16).replace('T', ' '); 114 } 111 115 } else { 112 // Default values if selectedTime is not valid116 // Find the first available time slot after the current time 113 117 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', ' '); 118 const currentTime = now.getHours() * 60 + now.getMinutes(); // Current time in minutes 119 const nextSlot = timeSlots.find(slot => { 120 const [hours, minutes] = slot.split(":"); 121 const slotTime = parseInt(hours) * 60 + parseInt(minutes); // Time of the slot in minutes 122 return slotTime > currentTime; 123 }); 124 125 // If no slot is found after the current time, use the first slot of the day 126 formattedDateTime = nextSlot ? `${date} ${nextSlot}` : `${date} ${timeSlots[0]}`; 122 127 } 123 128 … … 128 133 }; 129 134 130 console.log(" pecatam data pod mene")131 console.log(data) 135 console.log("Data to be submitted:"); 136 console.log(data); 132 137 133 138 try { … … 141 146 }; 142 147 148 const handleSearchByCuisine = async (cuisine) => { 149 const cuisineName = cuisine.replace('Searching by cuisine: ', ''); 150 try { 151 const response = await axios.post(`http://localhost:8080/api/search/shortcut/${cuisineName}`, cuisineName); 152 // Handle the response as needed 153 console.log(response.data); // Log the response data, for example 154 } catch (error) { 155 console.error('Error searching by cuisine:', error); 156 // Handle errors, such as displaying an error message to the user 157 } 158 }; 143 159 144 160 … … 154 170 return ( 155 171 <div className="container"> 156 <h2>Home</h2> 157 <p>Welcome to My Awesome App!</p> 172 <h2 className="display-1">Rezerviraj masa</h2> 158 173 <form className="row g-2 align-items-center" onSubmit={handleSubmit}> 159 174 <div className="col-auto"> … … 189 204 <button className="btn btn-outline-success" type="submit">Search</button> 190 205 </div> 206 <form> 207 <div className="mb-3"> 208 <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 191 223 </form> 192 224 </div> -
my-react-app/src/components/Customers.js
r75f5086 rcfc16a3 35 35 await axios.delete(`http://localhost:8080/api/customers/delete/${customerId}`); 36 36 setCustomers(customers.filter(customer => customer.customerID !== customerId)); 37 alert('Reservation canceled successfully');37 window.location.reload(); 38 38 } catch (error) { 39 39 console.error("Error + " + error); 40 alert("An error occurred while deleting");41 40 } 42 41 } -
my-react-app/src/components/Restaurants.js
r75f5086 rcfc16a3 1 import React, { useState, useEffect } from 'react'; 2 import axios from 'axios'; 1 import React, {useState, useEffect, useContext} from 'react'; 3 2 import 'bootstrap/dist/css/bootstrap.min.css'; 4 3 import StarRating from './StarRating'; 5 4 import { useNavigate } from 'react-router-dom'; 5 import {RestaurantContext} from "./RestaurantContext"; 6 6 7 7 const Restaurants = () => { 8 8 const [restaurants, setRestaurants] = useState([]); 9 9 const navigate = useNavigate(); 10 const restaurantContext = useContext(RestaurantContext); 10 11 11 12 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 }, []); 13 setRestaurants(restaurantContext.restaurants); 14 }, [restaurantContext]); 23 15 24 16 const handleDetailClick = (restaurantId) => { … … 98 90 </div> 99 91 </div> 100 <button onClick={() => handleDetailClick(restaurant.restaurantId)} className="btn btn-primary">View Details</button> 92 <button onClick={() => handleDetailClick(restaurant.restaurantId)} 93 className="btn btn-primary">View Details 94 </button> 101 95 </div> 102 96 </div> -
my-react-app/src/index.js
r75f5086 rcfc16a3 3 3 import './index.css'; 4 4 import App from './App'; 5 import { CuisineProvider } from './components/CuisineContext'; 6 import {RestaurantProvider} from "./components/RestaurantContext"; 5 7 6 8 const root = ReactDOM.createRoot(document.getElementById('root')); 7 9 root.render( 8 <React.StrictMode> 9 <App /> 10 </React.StrictMode> 10 <React.StrictMode> 11 <CuisineProvider> 12 <RestaurantProvider> 13 <App /> 14 </RestaurantProvider> 15 </CuisineProvider> 16 </React.StrictMode> 11 17 );
Note:
See TracChangeset
for help on using the changeset viewer.