diff --git a/components/booking/AvailableTimes.tsx b/components/booking/AvailableTimes.tsx index 2b59e59e..3acc07a2 100644 --- a/components/booking/AvailableTimes.tsx +++ b/components/booking/AvailableTimes.tsx @@ -11,18 +11,7 @@ const AvailableTimes = (props) => { const router = useRouter(); const { user, rescheduleUid } = router.query; - const [loading, setLoading] = useState(false); - const [busy, setBusy] = useState([]); - - let availableTimes = []; - - // Handle date change and timezone change - useEffect(() => { - setLoading(true); - fetch(`/api/availability/${user}?dateFrom=${props.date.startOf('day').utc().format()}&dateTo=${props.date.endOf('day').utc().format()}`) - .then( res => res.json()) - .then(setBusy); - }, [props.date]); + const [loaded, setLoaded] = useState(false); const times = useMemo(() => getSlots({ @@ -35,11 +24,10 @@ const AvailableTimes = (props) => { }) , []) - useEffect(() => { - + const handleAvailableSlots = (busyTimes: []) => { // Check for conflicts for (let i = times.length - 1; i >= 0; i -= 1) { - busy.forEach(busyTime => { + busyTimes.forEach(busyTime => { let startTime = dayjs(busyTime.start); let endTime = dayjs(busyTime.end); @@ -64,23 +52,17 @@ const AvailableTimes = (props) => { } }); } - // Display available times - availableTimes = times.map((time) => -
- ); + setLoaded(true); + }; - console.log(availableTimes); - - setLoading(false); - - }, [busy]); + // Re-render only when invitee changes date + useEffect(() => { + setLoaded(false); + fetch(`/api/availability/${user}?dateFrom=${props.date.startOf('day').utc().format()}&dateTo=${props.date.endOf('day').utc().format()}`) + .then( res => res.json()) + .then(handleAvailableSlots); + }, [props.date]); return ({props.eventType.description}