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";
|
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-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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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>
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
<FormNewServizioAcquisto initialData={data} userId={data.userData.id} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
OnboardServizio.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 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>;
|