* init bookings endpoint * abstracting functions * e2e tests for bookings * hooks for bookings endpoint * bookings respository fixtures * typings for booking input * fixup * abstract booking info code and use it as handler * import handlers for bookings endpoint * add cancel booking input * add handleCancelBooking handler in platform libraries * cancel booking endpoint * abstract call into its own separate fn * cancel booking hook * e2e test for cancel booking endpoint * fix import * export getBookings function * move getAllUserBookings into lib * add bookings folder to package exports * use getAllUserBookings from lib * fix import path * fix: hooks, endpoint and example for cancel / reschedule / list / booking success page * fix: unit test mock classNames import from lib * fix: unit test mock classNames import from lib --------- Co-authored-by: Morgan Vernay <[email protected]> Co-authored-by: Morgan <[email protected]>
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { useQuery } from "@tanstack/react-query";
|
|
|
|
import { BASE_URL, API_VERSION, 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";
|
|
|
|
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 endpoint = new URL(BASE_URL);
|
|
|
|
endpoint.pathname = `api/${API_VERSION}/${V2_ENDPOINTS.bookings}/${props.uid}/reschedule`;
|
|
|
|
const bookingQuery = useQuery({
|
|
queryKey: [QUERY_KEY, props.uid],
|
|
queryFn: () => {
|
|
return http
|
|
.get<ApiResponse<Awaited<ReturnType<typeof getBookingForReschedule>>>>(endpoint.toString())
|
|
.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: !!props?.uid,
|
|
});
|
|
|
|
return bookingQuery;
|
|
};
|