Files
calendar/packages/platform/atoms/hooks/bookings/useGetBookingForReschedule.ts
T
Lauris SkraucisandGitHub a10d3599e2 refactor: platform bookings hooks & bookings API (#16963)
* chore: delete unused bookings hook

* refactor: group bookings hooks in folder

* refactor: rename useGetBooking, useGetBookings to useBooking, useBookings

* refactor: booking inputs and outputs

* chore: make bookings controller work with new inputs and outputs

* make examples app work

* refactor: deprecate fields instead of removing them

* readd eventtypeid

* swagger regenerate

* make recurring booking backwards compatible

* fix test

* update changelog

* swagger update

* fix import

* chore: update lock file
2024-10-08 14:05:28 +02:00

55 lines
1.7 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import { V2_ENDPOINTS, SUCCESS_STATUS } from "@calcom/platform-constants";
import type { getBookingForReschedule } from "@calcom/platform-libraries";
import type { ApiResponse, ApiSuccessResponse } from "@calcom/platform-types";
import http from "../../lib/http";
import { useAtomsContext } from "../useAtomsContext";
export const QUERY_KEY = "user-booking";
interface IUseGetBookingForReschedule {
onSuccess?: (res: ApiSuccessResponse<Awaited<ReturnType<typeof getBookingForReschedule>>>["data"]) => void;
onError?: (err: Error) => void;
uid?: string;
}
export const useGetBookingForReschedule = (
props: IUseGetBookingForReschedule = {
onSuccess: () => {
return;
},
onError: () => {
return;
},
uid: "",
}
) => {
const { isInit } = useAtomsContext();
const pathname = `/${V2_ENDPOINTS.bookings}/${props.uid}/reschedule`;
const bookingQuery = useQuery({
queryKey: [QUERY_KEY, props.uid],
queryFn: () => {
return http
.get<ApiResponse<Awaited<ReturnType<typeof getBookingForReschedule>>>>(pathname)
.then((res) => {
if (res.data.status === SUCCESS_STATUS) {
props.onSuccess?.(
(res.data as ApiSuccessResponse<Awaited<ReturnType<typeof getBookingForReschedule>>>).data
);
return (res.data as ApiSuccessResponse<Awaited<ReturnType<typeof getBookingForReschedule>>>).data;
}
const error = new Error(res.data.error.message);
props.onError?.(error);
throw error;
})
.catch((err) => {
props.onError?.(err);
});
},
enabled: isInit && !!props?.uid,
});
return bookingQuery;
};