Files
calendar/packages/platform/atoms/hooks/calendars/useDeleteCalendarCredentials.ts
T
0b03bcb90e feat: CalendarSettings atom (#16120)
* dumb components for calendar settings

* shadcn switch

* update exports

* update packages

* add calendar settings atom to examples app

* init calendar settings atom

* refactors

* fix import path

* export type for calendar switch props

* invalidate queries on deleting calendar credentials

* replace calendars list with calendar settings wrapper

* cleanup

* update styling

* refactors

* cleanup

* fix: missing key prop CalendarSettingsPlatformWrapper

* Label as client components

* Address client component build errors

* Move QueryCell out of packages/lib

* PR feedback

* more feedback

---------

Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
Co-authored-by: Morgan Vernay <morgan@cal.com>
Co-authored-by: Joe Au-Yeung <j.auyeung419@gmail.com>
2024-08-13 21:51:29 +05:30

68 lines
1.7 KiB
TypeScript

import { useMutation, useQueryClient } from "@tanstack/react-query";
import type { CALENDARS } from "@calcom/platform-constants";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiErrorResponse, ApiResponse } from "@calcom/platform-types";
import http from "../../lib/http";
import { QUERY_KEY } from "../useConnectedCalendars";
interface IUseDeleteCalendarCredentials {
onSuccess?: (res: ApiResponse) => void;
onError?: (err: ApiErrorResponse | Error) => void;
}
export const useDeleteCalendarCredentials = (
{ onSuccess, onError }: IUseDeleteCalendarCredentials = {
onSuccess: () => {
return;
},
onError: () => {
return;
},
}
) => {
const queryClient = useQueryClient();
const deleteCalendarCredentials = useMutation<
ApiResponse<{
status: string;
data: {
id: number;
type: string;
userId: number | null;
teamId: number | null;
appId: string | null;
invalid: boolean | null;
};
}>,
unknown,
{ id: number; calendar: (typeof CALENDARS)[number] }
>({
mutationFn: (data) => {
const { id, calendar } = data;
const body = {
id,
};
return http.post(`/calendars/${calendar}/disconnect`, body).then((res) => {
return res.data;
});
},
onSuccess: (data) => {
if (data.status === SUCCESS_STATUS) {
onSuccess?.(data);
} else {
onError?.(data);
}
},
onError: (err) => {
onError?.(err as ApiErrorResponse);
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEY] });
},
});
return deleteCalendarCredentials;
};