Files
cal.diy/packages/platform/atoms/availability/wrappers/AvailabilitySettingsPlatformWrapper.tsx
T
d2bbbf3f45 feat: expose AvailabilitySettingsProps type in @calcom/atoms (#22246)
* feat: expose AvailabilitySettingsProps type in @calcom/atoms

- Export AvailabilitySettingsPlatformWrapperProps as AvailabilitySettingsProps
- Add type export to main atoms package index for direct import
- Enables external consumers to import type directly from @calcom/atoms

Co-Authored-By: [email protected] <[email protected]>

* feat: also export AtomScheduleType for atomSchedule data

- Export FindDetailedScheduleByIdReturnType as AtomScheduleType
- Enables direct import of atomSchedule type from @calcom/atoms
- Covers both AvailabilitySettingsProps and AtomScheduleType exports

Co-Authored-By: [email protected] <[email protected]>

* fix: update AtomScheduleType to include null and undefined

Co-Authored-By: [email protected] <[email protected]>

* refactor: move AtomScheduleType to AvailabilitySettingsPlatformWrapper for cleaner organization

Co-Authored-By: [email protected] <[email protected]>

* feat: rename AtomScheduleType to AvailabilitySettingsAtomScheduleType for better clarity

Co-Authored-By: [email protected] <[email protected]>

* feat: rename to AvailabilitySettingsScheduleType for better naming consistency

Co-Authored-By: [email protected] <[email protected]>

* fix: use proper import from @calcom/platform-libraries/schedules instead of dynamic import

Co-Authored-By: [email protected] <[email protected]>

* add changelogs

* feat: extract AvailabilitySettingsSchedule type from AvailabilitySettingsProps

- Extract schedule property structure into separate AvailabilitySettingsSchedule type
- Update AvailabilitySettingsProps to reuse the extracted schedule type
- Export AvailabilitySettingsSchedule from @calcom/atoms package index
- Enables direct import of schedule type for external consumers

Co-Authored-By: [email protected] <[email protected]>

* Revert "feat: extract AvailabilitySettingsSchedule type from AvailabilitySettingsProps"

This reverts commit 79239e4b18925db3fcc1d8df7e9f37770ec4f116.

* fixup: extract schedule for AvailabilitySettingsProps

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: [email protected] <[email protected]>
Co-authored-by: Rajiv Sahal <[email protected]>
Co-authored-by: Ryukemeister <[email protected]>
2025-07-11 16:53:40 +02:00

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";
export 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>
);
};