feat: v2 api endpoints and hooks to list private and public event types of user (#14192)

* refactor: extract getEventTypeByViewer for private user dashboard event types

* refactor: extract getEventTypesPublic for public event types to book

* refactor: group event-types lib functions in single folder

* refactor: rename getEventTypeByViewer to getEventTypesByViewer

* feat: add event type helpers from lib to platform libraries

* feat: v2 endpoint for private event types

* driveby:fix: fixed date in docs instead of being re-generated

* feature: endpoint to fetch public endpoints

* fix: getting private event types

* tests: fetch private event types

* tests: fetch private event types

* feat: hooks for public and private events

* fix: yarn test

* tests: IMO better mocking

* refactor: track dynamic prop in react query

* fix: distinct query key for public events

* fix: username in query key and example app demo

* fix: swagger for endpoint get public event input

---------

Co-authored-by: Morgan Vernay <[email protected]>
Co-authored-by: Morgan <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-03-26 08:35:17 +00:00
1 parent da7c5ec407
commit 0223db1f7c
24 files changed
+680 -410

No files matched your search

@@ -0,0 +1,26 @@
import { useQuery } from "@tanstack/react-query";
import { BASE_URL, API_VERSION, V2_ENDPOINTS, SUCCESS_STATUS } from "@calcom/platform-constants";
import type { EventTypesByViewer } from "@calcom/platform-libraries";
import type { ApiResponse, ApiSuccessResponse } from "@calcom/platform-types";
import http from "../../lib/http";
export const QUERY_KEY = "get-private-events";
export const useEventTypesPrivate = () => {
const endpoint = new URL(BASE_URL);
endpoint.pathname = `api/${API_VERSION}/${V2_ENDPOINTS.eventTypes}`;
return useQuery({
queryKey: [QUERY_KEY],
queryFn: () => {
return http?.get<ApiResponse<EventTypesByViewer>>(endpoint.toString()).then((res) => {
if (res.data.status === SUCCESS_STATUS) {
return (res.data as ApiSuccessResponse<EventTypesByViewer>).data;
}
throw new Error(res.data.error.message);
});
},
});
};
@@ -0,0 +1,26 @@
import { useQuery } from "@tanstack/react-query";
import { BASE_URL, API_VERSION, V2_ENDPOINTS, SUCCESS_STATUS } from "@calcom/platform-constants";
import type { EventTypesPublic } from "@calcom/platform-libraries";
import type { ApiResponse, ApiSuccessResponse } from "@calcom/platform-types";
import http from "../../lib/http";
export const QUERY_KEY = "get-public-events";
export const useEventTypesPublic = (username: string) => {
const endpoint = new URL(BASE_URL);
endpoint.pathname = `api/${API_VERSION}/${V2_ENDPOINTS.eventTypes}/${username}/public`;
return useQuery({
queryKey: [QUERY_KEY, username],
queryFn: () => {
return http?.get<ApiResponse<EventTypesPublic>>(endpoint.toString()).then((res) => {
if (res.data.status === SUCCESS_STATUS) {
return (res.data as ApiSuccessResponse<EventTypesPublic>).data;
}
throw new Error(res.data.error.message);
});
},
});
};
@@ -2,7 +2,7 @@ import { useQuery } from "@tanstack/react-query";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ConnectedDestinationCalendars } from "@calcom/platform-libraries";
import type { ApiResponse } from "@calcom/platform-types";
import type { ApiResponse, ApiSuccessResponse } from "@calcom/platform-types";
import http from "../lib/http";
@@ -13,7 +13,7 @@ export const useConnectedCalendars = (props: { enabled?: boolean }) => {
queryFn: () => {
return http.get<ApiResponse<ConnectedDestinationCalendars>>("/ee/calendars").then((res) => {
if (res.data.status === SUCCESS_STATUS) {
return res.data?.data;
return (res.data as ApiSuccessResponse<ConnectedDestinationCalendars>)?.data;
}
throw new Error(res.data.error.message);
});
+1
View File
@@ -5,3 +5,4 @@ export { BookerPlatformWrapper as Booker } from "./booker";
export { useIsPlatform } from "./hooks/useIsPlatform";
export { useAtomsContext } from "./hooks/useAtomsContext";
export { useConnectedCalendars } from "./hooks/useConnectedCalendars";
export { useEventTypesPublic } from "./hooks/event-types/useEventTypesPublic";
@@ -2,19 +2,39 @@ import { Navbar } from "@/components/Navbar";
import { Inter } from "next/font/google";
import { useState } from "react";
import { Booker } from "@calcom/atoms";
import { Booker, useEventTypesPublic } from "@calcom/atoms";
const inter = Inter({ subsets: ["latin"] });
export default function Bookings(props: { calUsername: string; calEmail: string }) {
const [bookingTitle, setBookingTitle] = useState<string | null>(null);
const [eventTypeSlug, setEventTypeSlug] = useState<string | null>(null);
const { isLoading: isLoadingEvents, data: eventTypes } = useEventTypesPublic(props.calUsername);
console.log(isLoadingEvents, eventTypes);
return (
<main
className={`flex min-h-screen flex-col ${inter.className} main text-default flex min-h-full w-full flex-col items-center overflow-visible`}>
<Navbar username={props.calUsername} />
<div>
<h1 className="my-4 text-2xl font-semibold">{props.calUsername} Public Booking Page</h1>
{!bookingTitle && (
{isLoadingEvents && !eventTypeSlug && <p>Loading...</p>}
{!isLoadingEvents && !eventTypeSlug && Boolean(eventTypes?.length) && (
<div className="flex flex-row gap-4">
{eventTypes?.map((event: { id: number; slug: string; title: string }) => (
<button
key={event.id}
className="rounded bg-blue-500 px-4 py-2 font-bold text-white hover:bg-blue-700"
onClick={() => setEventTypeSlug(event.slug)}>
{event.slug}
</button>
))}
</div>
)}
{!bookingTitle && eventTypeSlug && (
<Booker
eventSlug="sixty-minutes-video"
username={props.calUsername ?? ""}
+7 -2
View File
@@ -11,7 +11,13 @@ export type UpdateScheduleOutputType = Awaited<
typeof import("@calcom/trpc/server/routers/viewer/availability/schedule/update.handler").updateHandler
>
>;
export { getEventTypeById } from "@calcom/lib/getEventTypeById";
export { getEventTypeById } from "@calcom/lib/event-types/getEventTypeById";
export { getEventTypesByViewer } from "@calcom/lib/event-types/getEventTypesByViewer";
export { getEventTypesPublic } from "@calcom/lib/event-types/getEventTypesPublic";
export type { EventType } from "@calcom/lib/event-types/getEventTypeById";
export type { EventTypesByViewer } from "@calcom/lib/event-types/getEventTypesByViewer";
export type { EventTypesPublic } from "@calcom/lib/event-types/getEventTypesPublic";
export type PublicEventType = Awaited<ReturnType<typeof getPublicEvent>>;
export { getPublicEvent };
@@ -26,7 +32,6 @@ export { getAvailableSlots };
export type AvailableSlotsType = Awaited<ReturnType<typeof getAvailableSlots>>;
export { handleNewRecurringBooking } from "@calcom/features/bookings/lib/handleNewRecurringBooking";
export type { EventType } from "@calcom/lib/getEventTypeById";
export { getConnectedDestinationCalendars } from "@calcom/lib/getConnectedDestinationCalendars";
export type { ConnectedDestinationCalendars } from "@calcom/lib/getConnectedDestinationCalendars";
export { getBusyCalendarTimes } from "@calcom/core/CalendarManager";
+5
View File
@@ -1,19 +1,24 @@
import { ApiProperty } from "@nestjs/swagger";
import { Transform } from "class-transformer";
import { IsBoolean, IsOptional, IsString } from "class-validator";
export class GetPublicEventInput {
@IsString()
@Transform(({ value }: { value: string }) => value.toLowerCase())
@ApiProperty({ required: true })
username!: string;
@IsString()
@ApiProperty({ required: true })
eventSlug!: string;
@Transform(({ value }: { value: string }) => value === "true")
@IsBoolean()
@IsOptional()
@ApiProperty({ required: false })
isTeamEvent?: boolean;
@ApiProperty({ required: false })
@IsString()
@IsOptional()
org?: string;