fix: platform gcal connect reconnect credentials (#14513)

* fix: enable gcal connect to reconnect revoked google credentials

* feat: callback to react when check fails
This commit is contained in:
Morgan authored and GitHub committed 2024-04-11 09:36:35 +00:00
1 parent 3fcba33b1b
commit 621067acd8
7 files changed
+94 -11

No files matched your search

@@ -3,6 +3,7 @@ import type { FC } from "react";
import { Button } from "@calcom/ui";
import { useAtomsContext } from "../hooks/useAtomsContext";
import type { OnCheckErroType } from "../hooks/useGcal";
import { useGcal } from "../hooks/useGcal";
import { AtomsWrapper } from "../src/components/atoms-wrapper";
import { cn } from "../src/lib/utils";
@@ -11,16 +12,41 @@ interface GcalConnectProps {
className?: string;
label?: string;
alreadyConnectedLabel?: string;
onCheckError?: OnCheckErroType;
}
/**
* Renders a button to connect or disconnect the Google Calendar of a user.
* @requires AccessToken - The user must be authenticated with an access token passed to CalProvider.
* @component
* @example
* ```tsx
* <GcalConnect
* label="Connect Google Calendar"
* alreadyConnectedLabel="Connected Google Calendar"
* className="my-button"
* />
* ```
*
*
* @param {string} [label="Connect Google Calendar"] - The label for the connect button. Optional.
* @param {string} [alreadyConnectedLabel="Connected Google Calendar"] - The label for the already connected button. Optional.
* @param {string} [className] - Additional CSS class name for the button. Optional.
* @param {OnCheckErroType} [onCheckError] - A callback function to handle errors when checking the connection status. Optional.
* @returns {JSX.Element} The rendered component.
*/
export const GcalConnect: FC<GcalConnectProps> = ({
label = "Connect Google Calendar",
alreadyConnectedLabel = "Connected Google Calendar",
className,
onCheckError,
}) => {
const { isAuth } = useAtomsContext();
const { allowConnect, checked, redirectToGcalOAuth } = useGcal({ isAuth });
const { allowConnect, checked, redirectToGcalOAuth } = useGcal({
isAuth,
onCheckError,
});
if (!isAuth || !checked) return <></>;
+9 -2
View File
@@ -1,12 +1,16 @@
import { useState, useEffect } from "react";
import type { ApiErrorResponse } from "@calcom/platform-types";
import http from "../lib/http";
export type OnCheckErroType = (err: ApiErrorResponse) => void;
export interface useGcalProps {
isAuth: boolean;
onCheckError?: OnCheckErroType;
}
export const useGcal = ({ isAuth }: useGcalProps) => {
export const useGcal = ({ isAuth, onCheckError }: useGcalProps) => {
const [allowConnect, setAllowConnect] = useState<boolean>(false);
const [checked, setChecked] = useState<boolean>(false);
@@ -26,7 +30,10 @@ export const useGcal = ({ isAuth }: useGcalProps) => {
http
?.get("/ee/gcal/check")
.then(() => setAllowConnect(false))
.catch(() => setAllowConnect(true))
.catch((err) => {
setAllowConnect(true);
onCheckError?.(err as ApiErrorResponse);
})
.finally(() => setChecked(true));
}
}, [isAuth]);
@@ -27,7 +27,7 @@ export default function Calendars(props: { calUsername: string; calEmail: string
connectedCalendars.map((connectedCalendar) => (
<div key={connectedCalendar.credentialId}>
<h1 className="text-md font-bold">{connectedCalendar.integration.name}</h1>
{connectedCalendar.calendars.map((calendar) => (
{connectedCalendar.calendars?.map((calendar) => (
<div key={calendar.id}>
<h2>{calendar.name}</h2>
</div>