Changeset e15e8d9 for my-react-app/src/components/ReservationEdit.js
- Timestamp:
- 04/30/25 18:24:41 (3 weeks ago)
- Branches:
- main
- Children:
- 2518b3a
- Parents:
- deea3c4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
my-react-app/src/components/ReservationEdit.js
rdeea3c4 re15e8d9 65 65 }, [table]); 66 66 67 const generateTimeSlots = (operatingHours, interval) => {68 const slots = [];69 const [startTimeStr, endTimeStr] = operatingHours.split('-');70 const [startHours, startMinutes] = startTimeStr.split(':').map(Number);71 const [endHours, endMinutes] = endTimeStr.split(':').map(Number);72 73 const startTime = new Date();74 startTime.setHours(startHours, startMinutes, 0, 0);75 76 const endTime = new Date();77 endTime.setHours(endHours, endMinutes, 0, 0);78 79 let currentTime = startTime;80 while (currentTime <= endTime) {81 slots.push(new Date(currentTime).toISOString());82 currentTime = new Date(currentTime.getTime() + interval * 60000);83 }84 85 return slots;86 };87 88 67 const generateTimeOptions = (operatingHours) => { 89 const { startTime, endTime } = parseOperatingHours(operatingHours);90 68 const now = new Date(); 91 92 69 const selectedDateObj = new Date(selectedDate); 70 71 const { startTime, endTime } = parseOperatingHours(operatingHours, selectedDateObj); 72 93 73 const isToday = selectedDateObj.toDateString() === now.toDateString(); 94 const isTomorrow = selectedDateObj > now && selectedDateObj.getDate() === now.getDate() + 1;95 74 96 75 let currentTime; 97 76 98 77 if (isToday) { 99 currentTime = roundToNext15Minutes(new Date()); 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 } 100 87 } else { 101 currentTime = new Date(startTime);88 currentTime = startTime; 102 89 } 103 90 … … 111 98 }; 112 99 113 useEffect(() => { 114 const operatingHours = table?.restaurant?.operatingHours || "09:00-00:00"; 115 const allTimeSlots = generateTimeSlots(operatingHours, timeSlotInterval); 116 117 const availableSlots = allTimeSlots.filter((slot) => 118 !tableReservations.includes(slot) 119 ); 120 121 setFilteredTimeSlots(availableSlots); 122 }, [tableReservations, table]); 100 const roundToNext15Minutes = (date) => { 101 const ms = 1000 * 60 * 15; 102 return new Date(Math.ceil(date.getTime() / ms) * ms); 103 }; 104 105 useEffect(() => { 106 if (table?.restaurant?.operatingHours && selectedDate) { 107 const options = generateTimeOptions(table.restaurant.operatingHours); 108 setTimeOptions(options); 109 } 110 }, [table, selectedDate]); 111 123 112 124 113 const handleInputChange = (e) => { … … 189 178 const formattedDate = `${year}-${month}-${day}`; 190 179 191 const parseOperatingHours = (operatingHours ) => {180 const parseOperatingHours = (operatingHours, forDate) => { 192 181 const [start, end] = operatingHours.split('-'); 193 return { 194 startTime: new Date(`1970-01-01T${start}:00`), 195 endTime: new Date(`1970-01-01T${end}:00`) 196 }; 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 }; 197 193 }; 198 194
Note:
See TracChangeset
for help on using the changeset viewer.