Skip to content

Commit

Permalink
View and delete integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
baileypumfleet committed Apr 10, 2021
1 parent ac550f5 commit 1db4973
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 40 deletions.
17 changes: 17 additions & 0 deletions 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";
}
}
28 changes: 28 additions & 0 deletions pages/api/integrations.ts
Expand Up @@ -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'});
}
}
134 changes: 134 additions & 0 deletions 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 <p className="text-gray-400">Loading...</p>;
} 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(
<div>
<Head>
<title>{getIntegrationName(props.integration.type)} | Integrations | Calendso</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<Shell heading={getIntegrationName(props.integration.type)}>
<div className="grid grid-cols-3 gap-4">
<div className="col-span-2 bg-white shadow overflow-hidden sm:rounded-lg">
<div className="px-4 py-5 sm:px-6">
<h3 className="text-lg leading-6 font-medium text-gray-900">
Integration Details
</h3>
<p className="mt-1 max-w-2xl text-sm text-gray-500">
Information about your {getIntegrationName(props.integration.type)} integration.
</p>
</div>
<div className="border-t border-gray-200 px-4 py-5 sm:px-6">
<dl className="grid gap-y-8">
<div>
<dt className="text-sm font-medium text-gray-500">
Integration name
</dt>
<dd className="mt-1 text-sm text-gray-900">
{getIntegrationName(props.integration.type)}
</dd>
</div>
<div>
<dt className="text-sm font-medium text-gray-500">
Integration type
</dt>
<dd className="mt-1 text-sm text-gray-900">
{getIntegrationType(props.integration.type)}
</dd>
</div>
<div>
<dt className="text-sm font-medium text-gray-500">
API Key
</dt>
<dd className="mt-1 text-sm text-gray-900">
{!showAPIKey ?
<span>&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;</span>
:
<div>
<textarea name="apikey" rows={6} className="shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full sm:text-sm border-gray-300 rounded-md" readOnly>{JSON.stringify(props.integration.key)}</textarea>
</div>}
<button onClick={toggleShowAPIKey} className="ml-2 font-medium text-blue-600 hover:text-blue-700">{!showAPIKey ? 'Show' : 'Hide'}</button>
</dd>
</div>
</dl>
</div>
</div>
<div>
<div className="bg-white shadow sm:rounded-lg">
<div className="px-4 py-5 sm:p-6">
<h3 className="text-lg leading-6 font-medium text-gray-900">
Delete this integration
</h3>
<div className="mt-2 max-w-xl text-sm text-gray-500">
<p>
Once you delete this integration, it will be permanently removed.
</p>
</div>
<div className="mt-5">
<button onClick={deleteIntegrationHandler} type="button" className="inline-flex items-center justify-center px-4 py-2 border border-transparent font-medium rounded-md text-red-700 bg-red-100 hover:bg-red-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:text-sm">
Delete integration
</button>
</div>
</div>
</div>
</div>
</div>
</Shell>
</div>
);
}

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
}
}
84 changes: 44 additions & 40 deletions pages/integrations.tsx → 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';

Expand Down Expand Up @@ -38,51 +39,53 @@ export default function Home(props) {
<ul className="divide-y divide-gray-200">
{props.credentials.map((integration) =>
<li>
<a href="#" className="block hover:bg-gray-50">
<div className="flex items-center px-4 py-4 sm:px-6">
<div className="min-w-0 flex-1 flex items-center">
<div className="flex-shrink-0">
{integration.type == 'google_calendar' && <img className="h-10 w-10 mr-2" src="integrations/google-calendar.png" alt="Google Calendar" />}
</div>
<div className="min-w-0 flex-1 px-4 md:grid md:grid-cols-2 md:gap-4">
<div>
{integration.type == 'google_calendar' && <p className="text-sm font-medium text-blue-600 truncate">Google Calendar</p>}
<p className="mt-2 flex items-center text-sm text-gray-500">
{integration.type == 'google_calendar' && <span className="truncate">Calendar Integration</span>}
</p>
<Link href={"/integrations/" + integration.id}>
<a className="block hover:bg-gray-50">
<div className="flex items-center px-4 py-4 sm:px-6">
<div className="min-w-0 flex-1 flex items-center">
<div className="flex-shrink-0">
{integration.type == 'google_calendar' && <img className="h-10 w-10 mr-2" src="integrations/google-calendar.png" alt="Google Calendar" />}
</div>
<div className="hidden md:block">
<div className="min-w-0 flex-1 px-4 md:grid md:grid-cols-2 md:gap-4">
<div>
{integration.key &&
<p className="mt-3 flex items-center text text-gray-500">
<svg className="flex-shrink-0 mr-1.5 h-5 w-5 text-green-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clipRule="evenodd" />
</svg>
Connected
</p>
}
{!integration.key &&
<p className="mt-3 flex items-center text text-gray-500">
<svg className="flex-shrink-0 mr-1.5 h-5 w-5 text-yellow-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fillRule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clipRule="evenodd" />
</svg>
Not connected
{integration.type == 'google_calendar' && <p className="text-sm font-medium text-blue-600 truncate">Google Calendar</p>}
<p className="mt-2 flex items-center text-sm text-gray-500">
{integration.type == 'google_calendar' && <span className="truncate">Calendar Integration</span>}
</p>
}
</div>
<div className="hidden md:block">
<div>
{integration.key &&
<p className="mt-3 flex items-center text text-gray-500">
<svg className="flex-shrink-0 mr-1.5 h-5 w-5 text-green-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clipRule="evenodd" />
</svg>
Connected
</p>
}
{!integration.key &&
<p className="mt-3 flex items-center text text-gray-500">
<svg className="flex-shrink-0 mr-1.5 h-5 w-5 text-yellow-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fillRule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clipRule="evenodd" />
</svg>
Not connected
</p>
}
</div>
</div>
</div>
</div>
<div>
<svg className="h-5 w-5 text-gray-400" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fillRule="evenodd"
d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z"
clipRule="evenodd" />
</svg>
</div>
</div>
<div>
<svg className="h-5 w-5 text-gray-400" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fillRule="evenodd"
d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z"
clipRule="evenodd" />
</svg>
</div>
</div>
</a>
</a>
</Link>
</li>
)}
</ul>
Expand Down Expand Up @@ -201,6 +204,7 @@ export async function getServerSideProps(context) {
userId: user.id,
},
select: {
id: true,
type: true,
key: true
}
Expand Down

0 comments on commit 1db4973

Please sign in to comment.