"use client" import { useActionState, useEffect, useState } from "react"; import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { saveStammdaten, StammdatenState } from "@/modules/shared/actions/stammdaten"; import { Building2, CreditCard, FileText, MapPin, User, type LucideIcon, } from "lucide-react"; import { LEGAL_FORMS } from "@/modules/shared/data/stammdaten-data"; type Client = { company_name: string; legal_form: string; industry: string | null; vat_id: string | null; tax_number: string | null; commercial_register_number: string | null; street: string; postal_code: string; city: string; country: string; billing_street: string | null; billing_postal_code: string | null; billing_city: string | null; billing_country: string | null; contact_name: string; contact_position: string | null; contact_email: string; contact_phone: string | null; } | null; type PaymentDetails = { payment_method: "invoice" | "sepa_debit" | "card"; account_holder: string | null; iban: string | null; bic: string | null; } | null; const selectClassName = "flex h-10 w-full rounded-md border border-input bg-background px-3 text-body outline-none transition-colors placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50"; function FormSection({ icon: Icon, title, children, }: { icon: LucideIcon; title: string; children: React.ReactNode; }) { return (

{title}

{children}
); } function TextField({ name, label, defaultValue, type = "text", required, disabled, }: { name: string; label: string; defaultValue?: string | null; type?: string; required?: boolean; disabled?: boolean; }) { return ( {label} {required && *} ); } function SelectField({ name, label, value, defaultValue, onChange, options, required, disabled, }: { name: string; label: string; value?: string; defaultValue?: string; onChange?: (value: string) => void; options: { value: string; label: string }[]; required?: boolean; disabled?: boolean; }) { return ( {label} {required && *} ); } const StammdatenForm = ({ profileId, client, paymentDetails, isEditing, onDone, }: { profileId: string; client: Client; paymentDetails: PaymentDetails; isEditing: boolean; onDone: () => void; }) => { const [state, formAction, pending] = useActionState( saveStammdaten, undefined ); const [paymentMethod, setPaymentMethod] = useState(paymentDetails?.payment_method ?? "invoice"); const [hasDifferentBillingAddress, setHasDifferentBillingAddress] = useState( Boolean(client?.billing_street) ); useEffect(() => { if (state && "success" in state && state.success) { onDone(); } }, [state, onDone]); const disabled = !isEditing || pending; return (
({ value: form, label: form }))} />
{hasDifferentBillingAddress && ( )}
setPaymentMethod(value as typeof paymentMethod)} disabled={disabled} options={[ { value: "invoice", label: "Rechnung" }, { value: "sepa_debit", label: "SEPA-Lastschrift" }, { value: "card", label: "Kreditkarte" }, ]} /> {paymentMethod === "sepa_debit" && ( <> )}
{state && "error" in state && (

{state.error}

)} {isEditing && (
)}
); }; export default StammdatenForm;