"use client"; import {useState} from "react"; import {motion} from "framer-motion"; import {Trash2} from "lucide-react"; import {Button} from "@/components/ui/button"; import {Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger,} from "@/components/ui/dialog"; import {Label} from "@/components/ui/label"; import {Input} from "@/components/ui/input"; import {Textarea} from "@/components/ui/textarea"; import {Progress} from "@/components/ui/progress"; import {Card, CardContent, CardHeader} from "@/components/ui/card"; import {CreateCustomerDto, NoteDto, PhoneNumberDto} from "@/services/customers/dtos/createCustomer.dto"; import {addCustomer} from "@/services/customers/usecases/addCustomer"; import {validateCustomer} from "@/services/customers/usecases/validateCustomer"; import {useErrorHandler} from "@/components/error-boundary"; import {showInfoToast, showSuccessToast} from "@/lib/ui/showToast"; interface NewCustomerModalProps { onCustomerCreated?: () => void; } export function NewCustomerModal({ onCustomerCreated }: NewCustomerModalProps) { const [step, setStep] = useState(1); const [open, setOpen] = useState(false); const [email, setEmail] = useState(""); const [name, setName] = useState(""); const [companyName, setCompanyName] = useState(""); const [phoneNumbers, setPhoneNumbers] = useState([{note: "", number: ""}]); const [notes, setNotes] = useState([{text: ""}]); const [street, setStreet] = useState(""); const [zip, setZip] = useState(""); const [city, setCity] = useState(""); const [matches, setMatches] = useState([]); const [showDetailModal, setShowDetailModal] = useState(false); const [selectedCustomer] = useState(null); const handleError = useErrorHandler(); type CustomerMatch = { id: string; name: string; email: string; companyName: string; street: string; zip: string; city: string; }; const emailExists = Array.isArray(matches) && matches.some(m => m.email.toLowerCase() === email.toLowerCase()); const companyExists = Array.isArray(matches) && matches.some(m => m.companyName.toLowerCase() === companyName.toLowerCase()); const addressExists = Array.isArray(matches) && matches.some(m => m.street.toLowerCase() === street.toLowerCase() && m.zip.toLowerCase() === zip.toLowerCase() && m.city.toLowerCase() === city.toLowerCase() ); const validateField = async () => { try { const result = await validateCustomer({email, companyName, street, zip, city}); setMatches(result || []); showInfoToast("Datenvalidierung abgeschlossen"); } catch (err) { setMatches([]); // Ensure matches is always an array handleError(err); } }; const handleSubmit = async () => { if (!email || !name || !companyName || !street || !zip || !city) return; try { const payload: CreateCustomerDto = {email, name, companyName, street, zip, city, phoneNumbers, notes}; await addCustomer(payload); showSuccessToast("Kunde erfolgreich erstellt"); setOpen(false); if (onCustomerCreated) { onCustomerCreated(); } } catch (err) { handleError(err); } }; const renderFormInput = ( label: string, value: string, onChange: (value: string) => void, error?: boolean, className?: string ) => (
onChange(e.target.value)} onBlur={validateField} className={error ? "border border-red-500" : className} />
); const renderCustomerInfo = (customer: CustomerMatch) => (
Name: {customer.name}
Firma: {customer.companyName}
E-Mail: {customer.email}
Adresse: {customer.street}, {customer.zip} {customer.city}
); const updatePhoneNumber = (i: number, key: keyof PhoneNumberDto, value: string) => { const updated = [...phoneNumbers]; updated[i][key] = value; setPhoneNumbers(updated); }; const removePhoneNumber = (i: number) => { setPhoneNumbers(phoneNumbers.filter((_, idx) => idx !== i)); }; const updateNote = (i: number, value: string) => { const updated = [...notes]; updated[i].text = value; setNotes(updated); }; const renderStepOne = () => (
{renderFormInput("Name", name, setName)} {renderFormInput("Firma", companyName, setCompanyName, companyExists)} {renderFormInput("E-Mail", email, setEmail, emailExists)} {emailExists && (
Ein Kunde mit dieser E-Mail existiert bereits.
)}
{phoneNumbers.map((p, i) => (
updatePhoneNumber(i, "number", e.target.value)} /> updatePhoneNumber(i, "note", e.target.value)} />
))}
); const renderStepTwo = () => (
{renderFormInput("Straße", street, setStreet, addressExists)}
{renderFormInput("PLZ", zip, setZip, addressExists)}
{renderFormInput("Ort", city, setCity, addressExists)}
{notes.map((note, i) => (