️ Remove getServerSideProps on invite page + handle 404

This commit is contained in:
Luke Vella 2023-06-20 11:25:34 +01:00
parent 5bbc7248bf
commit cd05d7ab88
6 changed files with 76 additions and 54 deletions

View file

@ -47,6 +47,7 @@ import { Skeleton } from "@/components/skeleton";
import { Trans } from "@/components/trans";
import { useUser } from "@/components/user-provider";
import { usePoll } from "@/contexts/poll";
import Error404 from "@/pages/404";
import { NextPageWithLayout } from "../../types";
@ -261,6 +262,10 @@ const Prefetch = ({ children }: React.PropsWithChildren) => {
const participants = trpc.polls.participants.list.useQuery({ pollId: urlId });
const watchers = trpc.polls.getWatchers.useQuery({ pollId: urlId });
if (poll.error?.data?.code === "NOT_FOUND") {
return <Error404 />;
}
if (!poll.isFetched || !watchers.isFetched || !participants.isFetched) {
return (
<div>

View file

@ -1,12 +1,11 @@
import { FileSearchIcon } from "@rallly/icons";
import { GetStaticProps } from "next";
import { useTranslation } from "next-i18next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import React from "react";
import ErrorPage from "@/components/error-page";
import { getStandardLayout } from "@/components/layouts/standard-layout";
import { NextPageWithLayout } from "@/types";
import { withPageTranslations } from "@/utils/with-page-translations";
const Custom404: NextPageWithLayout = () => {
const { t } = useTranslation();
@ -21,12 +20,6 @@ const Custom404: NextPageWithLayout = () => {
Custom404.getLayout = getStandardLayout;
export const getStaticProps: GetStaticProps = async ({ locale = "en" }) => {
return {
props: {
...(await serverSideTranslations(locale)),
},
};
};
export const getStaticProps = withPageTranslations();
export default Custom404;

View file

@ -1,10 +1,10 @@
import { withSessionSsr } from "@rallly/backend/next";
import { decryptToken } from "@rallly/backend/session";
import { trpc } from "@rallly/backend";
import { ArrowUpLeftIcon } from "@rallly/icons";
import { Button } from "@rallly/ui/button";
import { GetServerSideProps } from "next";
import Head from "next/head";
import Link from "next/link";
import { useRouter } from "next/router";
import React from "react";
import { Poll } from "@/components/poll";
import { LegacyPollContextProvider } from "@/components/poll/poll-context-provider";
@ -12,7 +12,48 @@ import { Trans } from "@/components/trans";
import { useUser } from "@/components/user-provider";
import { PermissionsContext } from "@/contexts/permissions";
import { usePoll } from "@/contexts/poll";
import { withPageTranslations } from "@/utils/with-page-translations";
import { getStaticTranslations } from "@/utils/with-page-translations";
import Error404 from "../404";
const Prefetch = ({ children }: React.PropsWithChildren) => {
const router = useRouter();
const [urlId] = React.useState(router.query.urlId as string);
const { data: permission } = trpc.auth.getUserPermission.useQuery(
{ token: router.query.token as string },
{
enabled: !!router.query.token,
},
);
const { data: poll, error } = trpc.polls.get.useQuery(
{ urlId },
{
retry: false,
},
);
const { data: participants } = trpc.polls.participants.list.useQuery({
pollId: urlId,
});
if (error?.data?.code === "NOT_FOUND") {
return <Error404 />;
}
if (!poll || !participants) {
return null;
}
return (
<PermissionsContext.Provider value={{ userId: permission?.userId ?? null }}>
<Head>
<title>{poll.title}</title>
</Head>
{children}
</PermissionsContext.Provider>
);
};
const GoToApp = () => {
const poll = usePoll();
@ -37,13 +78,9 @@ const GoToApp = () => {
);
};
const Page = ({ forceUserId }: { forceUserId: string }) => {
const poll = usePoll();
const Page = () => {
return (
<PermissionsContext.Provider value={{ userId: forceUserId }}>
<Head>
<title>{poll.title}</title>
</Head>
<Prefetch>
<LegacyPollContextProvider>
<div className="">
<svg
@ -92,42 +129,17 @@ const Page = ({ forceUserId }: { forceUserId: string }) => {
</div>
</div>
</LegacyPollContextProvider>
</PermissionsContext.Provider>
</Prefetch>
);
};
export const getServerSideProps: GetServerSideProps = withSessionSsr(
[
withPageTranslations(),
async (ctx) => {
if (ctx.query.token) {
const res = await decryptToken<{ userId: string }>(
ctx.query.token as string,
);
if (res) {
export const getStaticPaths = async () => {
return {
props: {
forceUserId: res.userId,
},
paths: [], //indicates that no page needs be created at build time
fallback: "blocking", //indicates the type of fallback
};
}
}
};
return { props: {} };
},
],
{
onPrefetch: async (ssg, ctx) => {
const poll = await ssg.polls.get.fetch({
urlId: ctx.params?.urlId as string,
});
await ssg.polls.participants.list.prefetch({
pollId: poll.id,
});
},
},
);
export const getStaticProps = getStaticTranslations;
export default Page;

View file

@ -65,8 +65,8 @@ Page.getLayout = getPollLayout;
export const getStaticPaths = async () => {
return {
paths: [], //indicates that no page needs be created at build time
fallback: "blocking", //indicates the type of fallback
paths: [],
fallback: "blocking",
};
};

View file

@ -20,6 +20,7 @@ export const trpc = createTRPCNext<AppRouter>({
queryClientConfig: {
defaultOptions: {
queries: {
retry: false,
networkMode: "always",
cacheTime: Infinity,
staleTime: 1000 * 60,

View file

@ -247,4 +247,15 @@ export const auth = router({
return { user };
}),
getUserPermission: publicProcedure
.input(z.object({ token: z.string() }))
.query(async ({ input }) => {
const res = await decryptToken<{ userId: string }>(input.token);
if (!res) {
return null;
}
return res;
}),
});