infoalloggi-monorepo/apps/infoalloggi/src/components/servizio/main.tsx

316 lines
8.2 KiB
TypeScript
Raw Normal View History

import { ArrowRight, Package, PackageCheck } from "lucide-react";
2025-08-04 17:45:44 +02:00
import Link from "next/link";
import { useRouter } from "next/router";
2025-10-21 16:57:59 +02:00
import { useEffect } from "react";
2025-08-04 17:45:44 +02:00
import toast from "react-hot-toast";
2025-10-21 16:57:59 +02:00
import { AnnuncioCard } from "~/components/servizio/annuncio_card";
2025-08-28 18:27:07 +02:00
import { SaldoButton } from "~/components/servizio/interactions";
import { ServizioActions } from "~/components/servizio/servizio_actions";
2025-08-28 18:27:07 +02:00
import { Button } from "~/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "~/components/ui/card";
import { cn } from "~/lib/utils";
import {
ServizioAnnuncioProvider,
useServizio,
} from "~/providers/ServizioProvider";
import { api } from "~/utils/api";
import { AlarmClockSVG } from "../svgs";
import { AnnunciCompatibili } from "./compatibili_dialog";
2025-10-21 16:57:59 +02:00
import {
AnnunciSelezionatiAccordion,
AnnunciSelezionatiDialog,
2025-10-21 16:57:59 +02:00
} from "./servizio_annunci_accordions";
2025-08-04 17:45:44 +02:00
const ServizioCard = ({
2025-08-28 18:27:07 +02:00
cardClassName,
header,
headerClassName,
content,
contentClassName,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
cardClassName?: string;
header: React.ReactNode;
headerClassName?: string;
content: React.ReactNode;
contentClassName?: string;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
return (
<Card
className={cn(
"gap-2 rounded-none rounded-b-md border-primary border-t-0",
cardClassName,
)}
2025-08-28 18:27:07 +02:00
>
<CardHeader
className={cn(
"flex flex-col items-start justify-between gap-3 space-y-0 px-4 pb-8 sm:flex-row",
headerClassName,
)}
>
{header}
</CardHeader>
<CardContent className={cn("space-y-4 p-4 pt-0", contentClassName)}>
{content}
</CardContent>
</Card>
);
2025-08-04 17:45:44 +02:00
};
export const ServizioContent = () => {
2025-08-28 18:27:07 +02:00
const { servizio, isAdmin, userId } = useServizio();
const utils = api.useUtils();
const { mutate: updateServizio } = api.servizio.updateServizio.useMutation({
2025-08-29 16:18:32 +02:00
onError: (error) => {
toast.error(error.message);
},
2025-08-28 18:27:07 +02:00
onSuccess: async () => {
toast.success("Servizio modificato con successo");
await utils.servizio.getAllServizioAnnunci.invalidate({ userId });
},
});
const router = useRouter();
useEffect(() => {
const id = router.asPath.split("#")[1];
if (id) {
document.getElementById(id)?.scrollIntoView({
behavior: "smooth",
});
}
}, []);
if (servizio.isInterrotto) {
return (
<ServizioCard
//cardClassName="bg-neutral-100 border-neutral-300"
2025-08-29 16:18:32 +02:00
content={
<>
2025-10-10 16:18:43 +02:00
<p className="font-medium text-lg">Servizio interrotto</p>
2025-08-29 16:18:32 +02:00
<p>
Non è possibile riutilizzare questo servizio, poichè è stato
interrotto.
</p>
</>
}
2025-08-28 18:27:07 +02:00
header={
<>
<div className="flex flex-col gap-4">
<CardTitle className="flex items-center gap-2 text-xl sm:text-2xl">
2025-10-10 16:18:43 +02:00
<Package className="size-6 text-primary" />
2025-08-28 18:27:07 +02:00
Cerco Affitto {servizio.tipologia}
</CardTitle>
2025-10-10 16:18:43 +02:00
<CardDescription className="space-y-2 pl-2 text-foreground/70">
2025-08-28 18:27:07 +02:00
<p>
aggiunto il{" "}
{servizio.created_at.toLocaleDateString("it", {
day: "2-digit",
2025-08-29 16:18:32 +02:00
month: "2-digit",
year: "numeric",
2025-08-28 18:27:07 +02:00
})}
</p>
</CardDescription>
</div>
</>
}
/>
);
}
if (!servizio.isOkAcconto || servizio.decorrenza === null) {
return (
<ServizioCard
content={
<div className="flex w-full flex-col items-center justify-center gap-4">
<div className="flex w-full max-w-md flex-col items-center justify-center gap-4 text-center">
<AlarmClockSVG className="size-46" />
<h1 className="text-xl">Servizio non ancora attivato</h1>
2025-08-28 18:27:07 +02:00
<Link
aria-label="Attiva Servizio"
className="w-full"
2025-08-29 16:18:32 +02:00
href={`/servizio/onboard/${servizio.servizio_id}`}
2025-08-28 18:27:07 +02:00
>
<Button className="w-full text-lg" variant="success">
<span>Attiva il Servizio Ora!</span>
<ArrowRight />
</Button>
2025-08-28 18:27:07 +02:00
</Link>
2025-08-28 18:27:07 +02:00
{isAdmin && (
<Button
className="w-full"
2025-08-28 18:27:07 +02:00
onClick={() =>
updateServizio({
data: {
decorrenza: new Date(),
2025-08-29 16:18:32 +02:00
isOkAcconto: true,
2025-08-28 18:27:07 +02:00
},
2025-08-29 16:18:32 +02:00
servizioId: servizio.servizio_id,
2025-08-28 18:27:07 +02:00
})
}
variant="success"
>
Attiva Manualmente (Azione Admin)
</Button>
)}
</div>
{isAdmin && <AnnunciSelezionatiDialog annunci={servizio.annunci} />}
2025-08-28 18:27:07 +02:00
</div>
}
contentClassName={cn(!isAdmin && "gap-0")}
2025-08-29 16:18:32 +02:00
header={
<div className="flex w-full flex-col gap-2">
<ServizioActions />
2025-08-29 16:18:32 +02:00
</div>
}
headerClassName={cn(!isAdmin && "pb-0")}
2025-08-28 18:27:07 +02:00
/>
);
}
const annuncioConfermato = servizio.annunci.filter(
(a) => a.accettato_conferma_at !== null,
);
if (servizio.isOkSaldo && annuncioConfermato.length > 0) {
2025-08-28 18:27:07 +02:00
return (
<ServizioCard
2025-08-29 16:18:32 +02:00
content={
<>
{annuncioConfermato.map((data) => {
return (
<ServizioAnnuncioProvider
data={data}
key={data.id}
servizioId={servizio.servizio_id}
userId={userId}
>
2025-08-29 16:18:32 +02:00
<AnnuncioCard />
</ServizioAnnuncioProvider>
);
})}
</>
}
2025-08-28 18:27:07 +02:00
header={
<>
<ServizioActions />
2025-08-28 18:27:07 +02:00
</>
}
/>
);
}
const annunciSelezionati = servizio.annunci.filter(
(a) => a.user_confirmed_at === null,
);
2025-08-28 18:27:07 +02:00
const annunciInConferma = servizio.annunci.filter(
(a) => a.user_confirmed_at !== null,
);
return (
<ServizioCard
content={
<>
{/* <div className="flex w-full flex-col gap-8 sm:flex-row sm:items-start">
<div className="flex flex-2 flex-col gap-2 sm:max-w-1/2">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<UserCircle className="size-6 text-primary" />
<span className="font-medium">Contatti sbloccati</span>
</div>
<span className="text-sm">
{openContattiAnnunci.length} / 10
</span>
2025-08-28 18:27:07 +02:00
</div>
<Progress
className="h-2"
value={openContattiAnnunci.length * 10}
/>
2025-08-28 18:27:07 +02:00
</div>
<div className="flex flex-col flex-wrap gap-2 sm:flex-row sm:items-center">
<div className="flex items-center gap-2">
<CalendarClock className="size-6 text-primary" />
<span className="font-medium">Durata servizio:</span>
</div>
<span>
{`${new Date(servizio.decorrenza).toLocaleDateString("it-IT")} - ${new Date(add(servizio.decorrenza, { days: 60 })).toLocaleDateString("it-IT")}`}
</span>
</div>
</div> */}
2025-08-28 18:27:07 +02:00
{annuncioConfermato.length > 0 && (
<div className="flex flex-col gap-2">
<div className="flex items-center gap-2">
2025-10-10 16:18:43 +02:00
<PackageCheck className="size-6 text-primary" />
2025-08-28 18:27:07 +02:00
<span className="font-medium">Saldi in attesa:</span>
</div>
{servizio.annunci
.filter((a) => a.accettato_conferma_at !== null)
.map((a) => (
<div
className="flex items-center gap-2 rounded-md bg-sky-400 p-2"
2025-08-29 16:18:32 +02:00
key={a.annunci_id}
2025-08-28 18:27:07 +02:00
>
<h3>
{a.codice} - confermato il:{" "}
{a.accettato_conferma_at &&
new Date(a.accettato_conferma_at).toLocaleDateString(
"it",
{
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
month: "2-digit",
year: "numeric",
},
)}
2025-08-28 18:27:07 +02:00
</h3>
<SaldoButton
annuncioId={a.annunci_id}
2025-08-29 16:18:32 +02:00
className="border-none"
variant={"outline"}
2025-08-28 18:27:07 +02:00
/>
</div>
))}
</div>
)}
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-2 overflow-y-auto">
{annunciInConferma.map((data) => {
return (
<ServizioAnnuncioProvider
data={data}
key={data.id}
servizioId={servizio.servizio_id}
userId={userId}
>
<AnnuncioCard className="border-2 border-green-500" />
</ServizioAnnuncioProvider>
);
})}
</div>
{annunciSelezionati.length > 0 && (
<AnnunciSelezionatiAccordion annunci={annunciSelezionati} />
)}
<div className="flex items-center justify-center">
<AnnunciCompatibili />
</div>
2025-08-28 18:27:07 +02:00
</div>
</>
}
2025-08-29 16:18:32 +02:00
header={
<>
<ServizioActions />
2025-08-29 16:18:32 +02:00
</>
}
headerClassName="pb-0"
2025-08-28 18:27:07 +02:00
/>
);
};