'use client'; import { useCallback } from 'react'; import Link from 'next/link'; import { useRouter, useSearchParams } from 'next/navigation'; import { Pencil, Plus } from 'lucide-react'; import { formatDate } from '@kit/shared/dates'; import { Button } from '@kit/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card'; import { useActionWithToast } from '@kit/ui/use-action-with-toast'; import { deleteCompetition } from '../server/actions/fischerei-actions'; import { DeleteConfirmButton } from './delete-confirm-button'; interface CompetitionsDataTableProps { data: Array>; total: number; page: number; pageSize: number; account: string; accountId: string; } export function CompetitionsDataTable({ data, total, page, pageSize, account, accountId, }: CompetitionsDataTableProps) { const router = useRouter(); const searchParams = useSearchParams(); const totalPages = Math.max(1, Math.ceil(total / pageSize)); const { execute: executeDelete, isPending: isDeleting } = useActionWithToast( deleteCompetition, { successMessage: 'Wettbewerb gelöscht', onSuccess: () => router.refresh(), }, ); const updateParams = useCallback( (updates: Record) => { const params = new URLSearchParams(searchParams.toString()); for (const [key, value] of Object.entries(updates)) { if (value) { params.set(key, value); } else { params.delete(key); } } if (!('page' in updates)) params.delete('page'); router.push(`?${params.toString()}`); }, [router, searchParams], ); const handlePageChange = useCallback( (newPage: number) => { updateParams({ page: String(newPage) }); }, [updateParams], ); return (
{/* Toolbar */}

Wettbewerbe ({total})

{/* Table */} {data.length === 0 ? (

Keine Wettbewerbe vorhanden

Erstellen Sie Ihren ersten Wettbewerb.

) : (
{data.map((comp) => { const waters = comp.waters as Record< string, unknown > | null; return ( router.push( `/home/${account}/fischerei/competitions/${String(comp.id)}`, ) } > ); })}
Name Datum Gewässer Max. Teilnehmer Aktionen
{String(comp.name)} {formatDate(comp.competition_date as string | null)} {waters ? String(waters.name) : '—'} {comp.max_participants != null ? String(comp.max_participants) : '—'}
executeDelete({ competitionId: String(comp.id), accountId, }) } />
)} {totalPages > 1 && (

Seite {page} von {totalPages} ({total} Einträge)

)}
); }