infoalloggi-monorepo/apps/infoalloggi/src/pages/servizio/condizioni/[stringaId].tsx

96 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import type { GetServerSideProps } from "next";
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
2025-08-28 18:27:07 +02:00
import { Status500 } from "~/components/status-page";
import type { NextPageWithLayout } from "~/pages/_app";
2025-08-04 17:45:44 +02:00
import type { TestiEStringheStingaId } from "~/schemas/public/TestiEStringhe";
2025-08-28 18:27:07 +02:00
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zTestiEStringheStingaId } from "~/server/utils/zod_types";
import { api } from "~/utils/api";
2025-08-04 17:45:44 +02:00
type CondizioniProps = {
2025-08-28 18:27:07 +02:00
stringaId: TestiEStringheStingaId;
2025-08-04 17:45:44 +02:00
};
const CondizioniPage: NextPageWithLayout<CondizioniProps> = ({
2025-08-28 18:27:07 +02:00
stringaId,
2025-08-04 17:45:44 +02:00
}: CondizioniProps) => {
const { data, isLoading } = api.strings.getStringa.useQuery({
2025-08-28 18:27:07 +02:00
stringaId,
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (isLoading) {
return <LoadingPage />;
}
if (!data) {
return <Status500 />;
}
return (
<div className="w-full grow space-y-4 p-4 sm:p-6">
{data && (
<div
2025-10-10 16:18:43 +02:00
className="prose mx-auto max-w-5xl"
2025-08-28 18:27:07 +02:00
dangerouslySetInnerHTML={{ __html: data.stringa_value || "" }}
/>
)}
</div>
);
2025-08-04 17:45:44 +02:00
};
CondizioniPage.getLayout = function getLayout(page) {
2025-08-28 18:27:07 +02:00
return (
2025-08-29 16:18:32 +02:00
<AreaRiservataLayout noFooter noSidebar>
2025-08-28 18:27:07 +02:00
{page}
</AreaRiservataLayout>
);
2025-08-04 17:45:44 +02:00
};
export default CondizioniPage;
export const getServerSideProps = (async (context) => {
2025-08-28 18:27:07 +02:00
const id = context.params?.stringaId;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (typeof id !== "string") {
console.error("Error: stringaId is not a string");
return {
redirect: {
destination: "/500",
permanent: false,
},
};
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const parsed = zTestiEStringheStingaId.safeParse(id);
if (!parsed.success) {
console.error("Error parsing paymentId", parsed.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const access_token = context.req.cookies.access_token;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const helper = await TrpcAuthedFetchingIstance({ access_token });
if (helper) {
await helper.trpc.strings.getStringa.prefetch({
2025-08-28 18:27:07 +02:00
stringaId: parsed.data,
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return {
props: {
stringaId: parsed.data,
2025-08-29 16:18:32 +02:00
trpcState: helper.trpc.dehydrate(),
2025-08-28 18:27:07 +02:00
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
2025-08-04 17:45:44 +02:00
}) satisfies GetServerSideProps<CondizioniProps>;