import { useEffect } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { useForm } from 'react-hook-form'; import { customerApi } from '../../services/api'; import Card from '../../components/ui/Card'; import Button from '../../components/ui/Button'; import Input from '../../components/ui/Input'; import Select from '../../components/ui/Select'; import type { Customer } from '../../types'; type CustomerFormData = Omit; export default function CustomerForm() { const { id } = useParams(); const navigate = useNavigate(); const queryClient = useQueryClient(); const isEdit = !!id; const { register, handleSubmit, reset, watch, setValue, formState: { errors } } = useForm(); const customerType = watch('type'); const { data: customer } = useQuery({ queryKey: ['customer', id], queryFn: () => customerApi.getById(parseInt(id!)), enabled: isEdit, }); useEffect(() => { if (customer?.data) { const data = { ...customer.data }; // Convert date strings to YYYY-MM-DD format for date inputs if (data.birthDate) { data.birthDate = data.birthDate.split('T')[0] as any; } if (data.foundingDate) { data.foundingDate = data.foundingDate.split('T')[0] as any; } reset(data); } }, [customer, reset]); const createMutation = useMutation({ mutationFn: customerApi.create, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['customers'] }); navigate('/customers'); }, }); const updateMutation = useMutation({ mutationFn: (data: Partial) => customerApi.update(parseInt(id!), data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['customers'] }); queryClient.invalidateQueries({ queryKey: ['customer', id] }); navigate(`/customers/${id}`); }, }); const onSubmit = (data: CustomerFormData) => { // Only include the fields that can be updated - exclude relations and read-only fields const submitData: any = { type: data.type, salutation: data.salutation || undefined, firstName: data.firstName, lastName: data.lastName, companyName: data.companyName || undefined, email: data.email || undefined, phone: data.phone || undefined, mobile: data.mobile || undefined, taxNumber: data.taxNumber || undefined, commercialRegisterNumber: data.commercialRegisterNumber || undefined, notes: data.notes || undefined, birthPlace: data.birthPlace || undefined, }; // Handle birthDate - convert non-empty string to ISO string, or null to clear if (data.birthDate && typeof data.birthDate === 'string' && data.birthDate.trim() !== '') { submitData.birthDate = new Date(data.birthDate).toISOString(); } else { submitData.birthDate = null; } // Handle foundingDate for business customers - or null to clear if (data.foundingDate && typeof data.foundingDate === 'string' && data.foundingDate.trim() !== '') { submitData.foundingDate = new Date(data.foundingDate).toISOString(); } else { submitData.foundingDate = null; } if (isEdit) { updateMutation.mutate(submitData); } else { createMutation.mutate(submitData); } }; const isLoading = createMutation.isPending || updateMutation.isPending; const error = createMutation.error || updateMutation.error; return (

{isEdit ? 'Kunde bearbeiten' : 'Neuer Kunde'}

{error && (
{error instanceof Error ? error.message : 'Ein Fehler ist aufgetreten'}
)}
{customerType === 'BUSINESS' && ( <> setValue('foundingDate', '' as any)} /> )} {customerType !== 'BUSINESS' && ( <> setValue('birthDate', '' as any)} /> )}
{customerType === 'BUSINESS' && (
{isEdit && (

Dokumente (Gewerbeanmeldung, Handelsregisterauszug) können nach dem Speichern in der Kundendetailansicht hochgeladen werden.

)}
)}