Use server side props to get integration data

This commit is contained in:
Bailey Pumfleet 2021-03-30 14:23:51 +01:00
parent f6c50106bd
commit d82dc10d74

View file

@ -1,11 +1,11 @@
import Head from 'next/head'; import Head from 'next/head';
import prisma from '../lib/prisma';
import Shell from '../components/Shell'; import Shell from '../components/Shell';
import { useState } from 'react'; import { useState } from 'react';
import { useSession, getSession } from 'next-auth/client'; import { useSession, getSession } from 'next-auth/client';
export default function Home() { export default function Home(props) {
const [session, loading] = useSession(); const [session, loading] = useSession();
const [integrations, setIntegrations] = useState([]);
const [showAddModal, setShowAddModal] = useState(false); const [showAddModal, setShowAddModal] = useState(false);
if (loading) { 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() { function toggleAddModal() {
setShowAddModal(!showAddModal); setShowAddModal(!showAddModal);
} }
@ -45,7 +36,7 @@ export default function Home() {
<Shell heading="Integrations"> <Shell heading="Integrations">
<div className="bg-white shadow overflow-hidden sm:rounded-lg"> <div className="bg-white shadow overflow-hidden sm:rounded-lg">
<ul className="divide-y divide-gray-200"> <ul className="divide-y divide-gray-200">
{integrations.map((integration) => {props.credentials.map((integration) =>
<li> <li>
<a href="#" className="block hover:bg-gray-50"> <a href="#" className="block hover:bg-gray-50">
<div className="flex items-center px-4 py-4 sm:px-6"> <div className="flex items-center px-4 py-4 sm:px-6">
@ -95,7 +86,7 @@ export default function Home() {
</li> </li>
)} )}
</ul> </ul>
{integrations.length == 0 && {props.credentials.length == 0 &&
<div className="bg-white shadow sm:rounded-lg"> <div className="bg-white shadow sm:rounded-lg">
<div className="flex"> <div className="flex">
<div className="py-9 pl-8"> <div className="py-9 pl-8">
@ -192,3 +183,29 @@ export default function Home() {
</div> </div>
); );
} }
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
}
}