2025-08-24 15:47:08 +02:00
|
|
|
import { Package } from "lucide-react";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import { LoadingPage } from "~/components/loading";
|
|
|
|
|
import { ServizioContent } from "~/components/servizio/main";
|
2025-08-24 15:47:08 +02:00
|
|
|
import {
|
2025-08-28 18:27:07 +02:00
|
|
|
Collapsible,
|
|
|
|
|
CollapsibleContent,
|
|
|
|
|
CollapsibleTrigger,
|
2025-08-24 15:47:08 +02:00
|
|
|
} from "~/components/ui/collapsible";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { ServizioProvider } from "~/providers/ServizioProvider";
|
|
|
|
|
import type { UsersId } from "~/schemas/public/Users";
|
2025-08-24 15:47:08 +02:00
|
|
|
import type { ServizioData } from "~/server/controllers/servizio.controller";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { api } from "~/utils/api";
|
2025-08-24 15:47:08 +02:00
|
|
|
|
|
|
|
|
export const TabRicerca = ({
|
2025-08-28 18:27:07 +02:00
|
|
|
userId,
|
|
|
|
|
isAdmin,
|
2025-08-24 15:47:08 +02:00
|
|
|
}: {
|
2025-08-28 18:27:07 +02:00
|
|
|
userId: UsersId;
|
|
|
|
|
isAdmin: boolean;
|
2025-08-24 15:47:08 +02:00
|
|
|
}) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
const { data, isLoading } = api.servizio.getAllServizioAnnunci.useQuery({
|
|
|
|
|
userId,
|
|
|
|
|
});
|
2025-08-24 15:47:08 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
if (isLoading) return <LoadingPage />;
|
|
|
|
|
if (!data || data.length === 0) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="m-4 rounded-md bg-yellow-100 p-4 text-yellow-900">
|
|
|
|
|
Nessun servizio attualmente inserito
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-08-24 15:47:08 +02:00
|
|
|
|
2025-10-14 16:53:15 +02:00
|
|
|
const first_choice = data.findIndex((s) => !s.isInterrotto && s.isOkAcconto);
|
|
|
|
|
const second_choice = data.findIndex((s) => !s.isInterrotto);
|
|
|
|
|
const getDefaultOpen = (idx: number) => {
|
|
|
|
|
if (first_choice !== -1) return idx === first_choice;
|
|
|
|
|
if (second_choice !== -1) return idx === second_choice;
|
|
|
|
|
return idx === 0;
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col gap-2">
|
|
|
|
|
{data.map((servizio, idx) => {
|
|
|
|
|
return (
|
|
|
|
|
<Row
|
2025-10-14 16:53:15 +02:00
|
|
|
defaultOpen={getDefaultOpen(idx)}
|
2025-08-29 16:18:32 +02:00
|
|
|
isAdmin={isAdmin}
|
2025-10-28 11:55:01 +01:00
|
|
|
key={servizio.servizio_id}
|
2025-08-28 18:27:07 +02:00
|
|
|
servizio={servizio}
|
|
|
|
|
userId={userId}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-08-24 15:47:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const Row = ({
|
2025-08-28 18:27:07 +02:00
|
|
|
servizio,
|
|
|
|
|
userId,
|
|
|
|
|
isAdmin,
|
|
|
|
|
defaultOpen = false,
|
2025-08-24 15:47:08 +02:00
|
|
|
}: {
|
2025-08-28 18:27:07 +02:00
|
|
|
servizio: ServizioData;
|
|
|
|
|
userId: UsersId;
|
|
|
|
|
isAdmin: boolean;
|
|
|
|
|
defaultOpen: boolean;
|
2025-08-24 15:47:08 +02:00
|
|
|
}) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
const [isOpen, setIsOpen] = useState(defaultOpen);
|
|
|
|
|
return (
|
|
|
|
|
<ServizioProvider
|
|
|
|
|
isAdmin={isAdmin}
|
|
|
|
|
servizio={servizio}
|
2025-08-29 16:18:32 +02:00
|
|
|
servizioId={servizio.servizio_id}
|
|
|
|
|
userId={userId}
|
2025-08-28 18:27:07 +02:00
|
|
|
>
|
2025-08-29 16:18:32 +02:00
|
|
|
<Collapsible onOpenChange={setIsOpen} open={isOpen}>
|
2025-10-10 16:18:43 +02:00
|
|
|
<div className="flex items-center rounded-md border border-border py-2 shadow-sm hover:shadow-md">
|
2025-08-28 18:27:07 +02:00
|
|
|
<CollapsibleTrigger className="grid w-full cursor-pointer grid-cols-10 items-center">
|
|
|
|
|
<div className="col-span-2 sm:col-span-1">
|
2025-10-14 16:53:15 +02:00
|
|
|
<span className="text-xl transition-all duration-300 ease-in-out">
|
|
|
|
|
{isOpen ? "👇" : "👉"}
|
|
|
|
|
</span>
|
2025-08-28 18:27:07 +02:00
|
|
|
</div>
|
|
|
|
|
<div className="col-span-6 flex items-center gap-2 font-semibold sm:text-lg">
|
2025-10-10 16:18:43 +02:00
|
|
|
<Package className="size-6 text-primary" />
|
2025-08-28 18:27:07 +02:00
|
|
|
Affitto {servizio.tipologia}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="col-span-2">
|
2025-09-01 16:48:03 +02:00
|
|
|
{(() => {
|
|
|
|
|
if (servizio.isInterrotto)
|
|
|
|
|
return <span className="text-red-500">Interrotto</span>;
|
|
|
|
|
if (servizio.isOkAcconto && servizio.decorrenza !== null) {
|
|
|
|
|
return <span className="text-green-500">Attivo</span>;
|
|
|
|
|
}
|
|
|
|
|
return <span className="text-neutral-500">Inattivo</span>;
|
|
|
|
|
})()}
|
2025-08-28 18:27:07 +02:00
|
|
|
</div>
|
|
|
|
|
<div className="hidden text-sm sm:col-span-2 sm:contents sm:text-base">
|
|
|
|
|
{servizio.created_at.toLocaleDateString("it", {
|
|
|
|
|
day: "2-digit",
|
2025-08-29 16:18:32 +02:00
|
|
|
month: "short",
|
2025-08-28 18:27:07 +02:00
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
</CollapsibleTrigger>
|
|
|
|
|
</div>
|
2025-08-24 15:47:08 +02:00
|
|
|
|
2025-10-10 16:18:43 +02:00
|
|
|
<CollapsibleContent className="mt-2 overflow-hidden transition-all duration-300 ease-in-out data-[state=closed]:animate-collapsible-up data-[state=open]:animate-collapsible-down">
|
2025-10-29 09:39:06 +01:00
|
|
|
<ServizioContent />
|
2025-08-28 18:27:07 +02:00
|
|
|
</CollapsibleContent>
|
|
|
|
|
</Collapsible>
|
|
|
|
|
</ServizioProvider>
|
|
|
|
|
);
|
2025-08-24 15:47:08 +02:00
|
|
|
};
|