Ignore:
Timestamp:
04/30/25 18:24:41 (3 weeks ago)
Author:
Aleksandar Panovski <apano77@…>
Branches:
main
Children:
2518b3a
Parents:
deea3c4
Message:

menu feature done

File:
1 edited

Legend:

Unmodified
Added
Removed
  • my-react-app/src/components/ReservationEdit.js

    rdeea3c4 re15e8d9  
    6565    }, [table]);
    6666
    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 
    8867    const generateTimeOptions = (operatingHours) => {
    89         const { startTime, endTime } = parseOperatingHours(operatingHours);
    9068        const now = new Date();
    91 
    9269        const selectedDateObj = new Date(selectedDate);
     70
     71        const { startTime, endTime } = parseOperatingHours(operatingHours, selectedDateObj);
     72
    9373        const isToday = selectedDateObj.toDateString() === now.toDateString();
    94         const isTomorrow = selectedDateObj > now && selectedDateObj.getDate() === now.getDate() + 1;
    9574
    9675        let currentTime;
    9776
    9877        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            }
    10087        } else {
    101             currentTime = new Date(startTime);
     88            currentTime = startTime;
    10289        }
    10390
     
    11198    };
    11299
    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
    123112
    124113    const handleInputChange = (e) => {
     
    189178    const formattedDate = `${year}-${month}-${day}`;
    190179
    191     const parseOperatingHours = (operatingHours) => {
     180    const parseOperatingHours = (operatingHours, forDate) => {
    192181        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 };
    197193    };
    198194
Note: See TracChangeset for help on using the changeset viewer.