Files
calendar/example-apps/credential-sync/pages/index.tsx
T
d47c6b3fdb fix: Credential Syncing Improvements (#14588)
* Add example app to test credential sync

* Fixes

* Changes to normalize flow of GoogleCalendar and Zoom

* Add unit tests

* PR Feedback

* credential-sync-more-tests-and-more-apps

* Fix yarn.lock

* Clear cache

* Add test

* Fix yarn.lock

* Fix 204 handling

* Fix yarn.lock

---------

Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
2024-04-25 12:47:17 -04:00

58 lines
1.8 KiB
TypeScript

import { useRouter, useSearchParams, usePathname } from "next/navigation";
import { useEffect, useState } from "react";
export default function Index() {
const [data, setData] = useState("");
const router = useRouter();
const searchParams = useSearchParams();
const pathname = usePathname();
const appSlug = searchParams.get("appSlug");
const userId = searchParams.get("userId");
useEffect(() => {
let isRedirectNeeded = false;
const newSearchParams = new URLSearchParams(new URL(document.URL).searchParams);
if (!userId) {
newSearchParams.set("userId", "1");
isRedirectNeeded = true;
}
if (!appSlug) {
newSearchParams.set("appSlug", "google-calendar");
isRedirectNeeded = true;
}
if (isRedirectNeeded) {
router.push(`${pathname}?${newSearchParams.toString()}`);
}
}, [router, pathname, userId, appSlug]);
async function updateToken({ invalid } = { invalid: false }) {
const res = await fetch(
`/api/setTokenInCalCom?invalid=${invalid ? 1 : 0}&userId=${userId}&appSlug=${appSlug}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
},
}
);
const data = await res.json();
setData(JSON.stringify(data));
}
return (
<div>
<h1>Welcome to Credential Sync Playground</h1>
<p>
You are managing credentials for cal.com <strong>userId={userId}</strong> for{" "}
<strong>appSlug={appSlug}</strong>. Update query params to manage a different user or app{" "}
</p>
<button onClick={() => updateToken({ invalid: true })}>Give an invalid token to Cal.com</button>
<button onClick={() => updateToken()}>Give a valid token to Cal.com</button>
<div>{data}</div>
</div>
);
}