* init apple calendar service * update apple calendar service logic * update typing for CalendarApp interface * add id and type for apple calendar * fixup: logic for apple calendar service * update logic for checkIfCalendarConnected function * add apple calendar service to calendars module * fixup * update calendars to include cases for apple calendar * fix imports * init frontend for apple calendar connect * add apple connect atom to connect atom * fixup * fixup * add alias for latest version of platform library * fix import paths * custom hook for saving apple calendar credentials * add apple calendar to examples app * add interface for credentials syncing calendars * bring calendars controller to orginal state * refactor * update custom hook to sync credentials * update atom * resolve merge conflicts * add endpoint for syncing calendar credentials * fixup * update atom * update useCheck to invalidate queries onSubmit * fix typo * fixup * remove unused tokens respository * rename controller * cleanup * fix: reset react queryClient on access token change --------- Co-authored-by: Rajiv Sahal <rajivsahal@Rajivs-MacBook-Pro.local> Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Co-authored-by: Morgan Vernay <morgan@cal.com>
53 lines
1.9 KiB
TypeScript
53 lines
1.9 KiB
TypeScript
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import type { CALENDARS } from "@calcom/platform-constants";
|
|
import { SUCCESS_STATUS, ERROR_STATUS } from "@calcom/platform-constants";
|
|
import type { ApiResponse, ApiErrorResponse } from "@calcom/platform-types";
|
|
|
|
import http from "../../lib/http";
|
|
import { useAtomsContext } from "../useAtomsContext";
|
|
|
|
export interface UseCheckProps {
|
|
onCheckError?: OnCheckErrorType;
|
|
calendar: (typeof CALENDARS)[number];
|
|
}
|
|
export type OnCheckErrorType = (err: ApiErrorResponse) => void;
|
|
export const getQueryKey = (calendar: (typeof CALENDARS)[number]) => [`get-${calendar}-check`];
|
|
|
|
export const useCheck = ({ onCheckError, calendar }: UseCheckProps) => {
|
|
const { isInit, accessToken } = useAtomsContext();
|
|
const queryClient = useQueryClient();
|
|
|
|
const { data: check, refetch } = useQuery({
|
|
queryKey: getQueryKey(calendar),
|
|
staleTime: 6000,
|
|
enabled: isInit && !!accessToken,
|
|
queryFn: () => {
|
|
return http
|
|
?.get<ApiResponse<{ checked: boolean; allowConnect: boolean }>>(`/calendars/${calendar}/check`)
|
|
.then(({ data: responseBody }) => {
|
|
if (responseBody.status === SUCCESS_STATUS) {
|
|
return { status: SUCCESS_STATUS, data: { allowConnect: false, checked: true } };
|
|
}
|
|
onCheckError?.(responseBody);
|
|
return { status: ERROR_STATUS, data: { allowConnect: true, checked: true } };
|
|
})
|
|
.catch((err) => {
|
|
onCheckError?.(err);
|
|
return { status: ERROR_STATUS, data: { allowConnect: true, checked: true } };
|
|
});
|
|
},
|
|
});
|
|
return {
|
|
allowConnect: check?.data?.allowConnect ?? false,
|
|
checked: check?.data?.checked ?? false,
|
|
refetch: () => {
|
|
queryClient.setQueryData(getQueryKey(calendar), {
|
|
status: SUCCESS_STATUS,
|
|
data: { allowConnect: false, checked: false },
|
|
});
|
|
refetch();
|
|
},
|
|
};
|
|
};
|