refactor: update getStaticProps to getServerSideProps in user-related pages for improved data fetching

This commit is contained in:
Marco Pedone 2025-10-20 18:00:53 +02:00
parent b84c98c334
commit 11129d6ecb
7 changed files with 140 additions and 85 deletions

View file

@ -1,8 +1,9 @@
import type { GetStaticProps } from "next";
import type { GetServerSideProps } from "next";
import { AllegatiComp } from "~/components/area-riservata/allegati";
import { AreaRiservataLayoutUserView } from "~/components/Layout";
import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types";
type AllegatiProps = {
@ -20,7 +21,7 @@ const AllegatiUser: NextPageWithLayout<AllegatiProps> = ({
};
export default AllegatiUser;
export const getStaticProps: GetStaticProps = (context) => {
export const getServerSideProps = (async (context) => {
const userId = context.params?.userId;
if (typeof userId !== "string") {
return {
@ -35,17 +36,24 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true,
};
}
const access_token = context.req.cookies.access_token;
const helpers = await TrpcAuthedFetchingIstance({ access_token });
if (helpers) {
await helpers.trpc.storage.getUserStorage.prefetch({
userId: parsed.data,
});
return {
props: {
userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
},
};
};
export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] };
};
}
return {
notFound: true,
};
}) satisfies GetServerSideProps<AllegatiProps>;
AllegatiUser.getLayout = function getLayout(page) {
return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>;

View file

@ -1,8 +1,9 @@
import type { GetStaticProps } from "next";
import type { GetServerSideProps } from "next";
import { EmailAccordion } from "~/components/area-riservata/comunicazioni";
import { AreaRiservataLayoutUserView } from "~/components/Layout";
import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types";
type ComunicazioniUserProps = {
@ -28,7 +29,8 @@ const ComunicazioniUser: NextPageWithLayout<ComunicazioniUserProps> = ({
};
export default ComunicazioniUser;
export const getStaticProps: GetStaticProps = (context) => {
export const getServerSideProps = (async (context) => {
const userId = context.params?.userId;
if (typeof userId !== "string") {
return {
@ -43,13 +45,24 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true,
};
}
const access_token = context.req.cookies.access_token;
const helpers = await TrpcAuthedFetchingIstance({ access_token });
if (helpers) {
await helpers.trpc.comunicazioni.getEmails.prefetch({
userId: parsed.data,
});
return {
props: {
userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
},
};
};
}
return {
notFound: true,
};
}) satisfies GetServerSideProps<ComunicazioniUserProps>;
export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] };

View file

@ -7,7 +7,7 @@ import {
Unlock,
UserCircle,
} from "lucide-react";
import type { GetStaticProps } from "next";
import type { GetServerSideProps } from "next";
import { useRouter } from "next/router";
import toast from "react-hot-toast";
import { AreaRiservataLayoutUserView } from "~/components/Layout";
@ -32,6 +32,7 @@ import { ProfileFormAccount } from "~/forms/FormProfilo_Account";
import { ProfileFormAnagrafica } from "~/forms/FormProfilo_Anagrafica";
import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types";
import { api } from "~/utils/api";
@ -211,7 +212,7 @@ const EditUser: NextPageWithLayout<EditUserProps> = ({
};
export default EditUser;
export const getStaticProps: GetStaticProps = (context) => {
export const getServerSideProps = (async (context) => {
const userId = context.params?.userId;
if (typeof userId !== "string") {
return {
@ -226,17 +227,24 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true,
};
}
const access_token = context.req.cookies.access_token;
const helpers = await TrpcAuthedFetchingIstance({ access_token });
if (helpers) {
await helpers.trpc.users.getClientProfilo.prefetch({
id: parsed.data,
});
return {
props: {
userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
},
};
};
export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] };
};
}
return {
notFound: true,
};
}) satisfies GetServerSideProps<EditUserProps>;
EditUser.getLayout = function getLayout(page) {
return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>;

View file

@ -1,10 +1,11 @@
import type { GetStaticProps } from "next";
import type { GetServerSideProps } from "next";
import { AreaRiservataLayoutUserView } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { OrdiniTable } from "~/components/tables/orders-table";
import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types";
import { api } from "~/utils/api";
@ -37,7 +38,7 @@ const OrdiniUser: NextPageWithLayout<OrdiniUserProps> = ({
};
export default OrdiniUser;
export const getStaticProps: GetStaticProps = (context) => {
export const getServerSideProps = (async (context) => {
const userId = context.params?.userId;
if (typeof userId !== "string") {
return {
@ -52,17 +53,24 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true,
};
}
const access_token = context.req.cookies.access_token;
const helpers = await TrpcAuthedFetchingIstance({ access_token });
if (helpers) {
await helpers.trpc.servizio.getOrdini.prefetch({
userId: parsed.data,
});
return {
props: {
userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
},
};
};
export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] };
};
}
return {
notFound: true,
};
}) satisfies GetServerSideProps<OrdiniUserProps>;
OrdiniUser.getLayout = function getLayout(page) {
return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>;

View file

@ -1,9 +1,10 @@
import type { GetStaticProps } from "next";
import type { GetServerSideProps } from "next";
import { AreaRiservataLayoutUserView } from "~/components/Layout";
import { AnnunciRichiesti } from "~/components/servizio/servizio_annunci_accordions";
import { TabRicerca } from "~/components/tables/ricerca-table";
import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types";
type RicercaUserProps = {
@ -46,7 +47,7 @@ const RicercaUser: NextPageWithLayout<RicercaUserProps> = ({
RicercaUser.getLayout = (page) => {
return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>;
};
export const getStaticProps: GetStaticProps = (context) => {
export const getServerSideProps = (async (context) => {
const userId = context.params?.userId;
if (typeof userId !== "string") {
return {
@ -61,16 +62,26 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true,
};
}
const access_token = context.req.cookies.access_token;
const helpers = await TrpcAuthedFetchingIstance({ access_token });
if (helpers) {
await helpers.trpc.intrests.getUserInterestsAnnunci.prefetch({
userId: parsed.data,
});
await helpers.trpc.servizio.getAllServizioAnnunci.prefetch({
userId: parsed.data,
});
return {
props: {
userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
},
};
};
export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] };
};
}
return {
notFound: true,
};
}) satisfies GetServerSideProps<RicercaUserProps>;
export default RicercaUser;

View file

@ -1,4 +1,4 @@
import type { GetStaticProps } from "next";
import type { GetServerSideProps } from "next";
import { useState } from "react";
import toast from "react-hot-toast";
import { AreaRiservataLayoutUserView } from "~/components/Layout";
@ -16,6 +16,7 @@ import { FormNewServizio, type FormValues } from "~/forms/FormNewServizio";
import type { NextPageWithLayout } from "~/pages/_app";
import type { Servizio } from "~/schemas/public/Servizio";
import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types";
import { api } from "~/utils/api";
@ -49,7 +50,7 @@ const ServizioUser: NextPageWithLayout<ServizioUserProps> = ({
};
export default ServizioUser;
export const getStaticProps: GetStaticProps = (context) => {
export const getServerSideProps = (async (context) => {
const userId = context.params?.userId;
if (typeof userId !== "string") {
return {
@ -64,18 +65,25 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true,
};
}
const access_token = context.req.cookies.access_token;
const helpers = await TrpcAuthedFetchingIstance({ access_token });
if (helpers) {
await helpers.trpc.servizio.getUserServizi.prefetch({
userId: parsed.data,
});
return {
props: {
userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
},
};
};
export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] };
};
}
return {
notFound: true,
};
}) satisfies GetServerSideProps<ServizioUserProps>;
ServizioUser.getLayout = function getLayout(page) {
return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>;
};

View file

@ -1,39 +1,38 @@
import { z } from "zod/v4";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { GetEmails, StoreEmail } from "~/server/services/email.service";
import { genMail, type MailsTemplates } from "~/server/services/mailer";
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
import { GetEmails } from "~/server/services/email.service";
import { zUserId } from "~/server/utils/zod_types";
export const comunicazioniRouter = createTRPCRouter({
genEmail: publicProcedure
.input(
z.object({
mail: z.custom<MailsTemplates>(),
}),
)
.query(async ({ input }) => {
return await genMail({
...input.mail,
});
}),
getEmails: publicProcedure
// genEmail: publicProcedure
// .input(
// z.object({
// mail: z.custom<MailsTemplates>(),
// }),
// )
// .query(async ({ input }) => {
// return await genMail({
// ...input.mail,
// });
// }),
getEmails: protectedProcedure
.input(z.object({ userId: zUserId }))
.query(async ({ input }) => {
return await GetEmails({
userId: input.userId,
});
}),
storeEmail: publicProcedure
.input(
z.object({
data: z.custom<MailsTemplates>(),
userId: zUserId,
}),
)
.mutation(async ({ input }) => {
return await StoreEmail({
data: input.data,
userId: input.userId,
});
}),
// storeEmail: publicProcedure
// .input(
// z.object({
// data: z.custom<MailsTemplates>(),
// userId: zUserId,
// }),
// )
// .mutation(async ({ input }) => {
// return await StoreEmail({
// data: input.data,
// userId: input.userId,
// });
// }),
});