Skip to content

Commit

Permalink
🐛 Handle missing polls (#1455)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella authored Dec 2, 2024
1 parent 82ebcd8 commit 983c6e2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
10 changes: 8 additions & 2 deletions apps/web/src/app/[locale]/invite/[urlId]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { dehydrate, Hydrate } from "@tanstack/react-query";
import { notFound } from "next/navigation";

import { createSSRHelper } from "@/trpc/server/create-ssr-helper";

Expand All @@ -13,11 +14,16 @@ export default async function Layout({
}) {
const trpc = await createSSRHelper();

await Promise.all([
trpc.polls.get.prefetch({ urlId: params.urlId }),
const [poll] = await Promise.all([
trpc.polls.get.fetch({ urlId: params.urlId }),
trpc.polls.participants.list.prefetch({ pollId: params.urlId }),
trpc.polls.comments.list.prefetch({ pollId: params.urlId }),
]);

if (!poll) {
notFound();
}

return (
<Hydrate state={dehydrate(trpc.queryClient)}>
<Providers>{children}</Providers>
Expand Down
10 changes: 6 additions & 4 deletions apps/web/src/app/[locale]/poll/[urlId]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ export default async function Layout({
const trpc = await createSSRHelper();

// Prefetch all queries used in PollLayout
await Promise.all([
trpc.polls.get.prefetch({ urlId: params.urlId }),
const [poll] = await Promise.all([
trpc.polls.get.fetch({ urlId: params.urlId }),
trpc.polls.participants.list.prefetch({ pollId: params.urlId }),
trpc.polls.getWatchers.prefetch({ pollId: params.urlId }),
trpc.polls.comments.list.prefetch({ pollId: params.urlId }),
]);

const poll = await prisma.poll.findUnique({ where: { id: params.urlId } });
if (!poll) {
notFound();
}
Expand All @@ -36,7 +35,10 @@ export async function generateMetadata({
}: {
params: { locale: string; urlId: string };
}) {
const poll = await prisma.poll.findUnique({ where: { id: params.urlId } });
const poll = await prisma.poll.findUnique({
where: { id: params.urlId },
select: { title: true },
});

if (!poll) {
return notFound();
Expand Down
5 changes: 1 addition & 4 deletions apps/web/src/trpc/routers/polls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,7 @@ export const polls = router({
});

if (!res) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Poll not found",
});
return null;
}
const inviteLink = shortUrl(`/invite/${res.id}`);

Expand Down

0 comments on commit 983c6e2

Please sign in to comment.