chore: start laying out the home page

* Use the layout component
* Use SSR to get videos from API
This commit is contained in:
Sean Norwood 2022-04-04 02:52:03 +00:00
parent 4fa8cd89e2
commit 9284b93f2a

View File

@ -1,9 +1,9 @@
import type { NextPage } from "next"; import type { GetServerSideProps, NextPage } from "next";
import { signIn, signOut, useSession } from "next-auth/react"; import { signIn, signOut, useSession } from "next-auth/react";
import Head from "next/head"; import { Videos } from "../types/video";
import { Suspense } from "react";
import { CustomHead } from "../components/CustomHead"; import { CustomHead } from "../components/CustomHead";
import { DynamicHeader } from "../components/Header"; import { Layout } from "../components/Layout";
import { VideoList } from "../components/VideoList";
const SignInOutButton = ({ isSignedIn }: { isSignedIn: boolean }) => { const SignInOutButton = ({ isSignedIn }: { isSignedIn: boolean }) => {
if (isSignedIn) { if (isSignedIn) {
@ -12,7 +12,7 @@ const SignInOutButton = ({ isSignedIn }: { isSignedIn: boolean }) => {
return <button onClick={() => signIn()}>Sign in</button>; return <button onClick={() => signIn()}>Sign in</button>;
}; };
const Home: NextPage = () => { const Home: NextPage<{ videos: Videos }> = ({ videos }) => {
const { data: session, status } = useSession(); const { data: session, status } = useSession();
const authData = { const authData = {
session, session,
@ -23,21 +23,26 @@ const Home: NextPage = () => {
<> <>
<CustomHead /> <CustomHead />
<Suspense fallback={<h1>Loading</h1>}> <Layout>
<DynamicHeader authData={authData} /> <VideoList videos={videos} />
</Suspense>
<div
style={{
display: "flex",
flexDirection: "column",
maxWidth: "100px",
}}
>
<SignInOutButton isSignedIn={!!session?.user} /> <SignInOutButton isSignedIn={!!session?.user} />
</div> </Layout>
</> </>
); );
}; };
export default Home; export default Home;
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const response = await fetch("http://localhost:8000/api/video/", {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Token b4d4330462c7fc16c51873e45579b29a1a12fc90`,
mode: "no-cors",
},
});
const videos = await response.json();
return { props: { videos } };
};