"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { CalendarIcon } from "lucide-react"; import { format } from "date-fns"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Calendar } from "@/components/ui/calendar"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { trpc } from "@/utils/trpc"; import { Header } from "@/components/layout/header"; import { Footer } from "@/components/layout/footer"; const formSchema = z.object({ name: z.string().min(1, "Name wird benötigt"), birthDate: z.date({ required_error: "Bitte wähle ein Geburtsdatum", }), birthTime: z.string().regex(/^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/, "Ungültiges Zeitformat (HH:MM)").optional(), gender: z.enum(["male", "female"], { required_error: "Bitte wähle das Geschlecht", }), }); type FormValues = z.infer; export default function AddChildPage() { const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const [dateInputValue, setDateInputValue] = useState(""); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { name: "", gender: "male", birthTime: "", }, }); const addChild = trpc.child.add.useMutation({ onSuccess: () => { toast.success("Kind hinzugefügt", { description: "Das Kind wurde erfolgreich hinzugefügt." }); form.reset(); router.push("/app"); router.refresh(); }, onError: (err) => { toast.error("Fehler", { description: err.message }); setIsLoading(false); }, }); function onSubmit(values: FormValues) { setIsLoading(true); // Create a date with the selected time or default to noon let birthDateTime = values.birthDate; if (values.birthTime) { const [hours, minutes] = values.birthTime.split(':').map(Number); birthDateTime = new Date(Date.UTC( values.birthDate.getUTCFullYear(), values.birthDate.getUTCMonth(), values.birthDate.getUTCDate(), hours, minutes, 0 )); } else { // Default to noon if no time is specified birthDateTime = new Date(Date.UTC( values.birthDate.getUTCFullYear(), values.birthDate.getUTCMonth(), values.birthDate.getUTCDate(), 12, 0, 0 )); } addChild.mutate({ name: values.name, birthDate: birthDateTime.toISOString(), gender: values.gender, }); } return ( <>

Kind hinzufügen

( Name )} /> ( Geburtsdatum
{ setDateInputValue(e.target.value); }} onBlur={(e) => { const inputValue = e.target.value; // Allow empty input if (!inputValue) { field.onChange(null); return; } // Check if the input matches the expected format (DD.MM.YYYY) const dateRegex = /^(\d{1,2})\.(\d{1,2})\.(\d{4})$/; const match = inputValue.match(dateRegex); if (match) { const day = parseInt(match[1], 10); const month = parseInt(match[2], 10) - 1; // JavaScript months are 0-indexed const year = parseInt(match[3], 10); // Create a date object and validate it // Set the time to noon UTC to avoid timezone issues const date = new Date(Date.UTC(year, month, day, 12, 0, 0)); // Check if the date is valid (e.g., not February 30th) if ( date.getUTCDate() === day && date.getUTCMonth() === month && date.getUTCFullYear() === year && date <= new Date() && date >= new Date("1900-01-01") ) { field.onChange(date); setDateInputValue(format(date, "dd.MM.yyyy")); } else { // If invalid, reset to the last valid value setDateInputValue(field.value ? format(field.value, "dd.MM.yyyy") : ""); } } else { // If format is wrong, reset to the last valid value setDateInputValue(field.value ? format(field.value, "dd.MM.yyyy") : ""); } }} />
{ field.onChange(date); if (date) { setDateInputValue(format(date, "dd.MM.yyyy")); } else { setDateInputValue(""); } }} disabled={(date) => date > new Date() || date < new Date("1900-01-01") } initialFocus />
)} /> ( Geburtszeit (optional) )} /> ( Geschlecht )} />