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

124 lines
3.4 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";
import { DocNotFoundPage } from "~/components/doc_not_found";
2025-08-04 17:45:44 +02:00
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
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 type { AnnunciId } from "~/schemas/public/Annunci";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
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 = {
2025-08-28 18:27:07 +02:00
servizioId: ServizioServizioId;
annuncioId: AnnunciId;
2025-08-04 17:45:44 +02:00
};
const ContrattoViewer: NextPageWithLayout<ContrattoViewerProps> = ({
2025-08-28 18:27:07 +02:00
servizioId,
annuncioId,
2025-08-04 17:45:44 +02:00
}: ContrattoViewerProps) => {
2025-08-28 18:27:07 +02:00
const { data, isLoading } = api.servizio.getServizioAnnuncio.useQuery({
annuncioId,
2025-08-29 16:18:32 +02:00
servizioId,
2025-08-28 18:27:07 +02:00
});
const { t } = useTranslation();
if (isLoading) return <LoadingPage />;
if (!data) return <Status500 />;
if (!data.doc_contratto_ref) return <DocNotFoundPage />;
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">{t.servizio.contratto.titolo}</h1>
2025-08-28 18:27:07 +02:00
<p className="text-center">{t.servizio.contratto.descrizione}</p>
<FileSection storageId={data.doc_contratto_ref} />
</div>
</div>
</div>
);
2025-08-04 17:45:44 +02:00
};
2025-10-24 16:10:59 +02:00
const FileSection = ({ storageId }: { storageId: string }) => {
const { data: fileInfos, isLoading } = api.storage.getFileInfos.useQuery({
2025-08-28 18:27:07 +02:00
storageId,
});
const { t } = useTranslation();
if (isLoading) return <LoadingPage />;
if (!fileInfos) return <p>{t.file_section.error}</p>;
return (
<>
2025-10-24 16:10:59 +02:00
<AllegatoIframe allegato={fileInfos} className="h-full max-h-[70vh]" />
2025-08-04 17:45:44 +02:00
2025-10-24 16:10:59 +02:00
<Link
2025-08-29 16:18:32 +02:00
className="underline underline-offset-1 after:content-['_↗']"
2025-10-24 16:10:59 +02:00
href={`/storage-api/get/${fileInfos.id}?mode=download`}
2025-08-28 18:27:07 +02:00
>
{t.file_section.download}
2025-10-24 16:10:59 +02:00
</Link>
2025-08-28 18:27:07 +02:00
</>
);
2025-08-04 17:45:44 +02:00
};
ContrattoViewer.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 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) {
await helper.trpc.servizio.getServizioAnnuncio.prefetch({
annuncioId: parsedAnnuncioId.data,
2025-08-29 16:18:32 +02:00
servizioId: parsedServizioId.data,
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: {
annuncioId: parsedAnnuncioId.data,
2025-08-29 16:18:32 +02:00
servizioId: parsedServizioId.data,
2025-08-28 18:27:07 +02:00
trpcState: helper.trpc.dehydrate(),
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
2025-08-04 17:45:44 +02:00
}) satisfies GetServerSideProps<ContrattoViewerProps>;