2021-06-19 22:50:47 +00:00
import Link from "next/link" ;
2021-06-24 22:15:18 +00:00
import { useRouter } from "next/router" ;
2022-06-15 20:54:31 +00:00
import { FC , useEffect , useState } from "react" ;
2021-09-22 19:52:38 +00:00
2022-06-28 20:40:58 +00:00
import dayjs , { Dayjs } from "@calcom/dayjs" ;
2022-06-15 20:54:31 +00:00
import { useLocale } from "@calcom/lib/hooks/useLocale" ;
2022-10-25 00:14:06 +00:00
import { TimeFormat } from "@calcom/lib/timeFormat" ;
2022-03-27 19:48:13 +00:00
import { nameOfDay } from "@calcom/lib/weekday" ;
2022-07-22 17:27:06 +00:00
import type { Slot } from "@calcom/trpc/server/routers/viewer/slots" ;
2022-11-23 02:55:25 +00:00
import { SkeletonContainer , SkeletonText , ToggleGroup } from "@calcom/ui" ;
2022-03-27 19:48:13 +00:00
2021-12-15 10:26:39 +00:00
import classNames from "@lib/classNames" ;
2022-05-05 18:51:22 +00:00
import { timeZone } from "@lib/clock" ;
2021-09-22 19:52:38 +00:00
2021-09-23 17:18:29 +00:00
type AvailableTimesProps = {
2022-10-25 00:14:06 +00:00
timeFormat : TimeFormat ;
onTimeFormatChange : ( is24Hour : boolean ) = > void ;
2021-09-23 17:18:29 +00:00
eventTypeId : number ;
2022-05-05 21:16:25 +00:00
recurringCount : number | undefined ;
2022-04-06 17:20:30 +00:00
eventTypeSlug : string ;
2022-10-10 13:24:06 +00:00
date? : Dayjs ;
2022-05-24 13:19:12 +00:00
seatsPerTimeSlot? : number | null ;
2022-06-15 20:54:31 +00:00
slots? : Slot [ ] ;
2022-06-27 21:01:46 +00:00
isLoading : boolean ;
2022-09-05 21:10:58 +00:00
ethSignature? : string ;
2021-09-23 17:18:29 +00:00
} ;
const AvailableTimes : FC < AvailableTimesProps > = ( {
2022-06-15 20:54:31 +00:00
slots = [ ] ,
2022-06-27 21:01:46 +00:00
isLoading ,
2021-07-08 21:14:29 +00:00
date ,
eventTypeId ,
2022-04-06 17:20:30 +00:00
eventTypeSlug ,
2022-05-05 21:16:25 +00:00
recurringCount ,
2021-07-08 21:14:29 +00:00
timeFormat ,
2022-10-25 00:14:06 +00:00
onTimeFormatChange ,
2022-05-24 13:19:12 +00:00
seatsPerTimeSlot ,
2022-09-05 21:10:58 +00:00
ethSignature ,
2021-07-08 21:14:29 +00:00
} ) = > {
2021-12-20 11:55:49 +00:00
const { t , i18n } = useLocale ( ) ;
2021-06-19 22:50:47 +00:00
const router = useRouter ( ) ;
2021-06-27 22:30:11 +00:00
const { rescheduleUid } = router . query ;
2021-07-08 21:14:29 +00:00
2021-12-15 10:26:39 +00:00
const [ brand , setBrand ] = useState ( "#292929" ) ;
useEffect ( ( ) = > {
setBrand ( getComputedStyle ( document . documentElement ) . getPropertyValue ( "--brand-color" ) . trim ( ) ) ;
} , [ ] ) ;
2022-10-10 13:24:06 +00:00
if ( ! date ) return null ;
2021-06-19 22:50:47 +00:00
return (
2022-10-10 13:24:06 +00:00
< div className = "dark:bg-darkgray-100 mt-8 flex h-full w-full flex-col px-4 text-center sm:mt-0 sm:p-5 md:-mb-5 md:min-w-[200px] lg:min-w-[300px]" >
2022-10-25 00:14:06 +00:00
< div className = "mb-6 flex items-center text-left text-base" >
< div className = "mr-4" >
< span className = "text-bookingdarker dark:text-darkgray-800 font-semibold text-gray-900" >
{ nameOfDay ( i18n . language , Number ( date . format ( "d" ) ) , "short" ) }
< / span >
< span className = "text-bookinglight font-medium" >
2022-12-16 13:35:56 +00:00
, { date . toDate ( ) . toLocaleString ( i18n . language , { month : "short" } ) } { date . format ( " D " ) }
2022-10-25 00:14:06 +00:00
< / span >
< / div >
< div className = "ml-auto" >
< ToggleGroup
onValueChange = { ( timeFormat ) = > onTimeFormatChange ( timeFormat === "24" ) }
defaultValue = { timeFormat === TimeFormat . TWELVE_HOUR ? "12" : "24" }
options = { [
{ value : "12" , label : t ( "12_hour_short" ) } ,
{ value : "24" , label : t ( "24_hour_short" ) } ,
] }
/ >
< / div >
2021-06-19 22:50:47 +00:00
< / div >
2023-01-03 22:05:40 +00:00
< div className = "flex-grow overflow-y-auto sm:block md:h-[364px]" >
2022-06-27 21:01:46 +00:00
{ slots . length > 0 &&
2021-09-22 14:44:38 +00:00
slots . map ( ( slot ) = > {
2021-10-12 13:11:33 +00:00
type BookingURL = {
pathname : string ;
query : Record < string , string | number | string [ ] | undefined > ;
} ;
const bookingUrl : BookingURL = {
2022-10-19 21:25:03 +00:00
pathname : router.pathname.endsWith ( "/embed" ) ? "../book" : "book" ,
2021-09-22 14:44:38 +00:00
query : {
. . . router . query ,
2022-11-04 14:59:38 +00:00
date : dayjs.utc ( slot . time ) . tz ( timeZone ( ) ) . format ( ) ,
2021-09-22 14:44:38 +00:00
type : eventTypeId ,
2022-04-06 17:20:30 +00:00
slug : eventTypeSlug ,
2022-05-05 21:16:25 +00:00
/** Treat as recurring only when a count exist and it's not a rescheduling workflow */
count : recurringCount && ! rescheduleUid ? recurringCount : undefined ,
2023-01-12 13:55:00 +00:00
. . . ( ethSignature ? { ethSignature } : { } ) ,
2021-09-22 14:44:38 +00:00
} ,
} ;
2021-09-14 08:45:28 +00:00
2021-09-22 14:44:38 +00:00
if ( rescheduleUid ) {
2021-10-12 13:11:33 +00:00
bookingUrl . query . rescheduleUid = rescheduleUid as string ;
2021-09-22 14:44:38 +00:00
}
2021-09-14 08:45:28 +00:00
2022-05-24 13:19:12 +00:00
// If event already has an attendee add booking id
if ( slot . bookingUid ) {
bookingUrl . query . bookingUid = slot . bookingUid ;
}
2021-09-22 14:44:38 +00:00
return (
2022-12-21 19:32:42 +00:00
< div data - slot - owner = { ( slot . userIds || [ ] ) . join ( "," ) } key = { ` ${ dayjs ( slot . time ) . format ( ) } ` } >
{ /* ^ data-slot-owner is helpful in debugging and used to identify the owners of the slot. Owners are the users which have the timeslot in their schedule. It doesn't consider if a user has that timeslot booked */ }
2022-05-24 13:19:12 +00:00
{ /* Current there is no way to disable Next.js Links */ }
{ seatsPerTimeSlot && slot . attendees && slot . attendees >= seatsPerTimeSlot ? (
< div
2021-12-15 10:26:39 +00:00
className = { classNames (
2022-08-30 11:51:08 +00:00
"text-primary-500 dark:bg-darkgray-200 dark:text-darkgray-900 mb-2 block rounded-sm border bg-white py-2 font-medium opacity-25 dark:border-transparent " ,
brand === "#fff" || brand === "#ffffff" ? "" : ""
2022-05-24 13:19:12 +00:00
) } >
2022-06-15 20:54:31 +00:00
{ dayjs ( slot . time ) . tz ( timeZone ( ) ) . format ( timeFormat ) }
2022-07-12 17:50:04 +00:00
{ ! ! seatsPerTimeSlot && < p className = "text-sm" > { t ( "booking_full" ) } < / p > }
2022-05-24 13:19:12 +00:00
< / div >
) : (
2023-01-06 12:13:56 +00:00
< Link
href = { bookingUrl }
prefetch = { false }
className = { classNames (
"text-primary-500 hover:border-gray-900 hover:bg-gray-50" ,
2023-01-12 16:57:43 +00:00
"dark:bg-darkgray-200 dark:hover:bg-darkgray-300 dark:hover:border-darkmodebrand dark:text-darkgray-800 mb-2 block rounded-md border bg-white py-2 text-sm font-medium dark:border-transparent" ,
2023-01-06 12:13:56 +00:00
brand === "#fff" || brand === "#ffffff" ? "" : ""
) }
data - testid = "time" >
{ dayjs ( slot . time ) . tz ( timeZone ( ) ) . format ( timeFormat ) }
{ ! ! seatsPerTimeSlot && (
< p
className = { ` ${
slot . attendees && slot . attendees / seatsPerTimeSlot >= 0.8
? "text-rose-600"
: slot . attendees && slot . attendees / seatsPerTimeSlot >= 0.33
? "text-yellow-500"
: "text-emerald-400"
} text - sm ` }>
{ slot . attendees ? seatsPerTimeSlot - slot.attendees : seatsPerTimeSlot } / { " " }
{ seatsPerTimeSlot } { t ( "seats_available" ) }
< / p >
) }
2022-05-24 13:19:12 +00:00
< / Link >
) }
2021-09-22 14:44:38 +00:00
< / div >
) ;
} ) }
2022-06-27 21:01:46 +00:00
{ ! isLoading && ! slots . length && (
2022-02-09 00:05:13 +00:00
< div className = "-mt-4 flex h-full w-full flex-col content-center items-center justify-center" >
2021-10-08 11:43:48 +00:00
< h1 className = "my-6 text-xl text-black dark:text-white" > { t ( "all_booked_today" ) } < / h1 >
2021-09-22 14:44:38 +00:00
< / div >
) }
2022-06-27 21:01:46 +00:00
{ isLoading && ! slots . length && (
< >
< SkeletonContainer className = "mb-2" >
2022-11-14 10:31:36 +00:00
< SkeletonText className = "h-5 w-full" / >
2022-06-27 21:01:46 +00:00
< / SkeletonContainer >
< SkeletonContainer className = "mb-2" >
2022-11-14 10:31:36 +00:00
< SkeletonText className = "h-5 w-full" / >
2022-06-27 21:01:46 +00:00
< / SkeletonContainer >
< SkeletonContainer className = "mb-2" >
2022-11-14 10:31:36 +00:00
< SkeletonText className = "h-5 w-full" / >
2022-06-27 21:01:46 +00:00
< / SkeletonContainer >
< / >
) }
2021-09-22 14:44:38 +00:00
< / div >
2021-06-19 22:50:47 +00:00
< / div >
) ;
2021-06-24 22:15:18 +00:00
} ;
2021-06-19 22:50:47 +00:00
2022-10-10 13:24:06 +00:00
AvailableTimes . displayName = "AvailableTimes" ;
2021-06-22 15:19:28 +00:00
export default AvailableTimes ;