feat: Event availability platform wrapper (#17091)

* moved listSchedules to schedule repository

* Create EventAvailabilityTabPlatformWrapper.tsx

* Revert "moved listSchedules to schedule repository"

This reverts commit b5feacfabbadbf9d75eb205dfad8f70b7773a5cb.

* fix: active state of tabs items

* added event availability platform wrapper

* Update event-types.tsx

* better types

only pick the types we need and omit the rest

* fix: type-check failing

---------

Co-authored-by: Morgan Vernay <[email protected]>
Co-authored-by: Morgan <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-10-16 13:24:13 +03:00
1 parent 123074f4ba
commit e90e060739
11 files changed
+193 -53

No files matched your search

@@ -31,6 +31,7 @@ export function transformApiScheduleForAtom(
scheduleId: schedule.id,
days: "days" in entry ? entry.days : [],
date: "date" in entry ? entry.date : null,
id: schedule.id,
};
});
@@ -0,0 +1,36 @@
import { useQuery } from "@tanstack/react-query";
import {
V2_ENDPOINTS,
SUCCESS_STATUS,
VERSION_2024_06_11,
CAL_API_VERSION_HEADER,
} from "@calcom/platform-constants";
import type { GetScheduleOutput_2024_06_11 } from "@calcom/platform-types";
import http from "../../lib/http";
export const QUERY_KEY = "event-type-user-schedule";
export const useSchedule = (id?: number) => {
const pathname = id ? `/${V2_ENDPOINTS.availability}/${id}` : `/${V2_ENDPOINTS.availability}/default`;
const { isLoading, error, data } = useQuery({
queryKey: [QUERY_KEY, id],
queryFn: () => {
return http
.get<GetScheduleOutput_2024_06_11>(pathname, {
headers: { [CAL_API_VERSION_HEADER]: VERSION_2024_06_11 },
})
.then((res) => {
if (res.data.status === SUCCESS_STATUS) {
return res.data.data;
}
throw new Error(res.data.error?.message);
});
},
enabled: !!id,
});
return { isLoading, error, data };
};
@@ -0,0 +1,35 @@
import { useQuery } from "@tanstack/react-query";
import {
V2_ENDPOINTS,
SUCCESS_STATUS,
VERSION_2024_06_11,
CAL_API_VERSION_HEADER,
} from "@calcom/platform-constants";
import type { GetSchedulesOutput_2024_06_11 } from "@calcom/platform-types";
import http from "../../lib/http";
export const QUERY_KEY = "event-type-user-schedules";
export const useSchedules = () => {
const pathname = `/${V2_ENDPOINTS.availability}`;
const { isLoading, error, data } = useQuery({
queryKey: [QUERY_KEY],
queryFn: () => {
return http
.get<GetSchedulesOutput_2024_06_11>(pathname, {
headers: { [CAL_API_VERSION_HEADER]: VERSION_2024_06_11 },
})
.then((res) => {
if (res.data.status === SUCCESS_STATUS) {
return res.data.data;
}
throw new Error(res.data.error?.message);
});
},
});
return { isLoading, error, data };
};
@@ -0,0 +1,66 @@
import { useFormContext } from "react-hook-form";
import { EventAvailabilityTab } from "@calcom/features/eventtypes/components/tabs/availability/EventAvailabilityTab";
import type { EventTypeSetup, FormValues } from "@calcom/features/eventtypes/lib/types";
import type { User } from "@calcom/prisma/client";
import type { Availability } from "../../availability/AvailabilitySettings";
import { transformApiScheduleForAtom } from "../../availability/atom-api-transformers/transformApiScheduleForAtom";
import { useSchedule } from "../hooks/useSchedule";
import { useSchedules } from "../hooks/useSchedules";
type EventAvailabilityTabPlatformWrapperProps = {
user?: Pick<User, "id" | "defaultScheduleId" | "timeZone" | "timeFormat" | "weekStart">;
eventType: EventTypeSetup;
isTeamEvent: boolean;
};
const EventAvailabilityTabPlatformWrapper = ({
user,
...props
}: EventAvailabilityTabPlatformWrapperProps) => {
const formMethods = useFormContext<FormValues>();
const scheduleId = formMethods.watch("schedule");
const { isLoading: isSchedulePending, data: scheduleQueryData } = useSchedule(
scheduleId || user?.defaultScheduleId || undefined
);
const { data: schedulesQueryData, isLoading: isSchedulesPending } = useSchedules();
const atomSchedule = transformApiScheduleForAtom(user, scheduleQueryData, schedulesQueryData?.length || 0);
if (!atomSchedule) {
return <></>;
}
return (
<EventAvailabilityTab
{...props}
user={user}
schedulesQueryData={schedulesQueryData}
isSchedulesPending={isSchedulesPending}
isSchedulePending={isSchedulePending}
scheduleQueryData={{
isManaged: atomSchedule.isManaged,
readOnly: atomSchedule.readOnly,
id: atomSchedule.id,
timeZone: atomSchedule.timeZone,
schedule:
atomSchedule.schedule.reduce(
(acc: Availability[], avail: Availability) => [
...acc,
{
startTime: new Date(avail.startTime),
endTime: new Date(avail.endTime),
days: avail.days,
},
],
[]
) || [],
}}
/>
);
};
export default EventAvailabilityTabPlatformWrapper;
@@ -11,7 +11,7 @@ import { trpc } from "@calcom/trpc/react";
export type EventAvailabilityTabWebWrapperProps = {
eventType: EventTypeSetup;
isTeamEvent: boolean;
loggedInUser?: RouterOutputs["viewer"]["me"];
user?: RouterOutputs["viewer"]["me"];
teamMembers: TeamMembers;
};
@@ -32,13 +32,13 @@ const EventAvailabilityTabWebWrapper = (props: EventAvailabilityTabWebWrapperPro
const { isPending: isSchedulePending, data: scheduleQueryData } =
trpc.viewer.availability.schedule.get.useQuery(
{
scheduleId: scheduleId || props.loggedInUser?.defaultScheduleId || undefined,
scheduleId: scheduleId || props.user?.defaultScheduleId || undefined,
isManagedEventType: isManagedEventType || isChildrenManagedEventType,
},
{ enabled: !!scheduleId || !!props.loggedInUser?.defaultScheduleId }
{ enabled: !!scheduleId || !!props.user?.defaultScheduleId }
);
const { data: availabilityQueryData, isPending: isAvailabilityPending } =
const { data: schedulesQueryData, isPending: isSchedulesPending } =
trpc.viewer.availability.list.useQuery(undefined);
const hostSchedulesQuery = trpc.viewer.availability.schedule.getAllSchedulesByUserId.useQuery;
@@ -46,8 +46,8 @@ const EventAvailabilityTabWebWrapper = (props: EventAvailabilityTabWebWrapperPro
return (
<EventAvailabilityTab
{...props}
availabilityQueryData={availabilityQueryData}
isAvailabilityPending={isAvailabilityPending}
schedulesQueryData={schedulesQueryData?.schedules}
isSchedulesPending={isSchedulesPending}
isSchedulePending={isSchedulePending}
scheduleQueryData={scheduleQueryData}
editAvailabilityRedirectUrl={`/availability/${scheduleQueryData?.id}`}
@@ -20,6 +20,7 @@ import { useEventTypeForm } from "../hooks/useEventTypeForm";
import { useHandleRouteChange } from "../hooks/useHandleRouteChange";
import { usePlatformTabsNavigations } from "../hooks/usePlatformTabsNavigations";
import EventAdvancedPlatformWrapper from "./EventAdvancedPlatformWrapper";
import EventAvailabilityTabPlatformWrapper from "./EventAvailabilityTabPlatformWrapper";
import EventLimitsTabPlatformWrapper from "./EventLimitsTabPlatformWrapper";
import EventRecurringTabPlatformWrapper from "./EventRecurringTabPlatformWrapper";
import SetupTab from "./EventSetupTabPlatformWrapper";
@@ -83,7 +84,7 @@ const EventType = ({
// Reset the form with these values as new default values to ensure the correct comparison for dirtyFields eval
form.reset(currentValues);
toast({ description: t("event_type_updated_successfully", { eventTypeTitle: eventType.title }) });
onSuccess?.(currentValues);
props.onSuccess?.(currentValues);
},
async onSettled() {
return;
@@ -92,7 +93,7 @@ const EventType = ({
const currentValues = form.getValues();
const message = err?.message;
toast({ description: message ? t(message) : t(err.message) });
onError?.(currentValues, err);
props.onError?.(currentValues, err);
},
});
@@ -115,7 +116,11 @@ const EventType = ({
) : (
<></>
),
availability: <></>,
availability: tabs.includes("availability") ? (
<EventAvailabilityTabPlatformWrapper eventType={eventType} isTeamEvent={!!team} user={user?.data} />
) : (
<></>
),
team: <></>,
advanced: tabs.includes("advanced") ? (
<EventAdvancedPlatformWrapper
@@ -160,7 +160,7 @@ const EventTypeWeb = ({
const { t } = useLocale();
const utils = trpc.useUtils();
const { data: loggedInUser, isPending: isLoggedInUserPending } = useMeQuery();
const { data: user, isPending: isLoggedInUserPending } = useMeQuery();
const isTeamEventTypeDeleted = useRef(false);
const leaveWithoutAssigningHosts = useRef(false);
const telemetry = useTelemetry();
@@ -245,7 +245,7 @@ const EventTypeWeb = ({
<EventAvailabilityTab
eventType={eventType}
isTeamEvent={!!team}
loggedInUser={loggedInUser}
user={user}
teamMembers={teamMembers}
/>
),
@@ -255,7 +255,7 @@ const EventTypeWeb = ({
<EventAdvancedTab
eventType={eventType}
team={team}
user={loggedInUser}
user={user}
isUserLoading={isLoggedInUserPending}
showToast={showToast}
/>
@@ -91,7 +91,7 @@ export default function Bookings(props: { calUsername: string; calEmail: string
<EventTypeSettings
allowDelete={true}
id={eventTypeId}
tabs={["setup", "limits", "recurring", "advanced"]}
tabs={["setup", "limits", "recurring", "advanced", "availability"]}
onSuccess={(eventType) => {
console.log(eventType);
setEventTypeId(null);