From 3e9cd90f5f0620251c8e9a13e0d8e57d3bc11788 Mon Sep 17 00:00:00 2001 From: Brandon Egger Date: Thu, 16 Mar 2023 19:22:23 -0500 Subject: [PATCH] add ssg for resources --- src/components/ResourceTable.tsx | 6 +- src/pages/resources/[id].tsx | 69 +++++++++++++++++---- src/pages/resources/index.tsx | 11 +++- src/server/api/routers/auditoryResources.ts | 18 +++++- 4 files changed, 85 insertions(+), 19 deletions(-) diff --git a/src/components/ResourceTable.tsx b/src/components/ResourceTable.tsx index d65f04f..cd30914 100644 --- a/src/components/ResourceTable.tsx +++ b/src/components/ResourceTable.tsx @@ -30,7 +30,7 @@ const ResourceEntry = ({resource}: {resource: AuditoryResource}) => { return (
- +
{`${resource.name} { }) ?? []; return( -
-
+
+
diff --git a/src/pages/resources/[id].tsx b/src/pages/resources/[id].tsx index 9e0b814..fee5630 100644 --- a/src/pages/resources/[id].tsx +++ b/src/pages/resources/[id].tsx @@ -1,17 +1,60 @@ -// GOOD TUTORIAL +import { type InferGetStaticPropsType, type GetStaticPropsContext } from "next"; +import { createProxySSGHelpers } from '@trpc/react-query/ssg'; +import { appRouter } from "~/server/api/root"; +import { prisma } from "~/server/db"; +import { api } from "~/utils/api"; -export default function Resource() { - return ( - <> - Hello - - ); - } +const ResourceViewPage = (props: InferGetStaticPropsType) => { + const { id } = props; + const resourceQuery = api.auditoryResource.byId.useQuery({ id }); + + return <> +
+ {resourceQuery.data?.name ?? 'loading..'} +
+ +}; -export async function getStaticPaths() { - // Return a list of possible value for id +export const getStaticPaths = async () => { + //const amountPerPage = 10; + const resources = (await prisma.auditoryResource.findMany({ + select: { + id: true, + } + })); + //const pages = Math.ceil(objectCount / amountPerPage); + + return { + paths: resources.map((resource) => ({ + params: { + id: resource.id, + } + })), + fallback: 'blocking', + } +}; + +export async function getStaticProps( + context: GetStaticPropsContext<{ id: string }>, +) { + const ssg = createProxySSGHelpers({ + router: appRouter, + ctx: { + prisma, + session: null, + }, + }); + const id = context.params?.id as string; + + await ssg.auditoryResource.byId.prefetch({id}); + + return { + props: { + trpcState: ssg.dehydrate(), + id, + }, + revalidate: 1, + }; } -export async function getStaticProps({ params }) { - // Fetch necessary data for the blog post using params.id -} +export default ResourceViewPage \ No newline at end of file diff --git a/src/pages/resources/index.tsx b/src/pages/resources/index.tsx index b295cec..a1d5744 100644 --- a/src/pages/resources/index.tsx +++ b/src/pages/resources/index.tsx @@ -1,4 +1,5 @@ import Head from "next/head"; +import Link from "next/link"; import ResourceTable from "~/components/ResourceTable"; import { api } from "~/utils/api"; @@ -13,7 +14,15 @@ const Resources = () => {
-
+
+
+

Pages

+
    +
  • + +
  • +
+
diff --git a/src/server/api/routers/auditoryResources.ts b/src/server/api/routers/auditoryResources.ts index 8fb3590..92f7ac8 100644 --- a/src/server/api/routers/auditoryResources.ts +++ b/src/server/api/routers/auditoryResources.ts @@ -1,9 +1,23 @@ +import { z } from "zod"; + import { createTRPCRouter, publicProcedure, } from "~/server/api/trpc"; export const auditoryResourceRouter = createTRPCRouter({ - getAll: publicProcedure.query(({ ctx }) => { - return ctx.prisma.auditoryResource.findMany({ take:10 }); + byId: publicProcedure + .input(z.object({ id: z.string() })) + .query(async ({ input, ctx }) => { + const resource = await ctx.prisma.auditoryResource.findUnique({ + where: { + id: input.id, + } + }); + + return { ...resource }; }), + + getAll: publicProcedure.query(({ ctx }) => { + return ctx.prisma.auditoryResource.findMany({ take:10 }); + }), });