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 { AllegatiComp } from "~/components/area-riservata/allegati";
import { AreaRiservataLayoutUserView } from "~/components/Layout"; import { AreaRiservataLayoutUserView } from "~/components/Layout";
import type { NextPageWithLayout } from "~/pages/_app"; import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users"; import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types"; import { zUserId } from "~/server/utils/zod_types";
type AllegatiProps = { type AllegatiProps = {
@ -20,7 +21,7 @@ const AllegatiUser: NextPageWithLayout<AllegatiProps> = ({
}; };
export default AllegatiUser; export default AllegatiUser;
export const getStaticProps: GetStaticProps = (context) => { export const getServerSideProps = (async (context) => {
const userId = context.params?.userId; const userId = context.params?.userId;
if (typeof userId !== "string") { if (typeof userId !== "string") {
return { return {
@ -35,17 +36,24 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true, 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 { return {
props: { props: {
userId: parsed.data, userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
}, },
}; };
}
return {
notFound: true,
}; };
}) satisfies GetServerSideProps<AllegatiProps>;
export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] };
};
AllegatiUser.getLayout = function getLayout(page) { AllegatiUser.getLayout = function getLayout(page) {
return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>; 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 { EmailAccordion } from "~/components/area-riservata/comunicazioni";
import { AreaRiservataLayoutUserView } from "~/components/Layout"; import { AreaRiservataLayoutUserView } from "~/components/Layout";
import type { NextPageWithLayout } from "~/pages/_app"; import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users"; import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types"; import { zUserId } from "~/server/utils/zod_types";
type ComunicazioniUserProps = { type ComunicazioniUserProps = {
@ -28,7 +29,8 @@ const ComunicazioniUser: NextPageWithLayout<ComunicazioniUserProps> = ({
}; };
export default ComunicazioniUser; export default ComunicazioniUser;
export const getStaticProps: GetStaticProps = (context) => {
export const getServerSideProps = (async (context) => {
const userId = context.params?.userId; const userId = context.params?.userId;
if (typeof userId !== "string") { if (typeof userId !== "string") {
return { return {
@ -43,13 +45,24 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true, 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 { return {
props: { props: {
userId: parsed.data, userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
}, },
}; };
}
return {
notFound: true,
}; };
}) satisfies GetServerSideProps<ComunicazioniUserProps>;
export const getStaticPaths = () => { export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] }; return { fallback: "blocking", paths: [] };

View file

@ -7,7 +7,7 @@ import {
Unlock, Unlock,
UserCircle, UserCircle,
} from "lucide-react"; } from "lucide-react";
import type { GetStaticProps } from "next"; import type { GetServerSideProps } from "next";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import { AreaRiservataLayoutUserView } from "~/components/Layout"; import { AreaRiservataLayoutUserView } from "~/components/Layout";
@ -32,6 +32,7 @@ import { ProfileFormAccount } from "~/forms/FormProfilo_Account";
import { ProfileFormAnagrafica } from "~/forms/FormProfilo_Anagrafica"; import { ProfileFormAnagrafica } from "~/forms/FormProfilo_Anagrafica";
import type { NextPageWithLayout } from "~/pages/_app"; import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users"; import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types"; import { zUserId } from "~/server/utils/zod_types";
import { api } from "~/utils/api"; import { api } from "~/utils/api";
@ -211,7 +212,7 @@ const EditUser: NextPageWithLayout<EditUserProps> = ({
}; };
export default EditUser; export default EditUser;
export const getStaticProps: GetStaticProps = (context) => { export const getServerSideProps = (async (context) => {
const userId = context.params?.userId; const userId = context.params?.userId;
if (typeof userId !== "string") { if (typeof userId !== "string") {
return { return {
@ -226,17 +227,24 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true, 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 { return {
props: { props: {
userId: parsed.data, userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
}, },
}; };
}
return {
notFound: true,
}; };
}) satisfies GetServerSideProps<EditUserProps>;
export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] };
};
EditUser.getLayout = function getLayout(page) { EditUser.getLayout = function getLayout(page) {
return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>; 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 { AreaRiservataLayoutUserView } from "~/components/Layout";
import { LoadingPage } from "~/components/loading"; import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page"; import { Status500 } from "~/components/status-page";
import { OrdiniTable } from "~/components/tables/orders-table"; import { OrdiniTable } from "~/components/tables/orders-table";
import type { NextPageWithLayout } from "~/pages/_app"; import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users"; import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types"; import { zUserId } from "~/server/utils/zod_types";
import { api } from "~/utils/api"; import { api } from "~/utils/api";
@ -37,7 +38,7 @@ const OrdiniUser: NextPageWithLayout<OrdiniUserProps> = ({
}; };
export default OrdiniUser; export default OrdiniUser;
export const getStaticProps: GetStaticProps = (context) => { export const getServerSideProps = (async (context) => {
const userId = context.params?.userId; const userId = context.params?.userId;
if (typeof userId !== "string") { if (typeof userId !== "string") {
return { return {
@ -52,17 +53,24 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true, 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 { return {
props: { props: {
userId: parsed.data, userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
}, },
}; };
}
return {
notFound: true,
}; };
}) satisfies GetServerSideProps<OrdiniUserProps>;
export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] };
};
OrdiniUser.getLayout = function getLayout(page) { OrdiniUser.getLayout = function getLayout(page) {
return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>; 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 { AreaRiservataLayoutUserView } from "~/components/Layout";
import { AnnunciRichiesti } from "~/components/servizio/servizio_annunci_accordions"; import { AnnunciRichiesti } from "~/components/servizio/servizio_annunci_accordions";
import { TabRicerca } from "~/components/tables/ricerca-table"; import { TabRicerca } from "~/components/tables/ricerca-table";
import type { NextPageWithLayout } from "~/pages/_app"; import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users"; import type { UsersId } from "~/schemas/public/Users";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zUserId } from "~/server/utils/zod_types"; import { zUserId } from "~/server/utils/zod_types";
type RicercaUserProps = { type RicercaUserProps = {
@ -46,7 +47,7 @@ const RicercaUser: NextPageWithLayout<RicercaUserProps> = ({
RicercaUser.getLayout = (page) => { RicercaUser.getLayout = (page) => {
return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>; return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>;
}; };
export const getStaticProps: GetStaticProps = (context) => { export const getServerSideProps = (async (context) => {
const userId = context.params?.userId; const userId = context.params?.userId;
if (typeof userId !== "string") { if (typeof userId !== "string") {
return { return {
@ -61,16 +62,26 @@ export const getStaticProps: GetStaticProps = (context) => {
notFound: true, 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 { return {
props: { props: {
userId: parsed.data, userId: parsed.data,
trpcState: helpers.trpc.dehydrate(),
}, },
}; };
}
return {
notFound: true,
}; };
}) satisfies GetServerSideProps<RicercaUserProps>;
export const getStaticPaths = () => {
return { fallback: "blocking", paths: [] };
};
export default RicercaUser; export default RicercaUser;

View file

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

View file

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