feat: add delete functionality for leases, catch books, and permits; implement newsletter update feature
Some checks failed
Workflow / ʦ TypeScript (push) Failing after 4m52s
Workflow / ⚫️ Test (push) Has been skipped

This commit is contained in:
T. Zehetbauer
2026-04-01 17:53:39 +02:00
parent c6b2824da8
commit 080ec1cb47
22 changed files with 798 additions and 210 deletions

View File

@@ -4,16 +4,19 @@ import { useCallback } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { Check } from 'lucide-react';
import { Pencil } from 'lucide-react';
import { Badge } from '@kit/ui/badge';
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 {
CATCH_BOOK_STATUS_LABELS,
CATCH_BOOK_STATUS_COLORS,
} from '../lib/fischerei-constants';
import { deleteCatchBook } from '../server/actions/fischerei-actions';
import { DeleteConfirmButton } from './delete-confirm-button';
interface CatchBooksDataTableProps {
data: Array<Record<string, unknown>>;
@@ -21,6 +24,7 @@ interface CatchBooksDataTableProps {
page: number;
pageSize: number;
account: string;
accountId: string;
}
const STATUS_OPTIONS = [
@@ -38,10 +42,19 @@ export function CatchBooksDataTable({
page,
pageSize,
account,
accountId,
}: CatchBooksDataTableProps) {
const router = useRouter();
const searchParams = useSearchParams();
const { execute: executeDelete, isPending: isDeleting } = useActionWithToast(
deleteCatchBook,
{
successMessage: 'Fangbuch gelöscht',
onSuccess: () => router.refresh(),
},
);
const currentYear = searchParams.get('year') ?? '';
const currentStatus = searchParams.get('status') ?? '';
const totalPages = Math.max(1, Math.ceil(total / pageSize));
@@ -146,6 +159,7 @@ export function CatchBooksDataTable({
<th className="p-3 text-right font-medium">Angeltage</th>
<th className="p-3 text-right font-medium">Fänge</th>
<th className="p-3 text-left font-medium">Status</th>
<th className="p-3 text-right font-medium">Aktionen</th>
</tr>
</thead>
<tbody>
@@ -190,6 +204,34 @@ export function CatchBooksDataTable({
{CATCH_BOOK_STATUS_LABELS[status] ?? status}
</Badge>
</td>
<td className="p-3 text-right">
<div className="flex items-center justify-end gap-1">
<Button
variant="ghost"
size="sm"
data-test="catchbook-edit-btn"
onClick={(e) => {
e.stopPropagation();
router.push(
`/home/${account}/fischerei/catch-books/${String(cb.id)}`,
);
}}
>
<Pencil className="h-4 w-4" />
</Button>
<DeleteConfirmButton
title="Fangbuch löschen"
description="Möchten Sie dieses Fangbuch wirklich löschen? Alle zugehörigen Fangeinträge werden ebenfalls gelöscht. Diese Aktion kann nicht rückgängig gemacht werden."
isPending={isDeleting}
onConfirm={() =>
executeDelete({
catchBookId: String(cb.id),
accountId,
})
}
/>
</div>
</td>
</tr>
);
})}

View File

@@ -5,11 +5,15 @@ import { useCallback } from 'react';
import Link from 'next/link';
import { useRouter, useSearchParams } from 'next/navigation';
import { Plus } from 'lucide-react';
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<Record<string, unknown>>;
@@ -17,6 +21,7 @@ interface CompetitionsDataTableProps {
page: number;
pageSize: number;
account: string;
accountId: string;
}
export function CompetitionsDataTable({
@@ -25,11 +30,20 @@ export function CompetitionsDataTable({
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<string, string>) => {
const params = new URLSearchParams(searchParams.toString());
@@ -95,6 +109,7 @@ export function CompetitionsDataTable({
<th className="p-3 text-right font-medium">
Max. Teilnehmer
</th>
<th className="p-3 text-right font-medium">Aktionen</th>
</tr>
</thead>
<tbody>
@@ -126,6 +141,34 @@ export function CompetitionsDataTable({
? String(comp.max_participants)
: '—'}
</td>
<td className="p-3 text-right">
<div className="flex items-center justify-end gap-1">
<Button
variant="ghost"
size="sm"
data-test="competition-edit-btn"
onClick={(e) => {
e.stopPropagation();
router.push(
`/home/${account}/fischerei/competitions/${String(comp.id)}`,
);
}}
>
<Pencil className="h-4 w-4" />
</Button>
<DeleteConfirmButton
title="Wettbewerb löschen"
description="Möchten Sie diesen Wettbewerb wirklich löschen? Diese Aktion kann nicht rückgängig gemacht werden."
isPending={isDeleting}
onConfirm={() =>
executeDelete({
competitionId: String(comp.id),
accountId,
})
}
/>
</div>
</td>
</tr>
);
})}

View File

@@ -8,3 +8,5 @@ export { StockingDataTable } from './stocking-data-table';
export { CreateStockingForm } from './create-stocking-form';
export { CatchBooksDataTable } from './catch-books-data-table';
export { CompetitionsDataTable } from './competitions-data-table';
export { LeasesDataTable } from './leases-data-table';
export { PermitsDataTable } from './permits-data-table';

View File

@@ -0,0 +1,125 @@
'use client';
import { useRouter } from 'next/navigation';
import { formatDate } from '@kit/shared/dates';
import { Badge } from '@kit/ui/badge';
import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card';
import { useActionWithToast } from '@kit/ui/use-action-with-toast';
import { LEASE_PAYMENT_LABELS } from '../lib/fischerei-constants';
import { deleteLease } from '../server/actions/fischerei-actions';
import { DeleteConfirmButton } from './delete-confirm-button';
interface LeasesDataTableProps {
data: Array<Record<string, unknown>>;
total: number;
accountId: string;
}
export function LeasesDataTable({
data,
total,
accountId,
}: LeasesDataTableProps) {
const router = useRouter();
const { execute: executeDelete, isPending: isDeleting } = useActionWithToast(
deleteLease,
{
successMessage: 'Pacht gelöscht',
onSuccess: () => router.refresh(),
},
);
return (
<Card>
<CardHeader>
<CardTitle>Pachten ({total})</CardTitle>
</CardHeader>
<CardContent>
{data.length === 0 ? (
<div className="flex flex-col items-center justify-center rounded-lg border border-dashed p-12 text-center">
<h3 className="text-lg font-semibold">Keine Pachten vorhanden</h3>
<p className="text-muted-foreground mt-1 max-w-sm text-sm">
Erstellen Sie Ihren ersten Pachtvertrag.
</p>
</div>
) : (
<div className="rounded-md border">
<table className="w-full text-sm">
<thead>
<tr className="bg-muted/50 border-b">
<th className="p-3 text-left font-medium">Verpächter</th>
<th className="p-3 text-left font-medium">Gewässer</th>
<th className="p-3 text-left font-medium">Beginn</th>
<th className="p-3 text-left font-medium">Ende</th>
<th className="p-3 text-right font-medium">
Jahresbetrag (EUR)
</th>
<th className="p-3 text-left font-medium">Zahlungsart</th>
<th className="p-3 text-right font-medium">Aktionen</th>
</tr>
</thead>
<tbody>
{data.map((lease) => {
const waters = lease.waters as Record<string, unknown> | null;
const paymentMethod = String(
lease.payment_method ?? 'ueberweisung',
);
return (
<tr
key={String(lease.id)}
className="hover:bg-muted/30 border-b"
>
<td className="p-3 font-medium">
{String(lease.lessor_name)}
</td>
<td className="p-3">
{waters ? String(waters.name) : '\u2014'}
</td>
<td className="p-3">
{formatDate(lease.start_date as string | null)}
</td>
<td className="p-3">
{lease.end_date
? formatDate(lease.end_date as string | null)
: 'unbefristet'}
</td>
<td className="p-3 text-right">
{lease.initial_amount != null
? `${Number(lease.initial_amount).toLocaleString('de-DE', { minimumFractionDigits: 2 })} \u20AC`
: '\u2014'}
</td>
<td className="p-3">
<Badge variant="outline">
{LEASE_PAYMENT_LABELS[paymentMethod] ?? paymentMethod}
</Badge>
</td>
<td className="p-3 text-right">
<div className="flex items-center justify-end gap-1">
<DeleteConfirmButton
title="Pacht löschen"
description="Möchten Sie diesen Pachtvertrag wirklich löschen? Diese Aktion kann nicht rückgängig gemacht werden."
isPending={isDeleting}
onConfirm={() =>
executeDelete({
leaseId: String(lease.id),
accountId,
})
}
/>
</div>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
</CardContent>
</Card>
);
}

View File

@@ -0,0 +1,107 @@
'use client';
import { useRouter } from 'next/navigation';
import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card';
import { useActionWithToast } from '@kit/ui/use-action-with-toast';
import { deletePermit } from '../server/actions/fischerei-actions';
import { DeleteConfirmButton } from './delete-confirm-button';
interface PermitsDataTableProps {
data: Array<Record<string, unknown>>;
accountId: string;
}
export function PermitsDataTable({ data, accountId }: PermitsDataTableProps) {
const router = useRouter();
const { execute: executeDelete, isPending: isDeleting } = useActionWithToast(
deletePermit,
{
successMessage: 'Erlaubnisschein gelöscht',
onSuccess: () => router.refresh(),
},
);
return (
<Card>
<CardHeader>
<CardTitle>Erlaubnisscheine ({data.length})</CardTitle>
</CardHeader>
<CardContent>
{data.length === 0 ? (
<div className="flex flex-col items-center justify-center rounded-lg border border-dashed p-12 text-center">
<h3 className="text-lg font-semibold">
Keine Erlaubnisscheine vorhanden
</h3>
<p className="text-muted-foreground mt-1 max-w-sm text-sm">
Erstellen Sie Ihren ersten Erlaubnisschein.
</p>
</div>
) : (
<div className="rounded-md border">
<table className="w-full text-sm">
<thead>
<tr className="bg-muted/50 border-b">
<th className="p-3 text-left font-medium">Bezeichnung</th>
<th className="p-3 text-left font-medium">Kurzcode</th>
<th className="p-3 text-left font-medium">Hauptgewässer</th>
<th className="p-3 text-right font-medium">Gesamtmenge</th>
<th className="p-3 text-center font-medium">Zum Verkauf</th>
<th className="p-3 text-right font-medium">Aktionen</th>
</tr>
</thead>
<tbody>
{data.map((permit) => {
const waters = permit.waters as Record<
string,
unknown
> | null;
return (
<tr
key={String(permit.id)}
className="hover:bg-muted/30 border-b"
>
<td className="p-3 font-medium">{String(permit.name)}</td>
<td className="text-muted-foreground p-3">
{String(permit.short_code ?? '\u2014')}
</td>
<td className="p-3">
{waters ? String(waters.name) : '\u2014'}
</td>
<td className="p-3 text-right">
{permit.total_quantity != null
? String(permit.total_quantity)
: '\u2014'}
</td>
<td className="p-3 text-center">
{permit.is_for_sale ? '\u2713' : '\u2014'}
</td>
<td className="p-3 text-right">
<div className="flex items-center justify-end gap-1">
<DeleteConfirmButton
title="Erlaubnisschein löschen"
description="Möchten Sie diesen Erlaubnisschein wirklich löschen? Diese Aktion kann nicht rückgängig gemacht werden."
isPending={isDeleting}
onConfirm={() =>
executeDelete({
permitId: String(permit.id),
accountId,
})
}
/>
</div>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
</CardContent>
</Card>
);
}

View File

@@ -291,6 +291,25 @@ export const updateLease = authActionClient
return { success: true, data: result };
});
export const deleteLease = authActionClient
.inputSchema(
z.object({
leaseId: z.string().uuid(),
accountId: z.string().uuid(),
}),
)
.action(async ({ parsedInput: input }) => {
const client = getSupabaseServerClient();
const logger = await getLogger();
const api = createFischereiApi(client);
logger.info({ name: 'fischerei.lease.delete' }, 'Deleting lease...');
await api.deleteLease(input.leaseId);
logger.info({ name: 'fischerei.lease.delete' }, 'Lease deleted');
revalidatePath('/home/[account]/fischerei', 'page');
return { success: true };
});
// =====================================================
// Catch Books
// =====================================================
@@ -386,6 +405,28 @@ export const reviewCatchBook = authActionClient
return { success: true, data: result };
});
export const deleteCatchBook = authActionClient
.inputSchema(
z.object({
catchBookId: z.string().uuid(),
accountId: z.string().uuid(),
}),
)
.action(async ({ parsedInput: input }) => {
const client = getSupabaseServerClient();
const logger = await getLogger();
const api = createFischereiApi(client);
logger.info(
{ name: 'fischerei.catchBook.delete' },
'Deleting catch book...',
);
await api.deleteCatchBook(input.catchBookId);
logger.info({ name: 'fischerei.catchBook.delete' }, 'Catch book deleted');
revalidatePath('/home/[account]/fischerei', 'page');
return { success: true };
});
// =====================================================
// Catches
// =====================================================
@@ -473,6 +514,25 @@ export const updatePermit = authActionClient
return { success: true, data: result };
});
export const deletePermit = authActionClient
.inputSchema(
z.object({
permitId: z.string().uuid(),
accountId: z.string().uuid(),
}),
)
.action(async ({ parsedInput: input }) => {
const client = getSupabaseServerClient();
const logger = await getLogger();
const api = createFischereiApi(client);
logger.info({ name: 'fischerei.permit.delete' }, 'Deleting permit...');
await api.deletePermit(input.permitId);
logger.info({ name: 'fischerei.permit.delete' }, 'Permit deleted');
revalidatePath('/home/[account]/fischerei', 'page');
return { success: true };
});
// =====================================================
// Inspectors
// =====================================================

View File

@@ -670,6 +670,14 @@ export function createFischereiApi(client: SupabaseClient<Database>) {
return data;
},
async deleteLease(leaseId: string) {
const { error } = await client
.from('fishing_leases')
.delete()
.eq('id', leaseId);
if (error) throw error;
},
// =====================================================
// Catch Books
// =====================================================
@@ -836,6 +844,14 @@ export function createFischereiApi(client: SupabaseClient<Database>) {
return data;
},
async deleteCatchBook(catchBookId: string) {
const { error } = await client
.from('catch_books')
.delete()
.eq('id', catchBookId);
if (error) throw error;
},
// =====================================================
// Catches
// =====================================================
@@ -1010,6 +1026,24 @@ export function createFischereiApi(client: SupabaseClient<Database>) {
return data;
},
async getPermit(permitId: string) {
const { data, error } = await client
.from('fishing_permits')
.select('*, waters ( id, name )')
.eq('id', permitId)
.single();
if (error) throw error;
return data;
},
async deletePermit(permitId: string) {
const { error } = await client
.from('fishing_permits')
.delete()
.eq('id', permitId);
if (error) throw error;
},
// =====================================================
// Inspectors
// =====================================================