diff --git a/lib/slots.ts b/lib/slots.ts index 318b1bed2e..157b56a692 100644 --- a/lib/slots.ts +++ b/lib/slots.ts @@ -23,7 +23,7 @@ const getSlots = ({ if(!selectedDate) return [] - const lowerBound = selectedDate.startOf("day"); + const lowerBound = selectedDate.tz(selectedTimeZone).startOf("day"); // Simple case, same timezone if (calendarTimeZone === selectedTimeZone) { @@ -42,7 +42,7 @@ const getSlots = ({ return slots; } - const upperBound = selectedDate.endOf("day"); + const upperBound = selectedDate.tz(selectedTimeZone).endOf("day"); // We need to start generating slots from the start of the calendarTimeZone day const startDateTime = lowerBound diff --git a/pages/[user]/[type].tsx b/pages/[user]/[type].tsx index c33d095608..728694dab5 100644 --- a/pages/[user]/[type].tsx +++ b/pages/[user]/[type].tsx @@ -5,6 +5,7 @@ import prisma from '../../lib/prisma'; import { useRouter } from 'next/router'; import dayjs, { Dayjs } from 'dayjs'; import { Switch } from '@headlessui/react'; +import TimezoneSelect from 'react-timezone-select'; import { ClockIcon, GlobeIcon, ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/solid'; import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'; import isBetween from 'dayjs/plugin/isBetween'; @@ -32,6 +33,14 @@ export default function Type(props) { const [busy, setBusy] = useState([]); const telemetry = useTelemetry(); + const [selectedTimeZone, setSelectedTimeZone] = useState(''); + + useEffect(() => { + // Setting timezone only client-side + setSelectedTimeZone(dayjs.tz.guess()) + }, []) + + // Get router variables const router = useRouter(); const { user } = router.query; @@ -81,13 +90,13 @@ export default function Type(props) { const calendar = [...emptyDays, ...days.map((day) => )]; - // Handle date change + // Handle date change and timezone change useEffect(() => { const changeDate = async () => { if (!selectedDate) { @@ -101,17 +110,18 @@ export default function Type(props) { setLoading(false); } changeDate(); - }, [selectedDate]); + }, [selectedDate, selectedTimeZone]); - - const times = getSlots({ - calendarTimeZone: props.user.timeZone, - selectedTimeZone: dayjs.tz.guess(), - eventLength: props.eventType.length, - selectedDate: selectedDate, - dayStartTime: props.user.startTime, - dayEndTime: props.user.endTime, - }) + const times = useMemo(() => + getSlots({ + calendarTimeZone: props.user.timeZone, + selectedTimeZone: selectedTimeZone, + eventLength: props.eventType.length, + selectedDate: selectedDate, + dayStartTime: props.user.startTime, + dayEndTime: props.user.endTime, + }) + , [selectedDate, selectedTimeZone]) // Check for conflicts for(let i = times.length - 1; i >= 0; i -= 1) { @@ -135,7 +145,7 @@ export default function Type(props) { const availableTimes = times.map((time) =>
- {dayjs(time).tz(dayjs.tz.guess()).format(is24h ? "HH:mm" : "hh:mma")} + {dayjs(time).tz(selectedTimeZone).format(is24h ? "HH:mm" : "hh:mma")}
); @@ -158,39 +168,37 @@ export default function Type(props) { {props.eventType.length} minutes

- - { isTimeOptionsOpen && -
- - - am/pm - - - Use setting - - - 24h - - -
- } +

+ + + + am/pm + + + Use setting + + + 24h + + +

+

+ setSelectedTimeZone(value)} className="shadow-sm focus:ring-blue-500 focus:border-blue-500 mt-1 block w-full sm:text-sm border-gray-300 rounded-md" /> +

{props.eventType.description}

@@ -268,4 +276,4 @@ export async function getServerSideProps(context) { eventType }, } -} \ No newline at end of file +}