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)} + /> +
+
+ +