Files
cal.diy/apps/web/components/booking/actions/bookingActions.ts
T
Eunjae Lee 3986d61f40 feat(bookings): improve bookings redesign (#25251)
* use booking.uid instead of booking.id for url param

* show timezone on calendar

* fix type

* restore horizontal tab and remove header and subtitle

* clean up sidebar items

* fix event propagation from attendees

* fetch all statuses except for cancelled on calendar view

* clean up styles of the badges on BookingListItem

* fix useMediaQuery

* add close button to the header

* add assignment reason to the details sheet

* use separator row

* use ToggleGroup for the top bookings tab

* move ViewToggleButton

* resize the action button

* remove wrong prop

* fix type error

* fix type error

* hide view toggle button on mobile (and fix the breakpoint)

* remove unused e2e tests

* fix e2e tests

* hide toggle button when feature flag is off

* update skeleton

* improve attendees on booking list item and slide over

* improve attendee dropdown

* fix type error

* move query to containers

* select attendee email

* infinite fetching for calendar view

* update styles

* fix compatibility

* fix: add backward compatibility for status field in getAllUserBookings

* increase calendar height

* fix type error

* support Member filter only for admin / owners

* add debug log (TEMP)

* add event border color

* show Reject / Accept buttons on BookingDetailsSheet

* move description section to the top

* update When section

* update style of Who section

* add CancelBookingDialog WIP

* fix CancelBookingDialog

* increase clickable area

* add schedule info section WIP

* fix flaky reject button

* fixing reschedule info WIP

* add fromReschedule index to Booking

* improve rescheduled information

* improve reassignment

* fix type error

* fix unit test

* respect user's weekStart value on the booking calendar view

* update debug log

* improve payment section

* clean up

* fix log message

* reposition filters on list view

* fix bookings controller api2 e2e test

* clean up file by extracting logic into custom hooks

* rename files

* merge BookingCalendar into its container

* extract logic into separate hook files

* remove redundant logic

* rearrange items on calendar view

* add WeekPicker

* extract filter button

* responsive header on list view

* horizontal scroll for ToggleGroup WIP

* fix type error

* fix cancelling recurring event

* address feedback

* fix e2e tests

* fix unit test

* fix e2e tests

* make hover style more visible for ToggleGroup

* fix margin on CancelBookingDialog

* update styles on the slide over (mostly font weight)

* update style of CancelBookingDialog

* update styles

* update margin top for the header

* refactor getBookingDetails handler

* fix gap in who section

* auto-filter the current user on the calendar view

* calculate calendar height considering top banners

* improve booking details sheet interaction without overlay

* update calendar event styles

* update reject dialog style

* put uid first in the query params

* fix class name

* memoize functions in useMediaQuery

* query attendee with id instead of email

* update margins

* replace TRPCError with ErrorWithCode

* move calculation outside loop

* remove dead code
2025-12-10 13:40:04 +00:00

271 lines
8.7 KiB
TypeScript

import { BookingStatus, SchedulingType } from "@calcom/prisma/enums";
import type { ActionType } from "@calcom/ui/components/table";
import type { BookingItemProps } from "../types";
export interface BookingActionContext {
booking: BookingItemProps;
isUpcoming: boolean;
isOngoing: boolean;
isBookingInPast: boolean;
isCancelled: boolean;
isConfirmed: boolean;
isRejected: boolean;
isPending: boolean;
isRescheduled: boolean;
isRecurring: boolean;
isTabRecurring: boolean;
isTabUnconfirmed: boolean;
isBookingFromRoutingForm: boolean;
isDisabledCancelling: boolean;
isDisabledRescheduling: boolean;
isCalVideoLocation: boolean;
showPendingPayment: boolean;
isAttendee: boolean;
cardCharged: boolean;
attendeeList: Array<{
name: string;
email: string;
id: number;
noShow: boolean;
phoneNumber: string | null;
}>;
getSeatReferenceUid: () => string | undefined;
t: (key: string) => string;
}
export function getPendingActions(context: BookingActionContext): ActionType[] {
const { booking, isPending, isTabRecurring, isTabUnconfirmed, isRecurring, showPendingPayment, t } =
context;
const actions: ActionType[] = [];
// For bookings with payment, only confirm if the booking is paid for
// Original logic: (isPending && !paymentAppData.enabled) || (paymentAppData.enabled && !!paymentAppData.price && booking.paid)
if ((isPending && !showPendingPayment) || (showPendingPayment && booking.paid)) {
actions.push({
id: "confirm",
bookingUid: booking.uid,
label: (isTabRecurring || isTabUnconfirmed) && isRecurring ? t("confirm_all") : t("confirm"),
icon: "check" as const,
disabled: false, // This would be controlled by mutation state in the component
});
}
actions.push({
id: "reject",
label: (isTabRecurring || isTabUnconfirmed) && isRecurring ? t("reject_all") : t("reject"),
icon: "ban",
disabled: false, // This would be controlled by mutation state in the component
});
return actions;
}
export function getCancelEventAction(context: BookingActionContext): ActionType {
const { booking, isTabRecurring, isRecurring, t } = context;
return {
id: "cancel",
label: isTabRecurring && isRecurring ? t("cancel_all_remaining") : t("cancel_event"),
icon: "circle-x",
color: "destructive",
disabled: isActionDisabled("cancel", context),
bookingUid: booking.uid,
};
}
export function getVideoOptionsActions(context: BookingActionContext): ActionType[] {
const { booking, isBookingInPast, isConfirmed, isCalVideoLocation, t } = context;
return [
{
id: "view_recordings",
label: t("view_recordings"),
icon: "video",
disabled: !(isBookingInPast && isConfirmed && isCalVideoLocation && booking.isRecorded),
},
{
id: "meeting_session_details",
label: t("view_session_details"),
icon: "info",
disabled: !(isBookingInPast && isConfirmed && isCalVideoLocation),
},
];
}
export function getEditEventActions(context: BookingActionContext): ActionType[] {
const {
booking,
isBookingInPast,
isDisabledRescheduling,
isBookingFromRoutingForm,
getSeatReferenceUid,
isAttendee,
t,
} = context;
const seatReferenceUid = getSeatReferenceUid();
const isReassignableRoundRobin =
booking.eventType.schedulingType === SchedulingType.ROUND_ROBIN &&
(!booking.eventType.hostGroups || booking.eventType.hostGroups.length <= 1);
const isManagedChildEvent = booking.eventType.parentId != null;
const isReassignable = isReassignableRoundRobin || isManagedChildEvent;
const actions: (ActionType | null)[] = [
{
id: "reschedule",
icon: "clock",
label: t("reschedule_booking"),
href: `/reschedule/${booking.uid}${
booking.seatsReferences.length && isAttendee && seatReferenceUid
? `?seatReferenceUid=${seatReferenceUid}`
: ""
}`,
disabled:
(isBookingInPast && !booking.eventType.allowReschedulingPastBookings) || isDisabledRescheduling,
},
{
id: "reschedule_request",
icon: "send",
iconClassName: "rotate-45 w-[16px] -translate-x-0.5 ",
label: t("send_reschedule_request"),
disabled:
(isBookingInPast && !booking.eventType.allowReschedulingPastBookings) ||
isDisabledRescheduling ||
booking.seatsReferences.length > 0,
},
isBookingFromRoutingForm
? {
id: "reroute",
label: t("reroute"),
icon: "waypoints",
disabled: false,
}
: null,
{
id: "change_location",
label: t("edit_location"),
icon: "map-pin",
disabled: false,
},
booking.eventType?.disableGuests
? null
: {
id: "add_members",
label: t("additional_guests"),
icon: "user-plus",
disabled: false,
},
isReassignable
? {
id: "reassign",
label: t("reassign"),
icon: "users",
disabled: false,
}
: null,
];
return actions.filter(Boolean) as ActionType[];
}
export function getReportAction(context: BookingActionContext): ActionType {
const { booking, t } = context;
return {
id: "report",
label: t("report_booking"),
icon: "flag",
color: "destructive",
disabled: !!booking.report,
};
}
export function getAfterEventActions(context: BookingActionContext): ActionType[] {
const { booking, cardCharged, attendeeList, t } = context;
const actions: (ActionType | null)[] = [
...getVideoOptionsActions(context),
booking.status === BookingStatus.ACCEPTED && booking.paid && booking.payment[0]?.paymentOption === "HOLD"
? {
id: "charge_card",
label: cardCharged ? t("no_show_fee_charged") : t("collect_no_show_fee"),
icon: "credit-card",
disabled: cardCharged,
}
: null,
{
id: "no_show",
label:
attendeeList.length === 1 && attendeeList[0].noShow ? t("unmark_as_no_show") : t("mark_as_no_show"),
icon: attendeeList.length === 1 && attendeeList[0].noShow ? "eye" : "eye-off",
disabled: false, // This would be controlled by booking state in the component
},
];
return actions.filter(Boolean) as ActionType[];
}
export function shouldShowPendingActions(context: BookingActionContext): boolean {
const { isPending, isUpcoming, isCancelled } = context;
return isPending && isUpcoming && !isCancelled;
}
export function shouldShowEditActions(context: BookingActionContext): boolean {
const { isPending, isTabRecurring, isRecurring, isCancelled } = context;
return !isPending && !(isTabRecurring && isRecurring) && !isCancelled;
}
export function shouldShowRecurringCancelAction(context: BookingActionContext): boolean {
const { isTabRecurring, isRecurring } = context;
return isTabRecurring && isRecurring;
}
export function shouldShowIndividualReportButton(context: BookingActionContext): boolean {
const { booking, isPending, isUpcoming, isCancelled, isRejected } = context;
const hasDropdown = shouldShowEditActions(context);
return !booking.report && !hasDropdown && (isCancelled || isRejected || (isPending && isUpcoming));
}
export function isActionDisabled(actionId: string, context: BookingActionContext): boolean {
const { booking, isBookingInPast, isDisabledRescheduling, isDisabledCancelling } = context;
switch (actionId) {
case "reschedule":
case "reschedule_request":
return (isBookingInPast && !booking.eventType.allowReschedulingPastBookings) || isDisabledRescheduling;
case "cancel":
return isDisabledCancelling || isBookingInPast;
case "view_recordings":
return !(isBookingInPast && booking.status === BookingStatus.ACCEPTED && context.isCalVideoLocation);
case "meeting_session_details":
return !(isBookingInPast && booking.status === BookingStatus.ACCEPTED && context.isCalVideoLocation);
case "charge_card":
return context.cardCharged;
default:
return false;
}
}
export function getActionLabel(actionId: string, context: BookingActionContext): string {
const { isTabRecurring, isRecurring, attendeeList, cardCharged, t } = context;
switch (actionId) {
case "reject":
return (isTabRecurring || context.isTabUnconfirmed) && isRecurring ? t("reject_all") : t("reject");
case "confirm":
return (isTabRecurring || context.isTabUnconfirmed) && isRecurring ? t("confirm_all") : t("confirm");
case "cancel":
return isTabRecurring && isRecurring ? t("cancel_all_remaining") : t("cancel_event");
case "no_show":
return attendeeList.length === 1 && attendeeList[0].noShow
? t("unmark_as_no_show")
: t("mark_as_no_show");
case "charge_card":
return cardCharged ? t("no_show_fee_charged") : t("collect_no_show_fee");
default:
return t(actionId);
}
}