* 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>
93 lines
2.4 KiB
TypeScript
93 lines
2.4 KiB
TypeScript
import { useQuery, useMutation } 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";
|
|
|
|
export const getQueryKey = (calendar: (typeof CALENDARS)[number]) => [`get-${calendar}-redirect-uri`];
|
|
|
|
interface IPUpdateOAuthCredentials {
|
|
onSuccess?: (res: ApiResponse) => void;
|
|
onError?: (err: ApiErrorResponse) => void;
|
|
}
|
|
|
|
export const useGetRedirectUrl = (calendar: (typeof CALENDARS)[number], redir?: string) => {
|
|
const authUrl = useQuery({
|
|
queryKey: getQueryKey(calendar),
|
|
staleTime: Infinity,
|
|
enabled: false,
|
|
queryFn: () => {
|
|
return http
|
|
?.get<ApiResponse<{ authUrl: string }>>(
|
|
`/calendars/${calendar}/connect${redir ? `?redir=${redir}` : ""}`
|
|
)
|
|
.then(({ data: responseBody }) => {
|
|
if (responseBody.status === SUCCESS_STATUS) {
|
|
return responseBody.data.authUrl;
|
|
}
|
|
if (responseBody.status === ERROR_STATUS) throw new Error(responseBody.error.message);
|
|
return "";
|
|
});
|
|
},
|
|
});
|
|
|
|
return authUrl;
|
|
};
|
|
|
|
export const useConnect = (calendar: (typeof CALENDARS)[number], redir?: string) => {
|
|
const { refetch } = useGetRedirectUrl(calendar, redir);
|
|
|
|
const connect = async () => {
|
|
const redirectUri = await refetch();
|
|
|
|
if (redirectUri.data) {
|
|
window.location.href = redirectUri.data;
|
|
}
|
|
};
|
|
|
|
return { connect };
|
|
};
|
|
|
|
export const useSaveCalendarCredentials = (
|
|
{ onSuccess, onError }: IPUpdateOAuthCredentials = {
|
|
onSuccess: () => {
|
|
return;
|
|
},
|
|
onError: () => {
|
|
return;
|
|
},
|
|
}
|
|
) => {
|
|
const mutation = useMutation<
|
|
ApiResponse<{ status: string }>,
|
|
unknown,
|
|
{ username: string; password: string; calendar: (typeof CALENDARS)[number] }
|
|
>({
|
|
mutationFn: (data) => {
|
|
const { calendar, username, password } = data;
|
|
const body = {
|
|
username,
|
|
password,
|
|
};
|
|
|
|
return http.post(`/calendars/${calendar}/credentials`, body).then((res) => {
|
|
return res.data;
|
|
});
|
|
},
|
|
onSuccess: (data) => {
|
|
if (data.status === SUCCESS_STATUS) {
|
|
onSuccess?.(data);
|
|
} else {
|
|
onError?.(data);
|
|
}
|
|
},
|
|
onError: (err) => {
|
|
onError?.(err as ApiErrorResponse);
|
|
},
|
|
});
|
|
|
|
return mutation;
|
|
};
|