infoalloggi-monorepo/apps/infoalloggi/src/pages/servizio/contratto-registrazione/[...slug].tsx

115 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import type { GetServerSideProps } from "next";
2025-10-24 16:10:59 +02:00
import Link from "next/link";
2025-08-28 18:27:07 +02:00
import { AllegatoIframe } from "~/components/allegato-iframe";
2025-08-04 17:45:44 +02:00
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { getStorageUrl } from "~/lib/storage_utils";
import { redirectTo500 } from "~/lib/utils";
2025-08-28 18:27:07 +02:00
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
2025-08-28 18:27:07 +02:00
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types";
import { api } from "~/utils/api";
2025-08-04 17:45:44 +02:00
type ContrattoViewerProps = {
storageId: string;
2025-08-04 17:45:44 +02:00
};
/**
* Pagina di visualizzazione del contratto di registrazione: /servizio/contratto-registrazione/[servizioId]/[annuncioId]
*/
2025-08-04 17:45:44 +02:00
const ContrattoViewer: NextPageWithLayout<ContrattoViewerProps> = ({
storageId,
2025-08-04 17:45:44 +02:00
}: ContrattoViewerProps) => {
const { data: fileInfos, isLoading } = api.storage.getFileInfos.useQuery({
storageId,
2025-08-28 18:27:07 +02:00
});
const { t } = useTranslation();
if (isLoading) return <LoadingPage />;
if (!fileInfos) return <p>{t.file_section.error}</p>;
2025-08-28 18:27:07 +02:00
return (
<div className="mx-2 flex flex-1 overflow-auto">
<div className="mx-auto flex grow sm:grow-0">
<div className="flex flex-col items-center space-y-4 py-4">
2025-10-10 16:18:43 +02:00
<h1 className="font-bold text-xl">
2025-08-28 18:27:07 +02:00
{t.servizio.contratto_view.ricevuta}
</h1>
<p className="text-center">
{t.servizio.contratto_view.ricevuta_cta}
</p>
<AllegatoIframe
allegato={fileInfos}
className="h-full max-h-[70vh]"
/>
<Link
className="underline underline-offset-1 after:content-['_↗']"
href={getStorageUrl({
storageId: fileInfos.id,
params: { mode: "download" },
})}
>
{t.file_section.download}
</Link>
2025-08-28 18:27:07 +02:00
</div>
</div>
</div>
);
2025-08-04 17:45:44 +02:00
};
ContrattoViewer.getLayout = function getLayout(page) {
return <AreaRiservataLayout noSidebar>{page}</AreaRiservataLayout>;
2025-08-04 17:45:44 +02:00
};
export default ContrattoViewer;
export const getServerSideProps = (async (context) => {
2025-08-28 18:27:07 +02:00
const slug = context.params?.slug;
if (!slug || slug.length !== 2) {
return {
notFound: true,
};
}
const [servizioId, annuncioId] = slug;
if (!servizioId || !annuncioId) {
return {
notFound: true,
};
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const parsedServizioId = zServizioId.safeParse(servizioId);
const parsedAnnuncioId = zAnnuncioId.safeParse(parseInt(annuncioId));
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (!parsedServizioId.success || !parsedAnnuncioId.success) {
return {
notFound: true,
};
}
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) {
const storageId = await helper.trpc.servizio.getServizioRegistrazione.fetch(
{
annuncioId: parsedAnnuncioId.data,
servizioId: parsedServizioId.data,
},
);
if (!storageId) {
return redirectTo500;
}
await helper.trpc.storage.getFileInfos.prefetch({
storageId,
2025-08-28 18:27:07 +02:00
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return {
props: {
storageId,
2025-08-28 18:27:07 +02:00
trpcState: helper.trpc.dehydrate(),
},
};
}
return redirectTo500;
2025-08-04 17:45:44 +02:00
}) satisfies GetServerSideProps<ContrattoViewerProps>;