Files
calendar/packages/platform/atoms/hooks/useOAuthClient.ts
T
MorganandGitHub e641bb3d57 chore: enable platform emails (#14471)
* feat: enable platform emails

* fixup! feat: enable platform emails

* fixup! fixup! feat: enable platform emails

* fixup! Merge branch 'main' into chore-platform-emails

* fixup! fixup! Merge branch 'main' into chore-platform-emails

* fixup! fixup! fixup! Merge branch 'main' into chore-platform-emails

* chore: add oauth client repo

* chore: enable platform emails using oauth client

* fix: call getConnectedCalendar with dbWrite since it creates destinationCalendar for new credentials

* chore: update doc

* fixup! chore: update doc

* fixup! fixup! chore: update doc
2024-04-12 22:39:01 +03:00

52 lines
1.4 KiB
TypeScript

import type { AxiosError } from "axios";
import { useState, useEffect } from "react";
import { usePrevious } from "react-use";
import type { ApiResponse } from "@calcom/platform-types";
import http from "../lib/http";
export interface useOAuthClientProps {
clientId: string;
apiUrl?: string;
refreshUrl?: string;
onError: (error: string) => void;
onSuccess: () => void;
}
export const useOAuthClient = ({ clientId, apiUrl, refreshUrl, onError, onSuccess }: useOAuthClientProps) => {
const prevClientId = usePrevious(clientId);
const [isInit, setIsInit] = useState<boolean>(false);
useEffect(() => {
if (apiUrl && http.getUrl() !== apiUrl) {
http.setUrl(apiUrl);
setIsInit(true);
}
if (refreshUrl && http.getRefreshUrl() !== refreshUrl) {
http.setRefreshUrl(refreshUrl);
}
}, [apiUrl, refreshUrl]);
useEffect(() => {
if (clientId && http.getUrl() && prevClientId !== clientId) {
try {
http
.get<ApiResponse>(`/ee/provider/${clientId}`)
.then(() => {
onSuccess();
http.setClientIdHeader(clientId);
})
.catch((err: AxiosError) => {
if (err.response?.status === 401) {
onError("Invalid oAuth Client.");
}
});
} catch (err) {
console.error(err);
}
}
}, [clientId, onError, prevClientId, onSuccess]);
return { isInit };
};