Files
cal.diy/packages/platform/atoms/availability/wrappers/AvailabilitySettingsPlatformWrapper.tsx
T
5bb2a904ce refactor: Deprecate Legacy v2 Booker Atom Endpoints (#20939)
* feat: split atoms endpoints and add public event type endpoint

* Update ConnectedDestinationCalendars import path across platform/atoms components

* refactor: update BookingResponse import path from platform-libraries to features/bookings

* Move AvailableSlotsType to util.ts and update imports to use GetAvailableSlotsResponse

* Refactor import path for RecurringBookingCreateBody type from libraries to types

* Fix import path for getBookingForReschedule type from platform-libraries to features

* Refactor PublicEventType export location and update import references

* Remove @calcom/platform-libraries dependency from atoms package.json

* chore(deps): update yarn.lock dependencies

* Remove console.log statements and fix indentation in event type hooks

* Migrate event type transformers from platform/libraries to api/v2 directory

* Remove console.log

* Update BookerPlatformWrapper.tsx

* Add script to populate empty team slugs with slugified team names

* Remove vitest imports

* reset platform libraries version

* Remove unused orgId comment from useAtomGetPublicEvent hook params

* Update useApiV2AvailableSlots.ts

* refactor: remove unused exports from lib package index file

* Undo: @SomayChauhan
Add script to populate empty team slugs with slugified team names

* Update booking.tsx

* chore: upgrade @calcom/platform-libraries from 0.0.202 to 0.0.205

* chore: bump @calcom/platform-libraries from 0.0.205 to 0.0.206

* chore: configure babel and jest for node module transpilation in api v2

* fix: type errors

* Revert "chore: configure babel and jest for node module transpilation in api v2"

This reverts commit b2cf172a84fe8953f9497bf6e43874f476fbc04b.

* Update calendars.service.ts

* chore: bump @calcom/platform-libraries from 0.0.208 to 0.0.209

* fix: add proper type definition for calendar busy times to resolve ts-expect-error

* refactor: deprecate v2 old availability endpoints (#21075)

Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>

* chore: publish platform librareis

* feat: add delegation credential fields to calendar service mock

* chore: update @calcom/platform-libraries from 0.0.211 to 0.0.213

* fix: skip failing calendar integration test

* chore: bump @calcom/platform-libraries from 0.0.213 to 0.0.214

* fix: api/v2 build error

* fix: e2e tests

---------

Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
Co-authored-by: supalarry <laurisskraucis@gmail.com>
Co-authored-by: Lauris Skraucis <lauris.skraucis@gmail.com>
2025-06-12 20:20:52 +05:30

199 lines
5.9 KiB
TypeScript

import type { ReactNode } from "react";
import type { ScheduleLabelsType } from "@calcom/features/schedules/components/Schedule";
import type { UpdateScheduleResponse } from "@calcom/lib/schedules/updateSchedule";
import type { ApiErrorResponse, ApiResponse, UpdateScheduleInput_2024_06_11 } from "@calcom/platform-types";
import { useAtomSchedule } from "../../hooks/schedules/useAtomSchedule";
import { useAtomUpdateSchedule } from "../../hooks/schedules/useAtomUpdateSchedule";
import useDeleteSchedule from "../../hooks/schedules/useDeleteSchedule";
import { useMe } from "../../hooks/useMe";
import { AtomsWrapper } from "../../src/components/atoms-wrapper";
import { useToast } from "../../src/components/ui/use-toast";
import type { Availability } from "../AvailabilitySettings";
import type { CustomClassNames } from "../AvailabilitySettings";
import { AvailabilitySettings } from "../AvailabilitySettings";
import type { AvailabilityFormValues } from "../types";
type AvailabilitySettingsPlatformWrapperProps = {
id?: string;
labels?: {
tooltips: Partial<ScheduleLabelsType>;
};
customClassNames?: Partial<CustomClassNames>;
onUpdateSuccess?: (res: ApiResponse<UpdateScheduleResponse>) => void;
onUpdateError?: (err: ApiErrorResponse) => void;
onDeleteSuccess?: (res: ApiResponse) => void;
onDeleteError?: (err: ApiErrorResponse) => void;
disableEditableHeading?: boolean;
enableOverrides?: boolean;
onBeforeUpdate?: (updateBody: UpdateScheduleInput_2024_06_11) => boolean | Promise<boolean>;
onFormStateChange?: (formState: AvailabilityFormValues) => void;
allowDelete?: boolean;
allowSetToDefault?: boolean;
disableToasts?: boolean;
isDryRun?: boolean;
noScheduleChildren?: ReactNode;
loadingStateChildren?: ReactNode;
};
export const AvailabilitySettingsPlatformWrapper = ({
id,
customClassNames,
onDeleteError,
onDeleteSuccess,
onUpdateError,
onUpdateSuccess,
disableEditableHeading = false,
enableOverrides = false,
onBeforeUpdate,
onFormStateChange,
allowDelete,
allowSetToDefault,
disableToasts,
isDryRun = false,
noScheduleChildren,
loadingStateChildren,
}: AvailabilitySettingsPlatformWrapperProps) => {
const { isLoading, data: atomSchedule } = useAtomSchedule(id);
const { data: me } = useMe();
const { timeFormat } = me?.data || { timeFormat: null };
const { toast } = useToast();
const { mutate: deleteSchedule, isPending: isDeletionInProgress } = useDeleteSchedule({
onSuccess: (res) => {
onDeleteSuccess?.(res);
if (!disableToasts) {
toast({
description: "Schedule deleted successfully",
});
}
},
onError: (err) => {
onDeleteError?.(err);
if (!disableToasts) {
toast({
description: "Could not delete schedule",
});
}
},
});
const { mutate: updateSchedule, isPending: isSavingInProgress } = useAtomUpdateSchedule({
onSuccess: (res) => {
onUpdateSuccess?.(res);
if (!disableToasts) {
toast({
description: "Schedule updated successfully",
});
}
},
onError: (err) => {
onUpdateError?.(err);
if (!disableToasts) {
toast({
description: "Could not update schedule",
});
}
},
});
const handleDelete = async (id: number) => {
await deleteSchedule({ id });
};
const handleUpdate = async (id: number, body: AvailabilityFormValues) => {
let canUpdate = true;
if (onBeforeUpdate) {
canUpdate = await onBeforeUpdate(body);
}
if (canUpdate) {
updateSchedule({
scheduleId: id,
body: {
...body,
dateOverrides: body.dateOverrides.flatMap((override) => override.ranges),
},
});
}
};
if (isLoading) {
return (
<>
{loadingStateChildren ? loadingStateChildren : <div className="px-10 py-4 text-xl">Loading...</div>}
</>
);
}
if (!atomSchedule) {
return noScheduleChildren ? (
<>{noScheduleChildren}</>
) : (
<div className="px-10 py-4 text-xl">No user schedule present</div>
);
}
return (
<AtomsWrapper>
<AvailabilitySettings
disableEditableHeading={disableEditableHeading}
handleDelete={() => {
if (isDryRun) {
toast({
description: "Schedule deleted successfully",
});
}
if (!isDryRun && atomSchedule.id) {
handleDelete(atomSchedule.id);
}
}}
handleSubmit={async (data) => {
if (isDryRun) {
toast({
description: "Schedule updated successfully",
});
}
if (!isDryRun && atomSchedule.id) {
handleUpdate(atomSchedule.id, data);
}
}}
weekStart={me?.data?.weekStart || "Sunday"}
timeFormat={timeFormat}
enableOverrides={enableOverrides}
isLoading={isLoading}
schedule={{
name: atomSchedule.name,
id: atomSchedule.id,
isLastSchedule: atomSchedule.isLastSchedule,
isDefault: atomSchedule.isDefault,
workingHours: atomSchedule.workingHours,
dateOverrides: atomSchedule.dateOverrides,
timeZone: atomSchedule.timeZone,
availability: atomSchedule.availability,
schedule:
atomSchedule.schedule.reduce(
(acc: Availability[], avail: Availability) => [
...acc,
{ days: avail.days, startTime: new Date(avail.startTime), endTime: new Date(avail.endTime) },
],
[]
) || [],
}}
isDeleting={isDeletionInProgress}
isSaving={isSavingInProgress}
backPath=""
isPlatform={true}
customClassNames={customClassNames}
allowDelete={allowDelete}
allowSetToDefault={allowSetToDefault}
onFormStateChange={onFormStateChange}
/>
</AtomsWrapper>
);
};