feat: implement redirectTo500 utility for consistent error handling across pages

This commit is contained in:
Marco Pedone 2026-03-17 18:44:53 +01:00
parent 162c456834
commit 7e57f128f4
24 changed files with 74 additions and 226 deletions

View file

@ -14,7 +14,6 @@ const PagamentoConferma = () => {
<Section className="px-5 text-left text-base md:px-12">
<Row>
<Text>
{/**TODO correggere */}
Gentile Cliente, la informiamo che il pagamento è stato confermato
con successo.
</Text>
@ -22,8 +21,8 @@ const PagamentoConferma = () => {
Puoi controllare lo stato del tuo account nella sezione dedicata.
</Text>
<Text>
In allegato troverai una ricevuta di cortesia, la fattura
eletnica arriverà separatamente.
In allegato troverai una ricevuta di cortesia, la fattura eletnica
arriverà separatamente.
</Text>
</Row>
</Section>

View file

@ -1133,10 +1133,7 @@ export const AnnuncioEditForm = ({ data }: { data: AnnunciWithMedia }) => {
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent
align="start"
className="w-auto p-0"
>
<PopoverContent align="end" className="w-auto p-0">
<Calendar
autoFocus
captionLayout="dropdown"

View file

@ -234,7 +234,7 @@ export const FormEditOrder = ({ prezziario, data, close }: FormProps) => {
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent align="start" className="w-auto p-0">
<PopoverContent align="end" className="w-auto p-0">
<Calendar
autoFocus
captionLayout="dropdown"

View file

@ -164,7 +164,7 @@ export const FormEditServizio = ({
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent align="start" className="w-auto p-0">
<PopoverContent align="end" className="w-auto p-0">
<Calendar
autoFocus
captionLayout="dropdown"

View file

@ -210,7 +210,7 @@ export const FormNewOrder = ({
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent align="start" className="w-auto p-0">
<PopoverContent align="end" className="w-auto p-0">
<Calendar
autoFocus
captionLayout="dropdown"

View file

@ -206,7 +206,7 @@ export const ProfileFormAnagrafica = ({
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent align="start" className="w-auto p-0">
<PopoverContent align="end" className="w-auto p-0">
<Calendar
autoFocus
captionLayout="dropdown"

View file

@ -509,7 +509,7 @@ export const FormServizio = ({
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent align="start" className="w-auto p-0">
<PopoverContent align="end" className="w-auto p-0">
<Calendar
autoFocus
captionLayout="dropdown"

View file

@ -764,7 +764,7 @@ export const FormServizioAcquisto = ({
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent align="start" className="w-auto p-0">
<PopoverContent align="end" className="w-auto p-0">
<Calendar
autoFocus
captionLayout="dropdown"
@ -927,7 +927,7 @@ export const FormServizioAcquisto = ({
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent align="start" className="w-auto p-0">
<PopoverContent align="end" className="w-auto p-0">
<Calendar
autoFocus
captionLayout="dropdown"

View file

@ -5,6 +5,13 @@ export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export const redirectTo500 = {
redirect: {
destination: "/500",
permanent: false,
},
} as const;
export function getInitials(name: string) {
name = name.trim();

View file

@ -4,7 +4,7 @@ import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { Badge } from "~/components/ui/badge";
import { AnnuncioEditForm } from "~/forms/FormEditAnnuncio";
import { cn } from "~/lib/utils";
import { cn, redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import type { AnnunciId } from "~/schemas/public/Annunci";
import { generateSSGHelper } from "~/server/utils/ssgHelper";
@ -45,12 +45,7 @@ export const getServerSideProps = (async (context) => {
const check = zAnnuncioId.safeParse(annuncioId);
if (!check.success) {
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const ssg = generateSSGHelper();

View file

@ -3,6 +3,7 @@ import { AreaRiservataLayoutUserView } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { FormServizioAcquisto } from "~/forms/FormServizioAcquisto";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import { CatastoProvider } from "~/providers/CatastoProvider";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
@ -56,23 +57,13 @@ export const getServerSideProps = (async (context) => {
if (typeof id !== "string") {
console.error("Error: servizioId is not a string");
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const parsed = zServizioId.safeParse(id);
if (!parsed.success) {
console.error("Error parsing servizioId", parsed.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const access_token = context.req.cookies.access_token;
@ -104,12 +95,7 @@ export const getServerSideProps = (async (context) => {
};
}
return {
redirect: {
destination: `/area-riservata/admin/utenti`,
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<OnboardUserProps>;
OnboardUser.getLayout = function getLayout(page) {

View file

@ -4,6 +4,7 @@ import { LoadingPage } from "~/components/loading";
import { Servizio } from "~/components/servizio/servizio";
import { AnnunciRichiesti } from "~/components/servizio/servizio_annunci_accordions";
import { Status500 } from "~/components/status-page";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
import type { UsersId } from "~/schemas/public/Users";
@ -52,33 +53,18 @@ export const getServerSideProps = (async (context) => {
if (typeof servizio_id !== "string" || typeof user_id !== "string") {
console.error("Error: servizioId or userId is not a string");
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const parsedUserId = zUserId.safeParse(user_id);
if (!parsedUserId.success) {
console.error("Error parsing userId", parsedUserId.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const parsedServizioId = zServizioId.safeParse(servizio_id);
if (!parsedServizioId.success) {
console.error("Error parsing servizioId", parsedServizioId.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const servizioId = parsedServizioId.data;
const userId = parsedUserId.data;
@ -100,12 +86,7 @@ export const getServerSideProps = (async (context) => {
};
}
return {
redirect: {
destination: `/area-riservata/admin/utenti`,
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<ServizioUserProps>;
ServizioUser.getLayout = function getLayout(page) {

View file

@ -22,6 +22,7 @@ import {
} from "~/components/ui/dialog";
import { Input } from "~/components/ui/input";
import { getStorageUrl } from "~/lib/storage_utils";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
import { useEnforcedSession } from "~/providers/SessionProvider";
@ -151,12 +152,7 @@ export const getServerSideProps = (async (context) => {
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<AllegatoViewProps>;
export default AllegatoView;

View file

@ -5,6 +5,7 @@ import {
SchedaAnnuncio,
SchedaAnnuncioStampabile,
} from "~/components/schedaAnnuncioStampabile";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import type { AnnunciWithMedia } from "~/server/controllers/annunci.controller";
import { generateSSGHelper } from "~/server/utils/ssgHelper";
@ -53,23 +54,13 @@ export const getServerSideProps = (async (context) => {
if (typeof id !== "string") {
console.error("Error: id is not a string");
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const parsed = zAnnuncioId.safeParse(parseInt(id));
if (!parsed.success) {
console.error("Error parsing ordineId", parsed.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const helper = generateSSGHelper();
@ -87,7 +78,7 @@ export const getServerSideProps = (async (context) => {
props: {
raw: raw === "true",
trpcState: helper.dehydrate(),
data : AnnuncioToStringified(data),
data: AnnuncioToStringified(data),
},
};
}) satisfies GetServerSideProps<PageProps>;

View file

@ -4,6 +4,7 @@ import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Servizio } from "~/components/servizio/servizio";
import { Status500 } from "~/components/status-page";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import { useEnforcedSession } from "~/providers/SessionProvider";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
@ -34,21 +35,6 @@ const ServizioPage: NextPageWithLayout<ServizioPageProps> = ({
}
return (
<div className="flex w-full flex-1 flex-col items-start justify-center gap-3 overflow-auto p-2 md:gap-6">
{/* <div className=" w-full flex-col gap-2 flex">
<div className="flex w-full items-center justify-between">
<h3 className="font-semibold text-xl">
{t.servizio.dettaglio_servizio}
</h3>
<Link href="/area-riservata/dashboard">
<Button size="sm" variant="outline">
<ArrowRight className="size-4" />
<span>lista servizi</span>
</Button>
</Link>
</div>
<Separator />
</div> */}
<div className="flex w-full flex-1 grow flex-col space-y-5">
<Servizio
isAdmin={false}
@ -70,23 +56,13 @@ export const getServerSideProps = (async (context) => {
if (typeof servizio_id !== "string") {
console.error("Error: servizioId is not a string");
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const parsedServizioId = zServizioId.safeParse(servizio_id);
if (!parsedServizioId.success) {
console.error("Error parsing servizioId", parsedServizioId.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const servizioId = parsedServizioId.data;
@ -106,12 +82,7 @@ export const getServerSideProps = (async (context) => {
};
}
return {
redirect: {
destination: `/area-riservata/admin/utenti`,
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<ServizioPageProps>;
ServizioPage.getLayout = function getLayout(page) {

View file

@ -3,7 +3,7 @@ import type { GetServerSideProps } from "next";
import Link from "next/link";
import { AreaRiservataLayout } from "~/components/Layout";
import { Button } from "~/components/ui/button";
import { formatCurrency } from "~/lib/utils";
import { formatCurrency, redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zOrdineId } from "~/server/utils/zod_types";
@ -78,23 +78,13 @@ export const getServerSideProps = (async (context) => {
const id = context.params?.ordineId;
if (typeof id !== "string") {
console.error("Error: ordineId is not a string");
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const parsed = zOrdineId.safeParse(id);
if (!parsed.success) {
console.error("Error parsing ordineId", parsed.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const access_token = context.req.cookies.access_token;
@ -115,10 +105,5 @@ export const getServerSideProps = (async (context) => {
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<BonificoManualeProps>;

View file

@ -2,6 +2,7 @@ import type { GetServerSideProps } from "next";
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import type { TestiEStringheStingaId } from "~/schemas/public/TestiEStringhe";
import { generateSSGHelper } from "~/server/utils/ssgHelper";
@ -53,23 +54,13 @@ export const getServerSideProps = (async (context) => {
const raw = context.query?.raw;
if (typeof id !== "string") {
console.error("Error: stringaId is not a string");
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const parsed = zTestiEStringheStingaId.safeParse(id);
if (!parsed.success) {
console.error("Error parsing paymentId", parsed.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const helper = generateSSGHelper();
@ -86,10 +77,5 @@ export const getServerSideProps = (async (context) => {
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<CondizioniProps>;

View file

@ -9,6 +9,7 @@ import { FileSection } from "~/components/servizio/conferma";
import { Status500 } from "~/components/status-page";
import { Button } from "~/components/ui/button";
import { Checkbox } from "~/components/ui/checkbox";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
import { useEnforcedSession } from "~/providers/SessionProvider";
@ -214,10 +215,5 @@ export const getServerSideProps = (async (context) => {
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<ConfermaImmobileProps>;

View file

@ -6,6 +6,7 @@ import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { getStorageUrl } from "~/lib/storage_utils";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
import type { AnnunciId } from "~/schemas/public/Annunci";
@ -120,10 +121,5 @@ export const getServerSideProps = (async (context) => {
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<ContrattoViewerProps>;

View file

@ -6,6 +6,7 @@ import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { getStorageUrl } from "~/lib/storage_utils";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
import type { AnnunciId } from "~/schemas/public/Annunci";
@ -116,10 +117,5 @@ export const getServerSideProps = (async (context) => {
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<ContrattoViewerProps>;

View file

@ -4,6 +4,7 @@ import { LoadingPage } from "~/components/loading";
import { OnboardTutorial } from "~/components/onboard_tutorial";
import { Status500 } from "~/components/status-page";
import { FormServizioAcquisto } from "~/forms/FormServizioAcquisto";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import { CatastoProvider } from "~/providers/CatastoProvider";
import { useTranslation } from "~/providers/I18nProvider";
@ -65,23 +66,13 @@ export const getServerSideProps = (async (context) => {
if (typeof id !== "string") {
console.error("Error: servizioId is not a string");
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const parsed = zServizioId.safeParse(id);
if (!parsed.success) {
console.error("Error parsing servizioId", parsed.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const access_token = context.req.cookies.access_token;

View file

@ -6,10 +6,10 @@ import PaymentStatus from "~/components/payment_status";
import { ProgressRedirect } from "~/components/progress_redirect";
import { Button, buttonVariants } from "~/components/ui/button";
import stripe from "~/lib/stripe";
import { cn } from "~/lib/utils";
import { cn, redirectTo500 } from "~/lib/utils";
import { useTranslation } from "~/providers/I18nProvider";
import OrderTypeEnum from "~/schemas/public/OrderTypeEnum";
import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
enum AcquistoStatusEnum {
@ -21,26 +21,21 @@ enum AcquistoStatusEnum {
}
type AcquistoProcessingProps = {
servizioId: ServizioServizioId | null;
redirectUrl: string;
status: AcquistoStatusEnum;
};
export const getServerSideProps = (async (context) => {
const paymentIntentId = context.query?.payment_intent;
if (!paymentIntentId || typeof paymentIntentId !== "string") {
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId);
if (!paymentIntent) {
return {
props: {
servizioId: null,
redirectUrl: "",
status: AcquistoStatusEnum.internal_error,
},
};
@ -57,7 +52,7 @@ export const getServerSideProps = (async (context) => {
if (!payment) {
return {
props: {
servizioId: null,
redirectUrl: "",
status: AcquistoStatusEnum.not_found,
},
};
@ -79,25 +74,23 @@ export const getServerSideProps = (async (context) => {
}
return {
props: {
servizioId: payment.servizio_id,
redirectUrl:
payment.type === OrderTypeEnum.Rinnovo
? `/area-riservata/rinnovo/${payment.rinnovo_id}`
: `/area-riservata/servizio/${payment.servizio_id}`,
status: resultStatus,
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<AcquistoProcessingProps>;
/**
* Pagina di stato del pagamento dopo un acquisto: /servizio/pagamento-status?payment_intent=paymentIntentId
*/
export default function AcquistoProcessing({
servizioId,
redirectUrl,
status,
}: AcquistoProcessingProps) {
const { t } = useTranslation();
@ -117,7 +110,7 @@ export default function AcquistoProcessing({
title={t.acquisto_elaborazione.success_title}
/>
<ProgressRedirect href={`/area-riservata/servizio/${servizioId}`} />
<ProgressRedirect href={redirectUrl} />
</>
)}
{status === AcquistoStatusEnum.processing && (

View file

@ -7,6 +7,7 @@ import { LoadingPage } from "~/components/loading";
import { FileSection } from "~/components/servizio/conferma";
import { Status500 } from "~/components/status-page";
import { Button } from "~/components/ui/button";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
import type { AnnunciId } from "~/schemas/public/Annunci";
@ -126,10 +127,5 @@ export const getServerSideProps = (async (context) => {
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<RiapriConfermaProps>;

View file

@ -3,6 +3,7 @@ import { ReceiptGenerator } from "~/components/acquisto_receipt";
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
import type { OrdiniOrdineId } from "~/schemas/public/Ordini";
import { generateSSGHelper } from "~/server/utils/ssgHelper";
@ -52,23 +53,13 @@ export const getServerSideProps = (async (context) => {
const raw = context.query?.raw;
if (typeof id !== "string") {
console.error("Error: ordineId is not a string");
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}
const parsed = zOrdineId.safeParse(id);
if (!parsed.success) {
console.error("Error parsing ordineId", parsed.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500
}
const helper = generateSSGHelper();
@ -85,10 +76,5 @@ export const getServerSideProps = (async (context) => {
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
return redirectTo500;
}) satisfies GetServerSideProps<PagamentoRecapProps>;