Picker lists events with those running today first. The listing filters by salesperson, temperature, status, interest and lead type, with a table at md and up and tappable cards on phones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
114 lines
3.8 KiB
TypeScript
114 lines
3.8 KiB
TypeScript
"use client";
|
||
|
||
import { useEffect, useState } from "react";
|
||
import Link from "next/link";
|
||
import { Calendar, MapPin } from "lucide-react";
|
||
import { EmptyState, ErrorState } from "@/app/components/ui/states";
|
||
import { RequireModule } from "@/app/lib/RequireModule";
|
||
import { PageHeader } from "@/app/components/ui/page-header";
|
||
import { listEventLeadEvents } from "@/app/lib/services/adminEventLeads";
|
||
import type { EventLeadEvent } from "@/app/lib/types";
|
||
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
||
import { formatDmy } from "@/app/lib/date-input";
|
||
|
||
const isoToday = () => {
|
||
const d = new Date();
|
||
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
||
};
|
||
|
||
function EventLeadsPageContent() {
|
||
const t = useT();
|
||
const [events, setEvents] = useState<EventLeadEvent[]>([]);
|
||
const [loading, setLoading] = useState(true);
|
||
const [error, setError] = useState<string | null>(null);
|
||
const [reloadKey, setReloadKey] = useState(0);
|
||
|
||
useEffect(() => {
|
||
let cancelled = false;
|
||
// eslint-disable-next-line react-hooks/set-state-in-effect -- resets loading/error before the triggered fetch below, which is the actual synchronization.
|
||
setLoading(true);
|
||
setError(null);
|
||
listEventLeadEvents()
|
||
.then((data) => {
|
||
if (!cancelled) setEvents(data);
|
||
})
|
||
.catch((err: unknown) => {
|
||
if (!cancelled) setError(err instanceof Error ? err.message : "LeadLoadFailed");
|
||
})
|
||
.finally(() => {
|
||
if (!cancelled) setLoading(false);
|
||
});
|
||
return () => { cancelled = true; };
|
||
}, [reloadKey]);
|
||
|
||
const today = isoToday();
|
||
const sorted = events
|
||
.slice()
|
||
.sort((a, b) => {
|
||
// ISO dates compare as strings; an event runs today when start <= today <= end.
|
||
const running = (e: EventLeadEvent) => (e.startDate ?? "9") <= today && today <= (e.endDate ?? e.startDate ?? "");
|
||
return Number(running(b)) - Number(running(a));
|
||
});
|
||
|
||
if (error) {
|
||
return (
|
||
<>
|
||
<PageHeader title={t("BO_EVENT_LEADS")} />
|
||
<ErrorState message={t(error)} onRetry={() => setReloadKey((k) => k + 1)} className="mb-4" />
|
||
</>
|
||
);
|
||
}
|
||
|
||
if (loading) {
|
||
return (
|
||
<>
|
||
<PageHeader title={t("BO_EVENT_LEADS")} />
|
||
<div className="space-y-3">
|
||
{Array.from({ length: 3 }).map((_, i) => (
|
||
<div key={i} className="h-20 animate-pulse rounded-lg bg-muted" />
|
||
))}
|
||
</div>
|
||
</>
|
||
);
|
||
}
|
||
|
||
return (
|
||
<>
|
||
<PageHeader title={t("BO_EVENT_LEADS")} />
|
||
{sorted.length === 0 ? (
|
||
<EmptyState title={t("LeadNoEvents")} />
|
||
) : (
|
||
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
|
||
{sorted.map((evt) => (
|
||
<Link
|
||
key={evt.uuid}
|
||
href={`/event-leads/${evt.uuid}`}
|
||
className="flex flex-col gap-2 rounded-lg border p-4 transition-colors hover:bg-accent min-h-[8.5rem]"
|
||
>
|
||
<h2 className="font-semibold text-lg">{evt.name}</h2>
|
||
{evt.location && (
|
||
<div className="flex items-center gap-1 text-sm text-muted-foreground">
|
||
<MapPin className="size-3.5 shrink-0" aria-hidden="true" />
|
||
<span>{evt.location}</span>
|
||
</div>
|
||
)}
|
||
<div className="flex items-center gap-1 text-sm text-muted-foreground">
|
||
<Calendar className="size-3.5 shrink-0" aria-hidden="true" />
|
||
<span>{evt.startDate ? formatDmy(evt.startDate) : ""} – {evt.endDate ? formatDmy(evt.endDate) : ""}</span>
|
||
</div>
|
||
</Link>
|
||
))}
|
||
</div>
|
||
)}
|
||
</>
|
||
);
|
||
}
|
||
|
||
export default function EventLeadsPage() {
|
||
return (
|
||
<RequireModule pathname="/event-leads">
|
||
<EventLeadsPageContent />
|
||
</RequireModule>
|
||
);
|
||
}
|