🎨 Get urlId during ssr (#521)

This commit is contained in:
Luke Vella 2023-02-24 11:01:29 +05:30 committed by GitHub
parent ce3e5540db
commit 4ee3d7cc8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View file

@ -1,6 +1,5 @@
import { GetServerSideProps } from "next"; import { GetServerSideProps } from "next";
import Head from "next/head"; import Head from "next/head";
import { useRouter } from "next/router";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import FullPageLoader from "@/components/full-page-loader"; import FullPageLoader from "@/components/full-page-loader";
@ -16,10 +15,8 @@ import { AdminControls } from "../../components/admin-control";
import ModalProvider from "../../components/modal/modal-provider"; import ModalProvider from "../../components/modal/modal-provider";
import { NextPageWithLayout } from "../../types"; import { NextPageWithLayout } from "../../types";
const Page: NextPageWithLayout = () => { const Page: NextPageWithLayout<{ urlId: string }> = ({ urlId }) => {
const { query } = useRouter();
const { t } = useTranslation("app"); const { t } = useTranslation("app");
const urlId = query.urlId as string;
const pollQuery = trpcNext.poll.getByAdminUrlId.useQuery({ urlId }); const pollQuery = trpcNext.poll.getByAdminUrlId.useQuery({ urlId });
@ -53,7 +50,16 @@ const Page: NextPageWithLayout = () => {
Page.getLayout = getStandardLayout; Page.getLayout = getStandardLayout;
export const getServerSideProps: GetServerSideProps = withSessionSsr( export const getServerSideProps: GetServerSideProps = withSessionSsr(
withPageTranslations(["common", "app", "errors"]), [
withPageTranslations(["common", "app", "errors"]),
async (ctx) => {
return {
props: {
urlId: ctx.query.urlId as string,
},
};
},
],
{ {
onPrefetch: async (ssg, ctx) => { onPrefetch: async (ssg, ctx) => {
await ssg.poll.getByAdminUrlId.fetch({ await ssg.poll.getByAdminUrlId.fetch({

View file

@ -1,7 +1,6 @@
import { GetServerSideProps } from "next"; import { GetServerSideProps } from "next";
import Head from "next/head"; import Head from "next/head";
import Link from "next/link"; import Link from "next/link";
import { useRouter } from "next/router";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { ParticipantsProvider } from "@/components/participants-provider"; import { ParticipantsProvider } from "@/components/participants-provider";
@ -16,10 +15,7 @@ import StandardLayout from "../../components/layouts/standard-layout";
import ModalProvider from "../../components/modal/modal-provider"; import ModalProvider from "../../components/modal/modal-provider";
import { NextPageWithLayout } from "../../types"; import { NextPageWithLayout } from "../../types";
const Page: NextPageWithLayout = () => { const Page: NextPageWithLayout<{ urlId: string }> = ({ urlId }) => {
const { query } = useRouter();
const urlId = query.urlId as string;
const pollQuery = trpcNext.poll.getByParticipantUrlId.useQuery({ urlId }); const pollQuery = trpcNext.poll.getByParticipantUrlId.useQuery({ urlId });
const { user } = useUser(); const { user } = useUser();
@ -62,7 +58,16 @@ Page.getLayout = function getLayout(page) {
}; };
export const getServerSideProps: GetServerSideProps = withSessionSsr( export const getServerSideProps: GetServerSideProps = withSessionSsr(
withPageTranslations(["common", "app", "errors"]), [
withPageTranslations(["common", "app", "errors"]),
async (ctx) => {
return {
props: {
urlId: ctx.query.urlId as string,
},
};
},
],
{ {
onPrefetch: async (ssg, ctx) => { onPrefetch: async (ssg, ctx) => {
await ssg.poll.getByParticipantUrlId.fetch({ await ssg.poll.getByParticipantUrlId.fetch({