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) => -
- - {dayjs(time).tz(timeZone()).format(props.timeFormat)} - -
- ); + 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 (
@@ -89,7 +71,17 @@ const AvailableTimes = (props) => { {props.date.format("dddd DD MMMM YYYY")}
- {!loading ? availableTimes :
} + { + loaded ? times.map((time) => +
+ + {dayjs(time).tz(timeZone()).format(props.timeFormat)} + +
+ ) :
+ } ); } diff --git a/pages/[user]/[type].tsx b/pages/[user]/[type].tsx index 1b95f4b7..0d5b4c38 100644 --- a/pages/[user]/[type].tsx +++ b/pages/[user]/[type].tsx @@ -76,6 +76,18 @@ export default function Type(props) { )]; + const handleSelectTimeZone = (selectedTimeZone: string) => { + if (selectedDate) { + setSelectedDate(selectedDate.tz(selectedTimeZone)) + } + }; + + const handleToggle24hClock = (is24hClock: boolean) => { + if (selectedDate) { + setTimeFormat(is24hClock ? 'HH:mm' : 'h:mma'); + } + } + return (
@@ -115,12 +127,8 @@ export default function Type(props) { {timeZone()} - { - isTimeOptionsOpen && - setSelectedDate(selectedDate.tz(selectedTimeZone))} - onToggle24hFormat={(is24hClock: boolean) => setTimeFormat(is24hClock ? 'HH:mm' : 'h:mma')} - />} + { isTimeOptionsOpen && }

{props.eventType.description}