diff --git a/ApiCore-Frontend/src/app/(app)/apiaries/[id]/page.tsx b/ApiCore-Frontend/src/app/(app)/apiaries/[id]/page.tsx index 1ab809a..ef3462e 100644 --- a/ApiCore-Frontend/src/app/(app)/apiaries/[id]/page.tsx +++ b/ApiCore-Frontend/src/app/(app)/apiaries/[id]/page.tsx @@ -1,8 +1,10 @@ "use client"; +import Link from "next/link"; import { useParams } from "next/navigation"; import { useQuery } from "@tanstack/react-query"; import { fetchApiary } from "@/lib/apiaries/api"; +import { Button } from "@/components/ui/button"; import { ApiaryEditCard } from "./apiary-edit-card"; export default function ApiaryDetailPage() { @@ -24,8 +26,15 @@ export default function ApiaryDetailPage() { } return ( -
+
+
); } diff --git a/ApiCore-Frontend/src/app/(app)/extractions/new/page.tsx b/ApiCore-Frontend/src/app/(app)/extractions/new/page.tsx new file mode 100644 index 0000000..c9d65a6 --- /dev/null +++ b/ApiCore-Frontend/src/app/(app)/extractions/new/page.tsx @@ -0,0 +1,166 @@ +"use client"; + +import { FormEvent, useState } from "react"; +import { useRouter, useSearchParams } from "next/navigation"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { fetchHarvests } from "@/lib/harvests/api"; +import { createExtraction } from "@/lib/extractions/api"; +import { Button } from "@/components/ui/button"; +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"; + +function todayIsoDate(): string { + return new Date().toISOString().slice(0, 10); +} + +export default function NewExtractionPage() { + const router = useRouter(); + const searchParams = useSearchParams(); + const queryClient = useQueryClient(); + + const { data: harvests } = useQuery({ queryKey: ["harvests"], queryFn: fetchHarvests }); + + const preselectedHarvestId = searchParams.get("harvestId"); + const [date, setDate] = useState(todayIsoDate()); + const [harvestIds, setHarvestIds] = useState( + preselectedHarvestId ? [preselectedHarvestId] : [], + ); + const [amount, setAmount] = useState(""); + const [room, setRoom] = useState(""); + const [waterContent, setWaterContent] = useState(""); + const [notes, setNotes] = useState(""); + const [error, setError] = useState(null); + + const mutation = useMutation({ + mutationFn: createExtraction, + onSuccess: () => { + void queryClient.invalidateQueries({ queryKey: ["extractions"] }); + router.push("/colonies"); + }, + onError: () => { + setError("Schleuderung konnte nicht gespeichert werden."); + }, + }); + + function toggleHarvest(harvestId: string, checked: boolean) { + setHarvestIds((current) => + checked ? [...current, harvestId] : current.filter((id) => id !== harvestId), + ); + } + + function handleSubmit(event: FormEvent) { + event.preventDefault(); + setError(null); + if (harvestIds.length === 0) { + setError("Bitte mindestens eine Ernte auswählen."); + return; + } + mutation.mutate({ + date: new Date(date).toISOString(), + harvestIds, + amount: amount ? Number(amount) : undefined, + room: room || undefined, + waterContent: waterContent ? Number(waterContent) : undefined, + notes: notes || undefined, + }); + } + + return ( +
+ + + Neue Schleuderung + + +
+
+ + setDate(event.target.value)} + /> +
+
+ + {harvests?.length === 0 ? ( +

Noch keine Ernten vorhanden.

+ ) : null} +
+ {harvests?.map((harvest) => ( + + ))} +
+
+
+ + setAmount(event.target.value)} + /> +
+
+ + setRoom(event.target.value)} + /> +
+
+ + setWaterContent(event.target.value)} + /> +
+
+ +