import { useState } from 'react'; import { Stack, Box, Typography, Button, styled, Skeleton, CircularProgress, alpha, } from '@mui/material'; import { LoadingButton } from '@mui/lab'; import { CalendarPicker } from '@mui/x-date-pickers'; import dayjs, { Dayjs } from 'dayjs'; import utc from 'dayjs/plugin/utc'; import tz from 'dayjs/plugin/timezone'; import { BookingSlotHookProps } from '../../../api/bookings'; import { Slot } from '../../../api/bookings/types'; import TimezonePopover from '../../timezone-select/TimezonePopover'; import { TIMEZONE_LABELS } from '../../timezone-select/constants'; import { useTimezoneStateContext } from '../../timezone-select/TimezoneProvider'; dayjs.extend(utc); dayjs.extend(tz); export const StyledDateCalendar = styled(CalendarPicker)(({ theme }) => ({ margin: 0, width: '100%', height: 348, maxHeight: 348, '& .MuiDayPicker-weekDayLabel': { width: 48, }, '& .MuiPickersDay-root': { width: 48, height: 48, }, '& .MuiDayPicker-slideTransition': { minHeight: 252, }, })); function TimeSlotsSkeleton() { return ( {[...new Array(3).keys()].map((_, index) => ( ))} ); } function TimeSlots({ data, isLoading, error, loading, onConfirm, date, }: BookingSlotHookProps & { date: dayjs.Dayjs; }) { const [timezone] = useTimezoneStateContext(); const [selected, setSelected] = useState(null); const slots = data || []; const isLoadingFirst = !data && isLoading; const isLoadingMore = data && isLoading; if (isLoadingFirst) return ; if (error) { return ( Error loading slots ); } if (!data?.length) return ( No slots available ); return ( {slots.map((slot) => { const isSelected = slot === selected; const startOfDayInTz = date.tz(timezone, true); const startTimeInTz = dayjs(slot.startTime).tz(timezone); const formattedTime = startTimeInTz.format('h:mm A'); const isDifferentDay = !startTimeInTz.isSame(startOfDayInTz, 'date'); return ( {isSelected && ( onConfirm(slot)} > Confirm )} ); })} {isLoadingMore && ( alpha(theme.palette.background.paper, 0.5), }} > )} ); } export default function BaseCalendarForm({ date, setDate, ...rest }: BookingSlotHookProps & { date: Dayjs | null; setDate: (date: Dayjs | null) => void; }) { return ( <> Select a Date {!!date && } ); } function TimezoneButton() { const [open, setOpen] = useState(null); const [timezone, setTimezone] = useTimezoneStateContext(); return ( <> setOpen(null)} onChange={(value) => { setTimezone(value); setOpen(null); }} anchorOrigin={{ vertical: 'bottom', horizontal: 'right', }} transformOrigin={{ vertical: 'top', horizontal: 'right', }} /> ); }