infoalloggi-monorepo/apps/infoalloggi/src/pages/servizio/onboard/[servizioId].tsx

116 lines
3 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";
import { OnboardTutorial } from "~/components/onboard_tutorial";
2025-08-28 18:27:07 +02:00
import { Status500 } from "~/components/status-page";
2025-08-04 17:45:44 +02:00
import { FormNewServizioAcquisto } from "~/forms/FormNewServizioAcquisto";
2025-08-28 18:27:07 +02:00
import type { NextPageWithLayout } from "~/pages/_app";
2025-11-13 15:45:55 +01:00
import { CatastoProvider } from "~/providers/CatastoProvider";
import { useTranslation } from "~/providers/I18nProvider";
2025-08-28 18:27:07 +02:00
import type { ServizioServizioId } from "~/schemas/public/Servizio";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zServizioId } from "~/server/utils/zod_types";
import { api } from "~/utils/api";
2025-08-04 17:45:44 +02:00
type OnboardServizioProps = {
2025-08-28 18:27:07 +02:00
servizioId: ServizioServizioId;
2025-08-04 17:45:44 +02:00
};
const OnboardServizio: NextPageWithLayout<OnboardServizioProps> = ({
2025-08-28 18:27:07 +02:00
servizioId,
2025-08-04 17:45:44 +02:00
}: OnboardServizioProps) => {
2025-08-28 18:27:07 +02:00
const { data, isLoading } = api.servizio.getAcquistoData.useQuery({
servizioId: servizioId,
});
const { locale } = useTranslation();
if (isLoading) {
return <LoadingPage />;
}
if (!data) {
return <Status500 />;
}
return (
<div className="mx-auto w-full max-w-6xl grow space-y-6 p-2 sm:px-8 sm:py-6">
<div className="flex justify-center">
2025-10-10 16:18:43 +02:00
<h3 className="font-bold text-3xl text-primary">
2025-08-28 18:27:07 +02:00
{locale === "it" ? "Dettagli del servizio" : "Service Details"}
</h3>
</div>
<OnboardTutorial />
2025-11-13 15:45:55 +01:00
<CatastoProvider>
<FormNewServizioAcquisto initialData={data} userId={data.userData.id} />
</CatastoProvider>
2025-08-28 18:27:07 +02:00
</div>
);
2025-08-04 17:45:44 +02:00
};
OnboardServizio.getLayout = function getLayout(page) {
return <AreaRiservataLayout noSidebar>{page}</AreaRiservataLayout>;
2025-08-04 17:45:44 +02:00
};
export default OnboardServizio;
export const getServerSideProps = (async (context) => {
2025-08-28 18:27:07 +02:00
const id = context.params?.servizioId;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (typeof id !== "string") {
console.error("Error: servizioId 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 = zServizioId.safeParse(id);
if (!parsed.success) {
console.error("Error parsing servizioId", 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.servizio.getAcquistoData.prefetch({
servizioId: parsed.data,
});
const servizio = await helper.trpc.servizio.getServizio.fetch({
servizioId: parsed.data,
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (
!servizio ||
servizio.isOkAcconto ||
servizio.isInterrotto ||
servizio.decorrenza !== null
) {
return {
redirect: {
destination: "/area-riservata/dashboard",
permanent: false,
},
};
}
return {
props: {
servizioId: parsed.data,
2025-08-29 16:18:32 +02:00
trpcState: helper.trpc.dehydrate(),
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 {
redirect: {
destination: `/servizio/pre-onboard/${parsed.data}`,
permanent: false,
},
};
2025-08-04 17:45:44 +02:00
}) satisfies GetServerSideProps<OnboardServizioProps>;