feat: outlook 365 connect atom (#15318)

* custom for office 365 connect atom

* office 365 connect atom

* add office 365 atom to package exports

* fixup

* fixup

* fixup fixup

* refactors

* chore: refactor connect atoms

* fixup

* chore: refactor connect, enable custom redir

* fixup! Merge branch 'outlook-365-calendar-frontend' of github.com:calcom/cal.com into outlook-365-calendar-frontend

* fixup! fixup! Merge branch 'outlook-365-calendar-frontend' of github.com:calcom/cal.com into outlook-365-calendar-frontend

---------

Co-authored-by: Rajiv Sahal <[email protected]>
Co-authored-by: Morgan Vernay <[email protected]>
Co-authored-by: Morgan <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-06-06 15:08:38 +00:00
1 parent 50eeb712e3
commit 7d4e8e2210
17 files changed
+285 -195

No files matched your search

@@ -0,0 +1,69 @@
import type { FC } from "react";
import type { CALENDARS } from "@calcom/platform-constants";
import { Button } from "@calcom/ui";
import type { OnCheckErrorType } from "../hooks/connect/useCheck";
import { useCheck } from "../hooks/connect/useCheck";
import { useConnect } from "../hooks/connect/useConnect";
import { useAtomsContext } from "../hooks/useAtomsContext";
import { AtomsWrapper } from "../src/components/atoms-wrapper";
import { cn } from "../src/lib/utils";
export type OAuthConnectProps = {
className?: string;
label: string;
alreadyConnectedLabel: string;
loadingLabel: string;
onCheckError?: OnCheckErrorType;
redir?: string;
};
export const OAuthConnect: FC<OAuthConnectProps & { calendar: (typeof CALENDARS)[number] }> = ({
label,
alreadyConnectedLabel,
loadingLabel,
className,
onCheckError,
calendar,
redir,
}) => {
const { isAuth } = useAtomsContext();
const { connect } = useConnect(calendar, redir);
const { allowConnect, checked } = useCheck({
isAuth,
onCheckError,
calendar: calendar,
});
const isChecking = !isAuth || !checked;
const isDisabled = isChecking || !allowConnect;
let displayedLabel = label;
if (isChecking) {
displayedLabel = loadingLabel;
} else if (!allowConnect) {
displayedLabel = alreadyConnectedLabel;
}
return (
<AtomsWrapper>
<Button
StartIcon="calendar-days"
color="primary"
disabled={isDisabled}
className={cn(
"",
className,
isChecking && "animate-pulse",
isDisabled && "cursor-not-allowed",
!isDisabled && "cursor-pointer"
)}
onClick={() => connect()}>
{displayedLabel}
</Button>
</AtomsWrapper>
);
};
@@ -0,0 +1,27 @@
import type { FC } from "react";
import { GOOGLE_CALENDAR } from "@calcom/platform-constants";
import type { OAuthConnectProps } from "../OAuthConnect";
import { OAuthConnect } from "../OAuthConnect";
export const GcalConnect: FC<Partial<OAuthConnectProps>> = ({
label = "Connect Google Calendar",
alreadyConnectedLabel = "Connected Google Calendar",
loadingLabel = "Checking Google Calendar",
className,
onCheckError,
redir,
}) => {
return (
<OAuthConnect
label={label}
alreadyConnectedLabel={alreadyConnectedLabel}
loadingLabel={loadingLabel}
calendar={GOOGLE_CALENDAR}
className={className}
onCheckError={onCheckError}
redir={redir}
/>
);
};
+2
View File
@@ -0,0 +1,2 @@
export { GcalConnect as GoogleCalendar } from "./google/GcalConnect";
export { OutlookConnect as OutlookCalendar } from "./outlook/OutlookConnect";
@@ -0,0 +1,27 @@
import type { FC } from "react";
import { OFFICE_365_CALENDAR } from "@calcom/platform-constants";
import type { OAuthConnectProps } from "../OAuthConnect";
import { OAuthConnect } from "../OAuthConnect";
export const OutlookConnect: FC<Partial<OAuthConnectProps>> = ({
label = "Connect Outlook Calendar",
alreadyConnectedLabel = "Connected Outlook Calendar",
loadingLabel = "Checking Outlook Calendar",
className,
onCheckError,
redir,
}) => {
return (
<OAuthConnect
label={label}
alreadyConnectedLabel={alreadyConnectedLabel}
loadingLabel={loadingLabel}
calendar={OFFICE_365_CALENDAR}
className={className}
onCheckError={onCheckError}
redir={redir}
/>
);
};
@@ -1,65 +0,0 @@
import type { FC } from "react";
import { Button } from "@calcom/ui";
import { useAtomsContext } from "../hooks/useAtomsContext";
import type { OnCheckErrorType } from "../hooks/useGcal";
import { useGcal } from "../hooks/useGcal";
import { AtomsWrapper } from "../src/components/atoms-wrapper";
import { cn } from "../src/lib/utils";
interface GcalConnectProps {
className?: string;
label?: string;
alreadyConnectedLabel?: string;
onCheckError?: OnCheckErrorType;
}
/**
* 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 {OnCheckErrorType} [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,
onCheckError,
});
if (!isAuth || !checked) return <></>;
return (
<AtomsWrapper>
<Button
StartIcon="calendar-days"
color="primary"
disabled={!allowConnect}
className={cn("", className)}
onClick={() => redirectToGcalOAuth()}>
{allowConnect ? label : alreadyConnectedLabel}
</Button>
</AtomsWrapper>
);
};
@@ -1 +0,0 @@
export { GcalConnect } from "./GcalConnect";
@@ -0,0 +1,39 @@
import { useQuery } 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 interface UseCheckProps {
isAuth: boolean;
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 = ({ isAuth, onCheckError, calendar }: UseCheckProps) => {
const { data: check } = useQuery({
queryKey: getQueryKey(calendar),
staleTime: 6000,
enabled: isAuth,
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 };
};
@@ -0,0 +1,46 @@
import { useQuery } from "@tanstack/react-query";
import type { CALENDARS } from "@calcom/platform-constants";
import { SUCCESS_STATUS, ERROR_STATUS } from "@calcom/platform-constants";
import type { ApiResponse } from "@calcom/platform-types";
import http from "../../lib/http";
export const getQueryKey = (calendar: (typeof CALENDARS)[number]) => [`get-${calendar}-redirect-uri`];
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 };
};
-42
View File
@@ -1,42 +0,0 @@
import { useState, useEffect } from "react";
import type { ApiErrorResponse } from "@calcom/platform-types";
import http from "../lib/http";
export type OnCheckErrorType = (err: ApiErrorResponse) => void;
export interface useGcalProps {
isAuth: boolean;
onCheckError?: OnCheckErrorType;
}
export const useGcal = ({ isAuth, onCheckError }: useGcalProps) => {
const [allowConnect, setAllowConnect] = useState<boolean>(false);
const [checked, setChecked] = useState<boolean>(false);
const redirectToGcalOAuth = () => {
http
?.get("/gcal/oauth/auth-url")
.then(({ data: responseBody }) => {
if (responseBody.data?.authUrl) {
window.location.href = responseBody.data.authUrl;
}
})
.catch(console.error);
};
useEffect(() => {
if (isAuth) {
http
?.get("/gcal/check")
.then(() => setAllowConnect(false))
.catch((err) => {
setAllowConnect(true);
onCheckError?.(err as ApiErrorResponse);
})
.finally(() => setChecked(true));
}
}, [isAuth]);
return { allowConnect, checked, redirectToGcalOAuth };
};
+3 -1
View File
@@ -1,5 +1,5 @@
export { CalProvider } from "./cal-provider";
export { GcalConnect } from "./gcal-connect";
export { GcalConnect } from "./connect/google/GcalConnect";
export { AvailabilitySettingsPlatformWrapper as AvailabilitySettings } from "./availability";
export { BookerPlatformWrapper as Booker } from "./booker";
export { useIsPlatform } from "./hooks/useIsPlatform";
@@ -12,3 +12,5 @@ export { useCancelBooking } from "./hooks/useCancelBooking";
export { useGetBooking } from "./hooks/useGetBooking";
export { useGetBookings } from "./hooks/useGetBookings";
export { useMe } from "./hooks/useMe";
export { OutlookConnect } from "./connect/outlook/OutlookConnect";
export * as Connect from "./connect";
+3 -1
View File
@@ -17,6 +17,7 @@
"@types/react": "18.0.26",
"@types/react-dom": "^18.0.9",
"@vitejs/plugin-react": "^2.2.0",
"@vitejs/plugin-react-swc": "^3.7.0",
"autoprefixer": "^10.4.19",
"postcss": "^8.4.38",
"postcss-import": "^16.1.0",
@@ -25,7 +26,8 @@
"rollup-plugin-node-builtins": "^2.1.2",
"typescript": "^4.9.4",
"vite": "^5.0.10",
"vite-plugin-dts": "^3.7.3"
"vite-plugin-dts": "^3.7.3",
"vite-plugin-inspect": "^0.8.4"
},
"files": [
"dist",
+3 -2
View File
@@ -1,8 +1,9 @@
import react from "@vitejs/plugin-react";
import react from "@vitejs/plugin-react-swc";
import path from "path";
import { resolve } from "path";
import { defineConfig } from "vite";
import dts from "vite-plugin-dts";
import Inspect from "vite-plugin-inspect";
export default defineConfig({
optimizeDeps: {
@@ -16,7 +17,7 @@ export default defineConfig({
"@calcom/platform-utils",
],
},
plugins: [react(), dts({ insertTypesEntry: true })],
plugins: [Inspect(), react(), dts({ insertTypesEntry: true })],
build: {
commonjsOptions: {
include: [/@calcom\/lib/, /@calcom\/features/, /node_modules/],
@@ -1,7 +1,7 @@
import { Navbar } from "@/components/Navbar";
import { Inter, Poppins } from "next/font/google";
import { GcalConnect } from "@calcom/atoms";
import { GcalConnect, Connect } from "@calcom/atoms";
const inter = Inter({ subsets: ["latin"] });
const poppins = Poppins({ subsets: ["latin"], weight: ["400", "800"] });
@@ -19,7 +19,16 @@ export default function Home(props: { calUsername: string; calEmail: string }) {
<p className={`w-[70%] font-normal ${inter.className} pb-3 text-2xl`}>
To get started, connect your google calendar.
</p>
<GcalConnect className="h-[40px] bg-gradient-to-r from-[#8A2387] via-[#E94057] to-[#F27121] text-center text-base font-semibold text-transparent text-white hover:bg-orange-700" />
<div className="flex flex-row gap-4">
<GcalConnect
redir="http://localhost:4321/calendars"
className="h-[40px] bg-gradient-to-r from-[#8A2387] via-[#E94057] to-[#F27121] text-center text-base font-semibold text-transparent text-white hover:bg-orange-700"
/>
<Connect.OutlookCalendar
redir="http://localhost:4321/calendars"
className="h-[40px] bg-gradient-to-r from-[#8A2387] via-[#E94057] to-[#F27121] text-center text-base font-semibold text-transparent text-white hover:bg-orange-700"
/>
</div>
</div>
<div className="hidden lg:block">
<img