Changeset e48199a for my-react-app/src/components/ReservationEdit.js
- Timestamp:
- 05/07/25 18:34:01 (10 days ago)
- Branches:
- main
- Parents:
- b67dfd3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
my-react-app/src/components/ReservationEdit.js
rb67dfd3 re48199a 30 30 setIsLoading(true); 31 31 const response = await axios.get(`http://localhost:8081/api/reservations/${reservationId}`); 32 console.log(response)33 32 setCheckInTime(response.data.reservationDateTime); 34 33 setFormData(response.data); 35 setRestaurant(response.data.restaurantName); 34 36 35 setRestaurantId(response.data.restaurantId); 36 const restaurantResponse = await axios.get(`http://localhost:8081/api/restaurants/${response.data.restaurantId}`); 37 setRestaurant(restaurantResponse.data); 37 38 38 39 setTableNumber(response.data.tableNumber); … … 45 46 } 46 47 }; 47 48 48 fetchReservation(); 49 49 }, [reservationId]); … … 64 64 } 65 65 }, [table]); 66 67 const generateTimeOptions = (operatingHours) => {68 const now = new Date();69 const selectedDateObj = new Date(selectedDate);70 71 const { startTime, endTime } = parseOperatingHours(operatingHours, selectedDateObj);72 73 const isToday = selectedDateObj.toDateString() === now.toDateString();74 75 let currentTime;76 77 if (isToday) {78 const roundedNow = roundToNext15Minutes(now);79 80 if (roundedNow < startTime) {81 currentTime = startTime;82 } else if (roundedNow > endTime) {83 return [];84 } else {85 currentTime = roundedNow;86 }87 } else {88 currentTime = startTime;89 }90 91 const options = [];92 while (currentTime <= endTime) {93 options.push(currentTime.toTimeString().slice(0, 5));94 currentTime = new Date(currentTime.getTime() + 15 * 60 * 1000);95 }96 97 return options;98 };99 100 const roundToNext15Minutes = (date) => {101 const ms = 1000 * 60 * 15;102 return new Date(Math.ceil(date.getTime() / ms) * ms);103 };104 66 105 67 useEffect(() => { … … 178 140 const formattedDate = `${year}-${month}-${day}`; 179 141 180 const parseOperatingHours = (operatingHours, forDate) => { 181 const [start, end] = operatingHours.split('-'); 182 183 const [startHour, startMinute] = start.split(':').map(Number); 184 const [endHour, endMinute] = end.split(':').map(Number); 185 186 const startTime = new Date(forDate); 187 startTime.setHours(startHour, startMinute, 0, 0); 188 189 const endTime = new Date(forDate); 190 endTime.setHours(endHour, endMinute, 0, 0); 191 192 return { startTime, endTime }; 193 }; 194 195 useEffect(() => { 196 if (formData?.restaurant?.operatingHours && selectedDate) { 197 const options = generateTimeOptions(formData.restaurant.operatingHours); 142 const generateTimeOptions = (operatingHours) => { 143 const now = new Date(); 144 const selectedDateObj = new Date(selectedDate); 145 const isToday = selectedDateObj.toDateString() === now.toDateString(); 146 147 let options = []; 148 for (const hours of operatingHours.split(',')) { 149 const [start, end] = hours.trim().split('-'); 150 const startTime = new Date(selectedDateObj); 151 const endTime = new Date(selectedDateObj); 152 const [startHour, startMinute] = start.split(':').map(Number); 153 const [endHour, endMinute] = end.split(':').map(Number); 154 startTime.setHours(startHour, startMinute, 0); 155 endTime.setHours(endHour, endMinute, 0); 156 157 if (isToday && startTime < now) { 158 startTime.setTime(roundToNext15Minutes(now).getTime()); 159 } 160 161 if (endTime <= now) { 162 setNoAvailableMessage('No available time due to closing.'); 163 continue; 164 } 165 166 while (startTime <= endTime) { 167 options.push(startTime.toTimeString().slice(0, 5)); 168 startTime.setMinutes(startTime.getMinutes() + 15); 169 } 170 } 171 172 return options.length ? options : ['No available time']; 173 }; 174 175 const roundToNext15Minutes = (date) => { 176 const ms = 1000 * 60 * 15; 177 const roundedTime = new Date(Math.ceil(date.getTime() / ms) * ms); 178 179 if (roundedTime.getTime() === date.getTime()) { 180 roundedTime.setMinutes(roundedTime.getMinutes() + 15); 181 } 182 183 return roundedTime; 184 }; 185 186 useEffect(() => { 187 if (restaurant?.operatingHours && selectedDate) { 188 const options = generateTimeOptions(restaurant.operatingHours); 198 189 setTimeOptions(options); 199 190 } 200 191 }, [restaurant, selectedDate]); 201 192 202 // useEffect(() => {203 // if (checkInTime) {204 // const checkInDateObj = new Date(checkInTime);205 // setSelectedDate(checkInDateObj.toISOString().split("T")[0]);206 // setSelectedTime(checkInDateObj.toTimeString().slice(0, 5));207 // }208 // }, [checkInTime]);209 193 210 194 return ( … … 217 201 <div className="card-body"> 218 202 <h2 className="card-title"> 219 {formData.restaurant.name} <StarRating key={formData.restaurant.id} rating={formData.restaurant.rating} /> 203 {restaurant?.name} 204 <StarRating key={restaurant?.id} rating={restaurant?.rating || 0}/> 220 205 </h2> 221 <p className="card-text">{formData.restaurant.name}</p> 222 <p className="card-text">{formData.restaurant.operatingHours}</p> 223 <p className="card-text">Ul. {formData.restaurant.address}</p> 224 <br /> 206 <p className="card-text">Operating Hours: {restaurant?.operatingHours}</p> 207 <p className="card-text">Address: Ul. {restaurant?.address}</p> 208 <br/> 225 209 </div> 226 210 <form onSubmit={handleSubmit}>
Note:
See TracChangeset
for help on using the changeset viewer.