2025-08-04 17:45:44 +02:00
|
|
|
import type { GetServerSideProps } from "next";
|
|
|
|
|
import { AreaRiservataLayout } from "~/components/Layout";
|
|
|
|
|
import { LoadingPage } from "~/components/loading";
|
2025-10-30 18:18:01 +01:00
|
|
|
import { OnboardTutorial } from "~/components/onboard_tutorial";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { Status500 } from "~/components/status-page";
|
2026-03-12 16:26:01 +01:00
|
|
|
import { FormServizioAcquisto } from "~/forms/FormServizioAcquisto";
|
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";
|
2025-08-13 17:29:26 +02:00
|
|
|
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
|
|
|
};
|
2026-03-08 01:02:57 +01:00
|
|
|
/**
|
|
|
|
|
* /servizio/onboard/[servizioId] \
|
|
|
|
|
* Pagina di onboarding per un nuovo servizio. Mostra un tutorial e un form per completare l'acquisto del servizio, con i dati precompilati se disponibili.
|
|
|
|
|
* Accessibile solo se l'onboarding è disponibile per l'utente e il servizio specificato.
|
|
|
|
|
* Se l'onboarding non è disponibile, reindirizza alla dashboard dell'area riservata.
|
|
|
|
|
* Se non è possibile recuperare i dati necessari, mostra una pagina di errore.
|
|
|
|
|
*/
|
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-12-29 16:09:51 +01:00
|
|
|
<h3 className="font-bold text-3xl">
|
2025-08-28 18:27:07 +02:00
|
|
|
{locale === "it" ? "Dettagli del servizio" : "Service Details"}
|
|
|
|
|
</h3>
|
|
|
|
|
</div>
|
2025-10-30 18:18:01 +01:00
|
|
|
<OnboardTutorial />
|
2025-11-13 15:45:55 +01:00
|
|
|
<CatastoProvider>
|
2026-03-12 16:26:01 +01:00
|
|
|
<FormServizioAcquisto
|
2026-03-08 01:02:57 +01:00
|
|
|
initialData={data}
|
|
|
|
|
isAdmin={false}
|
|
|
|
|
userId={data.userData.id}
|
|
|
|
|
/>
|
2025-11-13 15:45:55 +01:00
|
|
|
</CatastoProvider>
|
2025-08-28 18:27:07 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
OnboardServizio.getLayout = function getLayout(page) {
|
2025-12-04 11:02:28 +01:00
|
|
|
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,
|
|
|
|
|
});
|
2026-03-06 18:23:35 +01:00
|
|
|
const { available, userId: _ } =
|
|
|
|
|
await helper.trpc.servizio.isOnboardAvailable.fetch({
|
|
|
|
|
servizioId: parsed.data,
|
|
|
|
|
});
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2026-03-06 18:23:35 +01:00
|
|
|
if (!available) {
|
2025-08-28 18:27:07 +02:00
|
|
|
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>;
|