From 1db4973aeee91725c1e2e6088d4c8f9dcd89684d Mon Sep 17 00:00:00 2001 From: Bailey Pumfleet Date: Sat, 10 Apr 2021 13:02:35 +0100 Subject: [PATCH] View and delete integrations --- lib/integrations.ts | 17 +++ pages/api/integrations.ts | 28 ++++ pages/integrations/[integration].tsx | 134 ++++++++++++++++++ .../index.tsx} | 84 +++++------ 4 files changed, 223 insertions(+), 40 deletions(-) create mode 100644 lib/integrations.ts create mode 100644 pages/integrations/[integration].tsx rename pages/{integrations.tsx => integrations/index.tsx} (73%) diff --git a/lib/integrations.ts b/lib/integrations.ts new file mode 100644 index 0000000000..940ea153f8 --- /dev/null +++ b/lib/integrations.ts @@ -0,0 +1,17 @@ +export function getIntegrationName(name: String) { + switch(name) { + case "google_calendar": + return "Google Calendar"; + default: + return "Unknown"; + } +} + +export function getIntegrationType(name: String) { + switch(name) { + case "google_calendar": + return "Calendar"; + default: + return "Unknown"; + } +} \ No newline at end of file diff --git a/pages/api/integrations.ts b/pages/api/integrations.ts index 7401a98de5..df39f6919f 100644 --- a/pages/api/integrations.ts +++ b/pages/api/integrations.ts @@ -30,4 +30,32 @@ export default async function handler(req, res) { res.status(200).json(credentials); } + + if (req.method == "DELETE") { + const session = await getSession({req: req}); + + if (!session) { res.status(401).json({message: 'You must be logged in to do this'}); return; } + + // TODO: Add user ID to user session object + const user = await prisma.user.findFirst({ + where: { + email: session.user.email, + }, + select: { + id: true + } + }); + + if (!user) { res.status(404).json({message: 'User not found'}); return; } + + const id = req.body.id; + + const deleteIntegration = await prisma.credential.delete({ + where: { + id: id, + }, + }); + + res.status(200).json({message: 'Integration deleted successfully'}); + } } \ No newline at end of file diff --git a/pages/integrations/[integration].tsx b/pages/integrations/[integration].tsx new file mode 100644 index 0000000000..6366122a0d --- /dev/null +++ b/pages/integrations/[integration].tsx @@ -0,0 +1,134 @@ +import Head from 'next/head'; +import prisma from '../../lib/prisma'; +import { getIntegrationName, getIntegrationType } from '../../lib/integrations'; +import Shell from '../../components/Shell'; +import { useState } from 'react'; +import { useRouter } from 'next/router'; +import { useSession, getSession } from 'next-auth/client'; + +export default function integration(props) { + const router = useRouter(); + const [session, loading] = useSession(); + const [showAPIKey, setShowAPIKey] = useState(false); + + if (loading) { + return

Loading...

; + } else { + if (!session) { + window.location.href = "/"; + } + } + + function toggleShowAPIKey() { + setShowAPIKey(!showAPIKey); + } + + async function deleteIntegrationHandler(event) { + event.preventDefault(); + + const response = await fetch('/api/integrations', { + method: 'DELETE', + body: JSON.stringify({id: props.integration.id}), + headers: { + 'Content-Type': 'application/json' + } + }); + + router.push('/integrations'); + } + + return( +
+ + {getIntegrationName(props.integration.type)} | Integrations | Calendso + + + + +
+
+
+

+ Integration Details +

+

+ Information about your {getIntegrationName(props.integration.type)} integration. +

+
+
+
+
+
+ Integration name +
+
+ {getIntegrationName(props.integration.type)} +
+
+
+
+ Integration type +
+
+ {getIntegrationType(props.integration.type)} +
+
+
+
+ API Key +
+
+ {!showAPIKey ? + •••••••• + : +
+ +
} + +
+
+
+
+
+
+
+
+

+ Delete this integration +

+
+

+ Once you delete this integration, it will be permanently removed. +

+
+
+ +
+
+
+
+
+
+
+ ); +} + +export async function getServerSideProps(context) { + const session = await getSession(context); + + const integration = await prisma.credential.findFirst({ + where: { + id: parseInt(context.query.integration), + }, + select: { + id: true, + type: true, + key: true + } + }); + return { + props: {integration}, // will be passed to the page component as props + } +} \ No newline at end of file diff --git a/pages/integrations.tsx b/pages/integrations/index.tsx similarity index 73% rename from pages/integrations.tsx rename to pages/integrations/index.tsx index 538f1dc983..6b0bd14228 100644 --- a/pages/integrations.tsx +++ b/pages/integrations/index.tsx @@ -1,6 +1,7 @@ import Head from 'next/head'; -import prisma from '../lib/prisma'; -import Shell from '../components/Shell'; +import Link from 'next/link'; +import prisma from '../../lib/prisma'; +import Shell from '../../components/Shell'; import { useState } from 'react'; import { useSession, getSession } from 'next-auth/client'; @@ -38,51 +39,53 @@ export default function Home(props) { @@ -201,6 +204,7 @@ export async function getServerSideProps(context) { userId: user.id, }, select: { + id: true, type: true, key: true }