From 6e328cdacbd34f83ab60f818a39cb6d74c7d681d Mon Sep 17 00:00:00 2001 From: Nick Szaunig Date: Fri, 21 Aug 2026 18:21:10 +0200 Subject: [PATCH] feat(frontend): Voelker-Views (Modul 3) + Refresh-Token-Race-Fix /colonies: Liste; /colonies/new: Anlegeformular mit Bienenstand-/ Volkstyp-Auswahl; /colonies/[id]: Bearbeiten, Aktionen (Umsetzen, Ableger bilden, Vereinigen, Aufloesen je als Dialog/Button) und Historie-Anzeige. Aktionen deaktiviert, sobald das Volk nicht mehr ACTIVE ist (Frontend-Pendant zum neuen Backend-Guard). Beim Live-Testen im Browser gefunden und behoben: - Echter Root-Cause fuer den fruehren Session-Abbruch nach Reload: der Refresh-Token ist einmalig/rotierend, und React StrictMode ruft den Session-Restore-Effect im Dev-Modus doppelt auf. Der vorherige AbortController-Fix verhinderte nur, dass der Client die verworfene Antwort nutzt - er verhinderte NICHT, dass die zweite Anfrage bereits serverseitig verarbeitet wurde, bevor abort() griff. Fix: echtes Request-Deduplizieren in refreshRequest() (gemeinsames In-Flight- Promise) statt Abbruchversuch - der Standardansatz fuer rotierende Refresh-Token. - Historie-Text beim Vereinigen war fuer die aufnehmende Seite falsch ("Entstanden durch Vereinigung" impliziert Neuanlage) - jetzt "Anderes Volk eingegliedert". Nav-Leiste und proxy.ts-Matcher um /colonies erweitert. 9 Vitest-Tests, Lint, Typecheck und Build gruen. Kompletter Flow (Anlegen, Umsetzen, Ableger bilden, Vereinigen, Aufloesen, Historie) live im Browser gegen den echten Server verifiziert, inkl. Rollen- filterung in den Aktions-Dialogen. --- ApiCore-Frontend/proxy.ts | 2 +- .../(app)/colonies/[id]/colony-actions.tsx | 274 ++++++++++++++++++ .../(app)/colonies/[id]/colony-edit-card.tsx | 105 +++++++ .../(app)/colonies/[id]/colony-history.tsx | 57 ++++ .../src/app/(app)/colonies/[id]/page.tsx | 35 +++ .../src/app/(app)/colonies/new/page.tsx | 129 +++++++++ .../src/app/(app)/colonies/page.spec.tsx | 49 ++++ .../src/app/(app)/colonies/page.tsx | 69 +++++ ApiCore-Frontend/src/app/(app)/layout.tsx | 3 + ApiCore-Frontend/src/lib/auth/api.ts | 20 +- .../src/lib/auth/auth-context.tsx | 16 +- ApiCore-Frontend/src/lib/colonies/api.ts | 61 ++++ ApiCore-Frontend/src/lib/colonies/types.ts | 100 +++++++ TASKS.md | 2 +- 14 files changed, 904 insertions(+), 18 deletions(-) create mode 100644 ApiCore-Frontend/src/app/(app)/colonies/[id]/colony-actions.tsx create mode 100644 ApiCore-Frontend/src/app/(app)/colonies/[id]/colony-edit-card.tsx create mode 100644 ApiCore-Frontend/src/app/(app)/colonies/[id]/colony-history.tsx create mode 100644 ApiCore-Frontend/src/app/(app)/colonies/[id]/page.tsx create mode 100644 ApiCore-Frontend/src/app/(app)/colonies/new/page.tsx create mode 100644 ApiCore-Frontend/src/app/(app)/colonies/page.spec.tsx create mode 100644 ApiCore-Frontend/src/app/(app)/colonies/page.tsx create mode 100644 ApiCore-Frontend/src/lib/colonies/api.ts create mode 100644 ApiCore-Frontend/src/lib/colonies/types.ts diff --git a/ApiCore-Frontend/proxy.ts b/ApiCore-Frontend/proxy.ts index 970ac5e..f9ffd47 100644 --- a/ApiCore-Frontend/proxy.ts +++ b/ApiCore-Frontend/proxy.ts @@ -22,5 +22,5 @@ export function proxy(request: NextRequest) { } export const config = { - matcher: ["/login", "/settings/:path*", "/apiaries/:path*"], + matcher: ["/login", "/settings/:path*", "/apiaries/:path*", "/colonies/:path*"], }; diff --git a/ApiCore-Frontend/src/app/(app)/colonies/[id]/colony-actions.tsx b/ApiCore-Frontend/src/app/(app)/colonies/[id]/colony-actions.tsx new file mode 100644 index 0000000..6e5b60e --- /dev/null +++ b/ApiCore-Frontend/src/app/(app)/colonies/[id]/colony-actions.tsx @@ -0,0 +1,274 @@ +"use client"; + +import { FormEvent, useState } from "react"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { dissolveColony, mergeColony, moveColony, splitColony } from "@/lib/colonies/api"; +import { Colony, ColonyType } from "@/lib/colonies/types"; +import { fetchApiaries } from "@/lib/apiaries/api"; +import { fetchColonies } from "@/lib/colonies/api"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { + Dialog, + DialogContent, + DialogFooter, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; + +const COLONY_TYPES: ColonyType[] = [ + "PRODUCTION_COLONY", + "NUCLEUS_COLONY", + "YOUNG_COLONY", + "CARETAKER_COLONY", + "MATING_NUCLEUS", + "RESERVE_COLONY", +]; + +function invalidateColony( + queryClient: ReturnType, + colonyId: string, +): void { + void queryClient.invalidateQueries({ queryKey: ["colonies"] }); + void queryClient.invalidateQueries({ queryKey: ["colonies", colonyId] }); + void queryClient.invalidateQueries({ queryKey: ["colonies", colonyId, "history"] }); +} + +function MoveDialog({ colony }: { colony: Colony }) { + const queryClient = useQueryClient(); + const [open, setOpen] = useState(false); + const [apiaryId, setApiaryId] = useState(""); + const { data: apiaries } = useQuery({ queryKey: ["apiaries"], queryFn: fetchApiaries }); + + const mutation = useMutation({ + mutationFn: () => moveColony(colony.id, { apiaryId }), + onSuccess: () => { + invalidateColony(queryClient, colony.id); + setOpen(false); + }, + }); + + return ( + + + Umsetzen + + } + /> + + + Volk umsetzen + +
{ + event.preventDefault(); + mutation.mutate(); + }} + > +
+ + +
+ + + +
+
+
+ ); +} + +function SplitDialog({ colony }: { colony: Colony }) { + const queryClient = useQueryClient(); + const [open, setOpen] = useState(false); + const [code, setCode] = useState(""); + const [colonyType, setColonyType] = useState("NUCLEUS_COLONY"); + + const mutation = useMutation({ + mutationFn: () => splitColony(colony.id, { code, colonyType }), + onSuccess: () => { + invalidateColony(queryClient, colony.id); + void queryClient.invalidateQueries({ queryKey: ["colonies"] }); + setOpen(false); + }, + }); + + return ( + + + Ableger bilden + + } + /> + + + Ableger bilden + +
{ + event.preventDefault(); + mutation.mutate(); + }} + > +
+ + setCode(event.target.value)} + /> +
+
+ + +
+ + + +
+
+
+ ); +} + +function MergeDialog({ colony }: { colony: Colony }) { + const queryClient = useQueryClient(); + const [open, setOpen] = useState(false); + const [mergedColonyId, setMergedColonyId] = useState(""); + const { data: colonies } = useQuery({ queryKey: ["colonies"], queryFn: fetchColonies }); + + const mutation = useMutation({ + mutationFn: () => mergeColony(colony.id, { mergedColonyIds: [mergedColonyId] }), + onSuccess: () => { + invalidateColony(queryClient, colony.id); + void queryClient.invalidateQueries({ queryKey: ["colonies"] }); + setOpen(false); + }, + }); + + return ( + + + Vereinigen + + } + /> + + + Völker vereinigen + +
{ + event.preventDefault(); + mutation.mutate(); + }} + > +
+ + +
+ + + +
+
+
+ ); +} + +function DissolveButton({ colony }: { colony: Colony }) { + const queryClient = useQueryClient(); + const mutation = useMutation({ + mutationFn: () => dissolveColony(colony.id), + onSuccess: () => invalidateColony(queryClient, colony.id), + }); + + return ( + + ); +} + +export function ColonyActions({ colony }: { colony: Colony }) { + return ( +
+ + + + +
+ ); +} diff --git a/ApiCore-Frontend/src/app/(app)/colonies/[id]/colony-edit-card.tsx b/ApiCore-Frontend/src/app/(app)/colonies/[id]/colony-edit-card.tsx new file mode 100644 index 0000000..c7c858b --- /dev/null +++ b/ApiCore-Frontend/src/app/(app)/colonies/[id]/colony-edit-card.tsx @@ -0,0 +1,105 @@ +"use client"; + +import { FormEvent, useState } from "react"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; +import { updateColony } from "@/lib/colonies/api"; +import { Colony } from "@/lib/colonies/types"; +import { Button } from "@/components/ui/button"; +import { Badge } from "@/components/ui/badge"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { Textarea } from "@/components/ui/textarea"; +import { + Card, + CardContent, + CardHeader, + CardTitle, +} from "@/components/ui/card"; + +export function ColonyEditCard({ colony }: { colony: Colony }) { + const queryClient = useQueryClient(); + const [strength, setStrength] = useState(colony.strength?.toString() ?? ""); + const [broodStatus, setBroodStatus] = useState(colony.broodStatus ?? ""); + const [feedStatus, setFeedStatus] = useState(colony.feedStatus ?? ""); + const [healthStatus, setHealthStatus] = useState(colony.healthStatus ?? ""); + const [notes, setNotes] = useState(colony.notes ?? ""); + + const updateMutation = useMutation({ + mutationFn: () => + updateColony(colony.id, { + strength: strength ? Number(strength) : undefined, + broodStatus: broodStatus || undefined, + feedStatus: feedStatus || undefined, + healthStatus: healthStatus || undefined, + notes: notes || undefined, + }), + onSuccess: () => { + void queryClient.invalidateQueries({ queryKey: ["colonies"] }); + void queryClient.invalidateQueries({ queryKey: ["colonies", colony.id] }); + }, + }); + + function handleSubmit(event: FormEvent) { + event.preventDefault(); + updateMutation.mutate(); + } + + return ( + + + {colony.code} + + {colony.status} + + + +
+
+ + setStrength(event.target.value)} + /> +
+
+ + setBroodStatus(event.target.value)} + /> +
+
+ + setFeedStatus(event.target.value)} + /> +
+
+ + setHealthStatus(event.target.value)} + /> +
+
+ +