From d82dc10d74ae272c6634a8cd023798e18844108f Mon Sep 17 00:00:00 2001 From: Bailey Pumfleet Date: Tue, 30 Mar 2021 14:23:51 +0100 Subject: [PATCH] Use server side props to get integration data --- pages/integrations.tsx | 43 +++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/pages/integrations.tsx b/pages/integrations.tsx index 445845fa7f..538f1dc983 100644 --- a/pages/integrations.tsx +++ b/pages/integrations.tsx @@ -1,11 +1,11 @@ import Head from 'next/head'; +import prisma from '../lib/prisma'; import Shell from '../components/Shell'; import { useState } from 'react'; import { useSession, getSession } from 'next-auth/client'; -export default function Home() { +export default function Home(props) { const [session, loading] = useSession(); - const [integrations, setIntegrations] = useState([]); const [showAddModal, setShowAddModal] = useState(false); if (loading) { @@ -16,15 +16,6 @@ export default function Home() { } } - function getIntegrations() { - fetch('/api/integrations') - .then((response) => response.json()) - .then((data) => setIntegrations(data)); - } - - // TODO: Stop this function from running repeatedly - getIntegrations() - function toggleAddModal() { setShowAddModal(!showAddModal); } @@ -45,7 +36,7 @@ export default function Home() {
- {integrations.length == 0 && + {props.credentials.length == 0 &&
@@ -191,4 +182,30 @@ export default function Home() {
); +} + +export async function getServerSideProps(context) { + const session = await getSession(context); + + const user = await prisma.user.findFirst({ + where: { + email: session.user.email, + }, + select: { + id: true + } + }); + + const credentials = await prisma.credential.findMany({ + where: { + userId: user.id, + }, + select: { + type: true, + key: true + } + }); + return { + props: {credentials}, // will be passed to the page component as props + } } \ No newline at end of file