Enhance NewCustomerModal with callback support and toast notifications
- Add `onCustomerCreated` callback to refresh customer list after creation. - Integrate `showInfoToast` and `showSuccessToast` for validation and creation feedback. - Prevent closing modal on backdrop click; add explicit cancel button. - Refactor `addCustomer` to use `callApi` and centralized routes. - Simplify customer fetching logic in `CustomersPage` with reusable function.
This commit is contained in:
@@ -14,8 +14,13 @@ import {CreateCustomerDto, NoteDto, PhoneNumberDto} from "@/services/customers/d
|
||||
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";
|
||||
|
||||
export function NewCustomerModal() {
|
||||
interface NewCustomerModalProps {
|
||||
onCustomerCreated?: () => void;
|
||||
}
|
||||
|
||||
export function NewCustomerModal({ onCustomerCreated }: NewCustomerModalProps) {
|
||||
const [step, setStep] = useState(1);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [email, setEmail] = useState("");
|
||||
@@ -53,6 +58,7 @@ export function NewCustomerModal() {
|
||||
try {
|
||||
const result = await validateCustomer({email, companyName, street, zip, city});
|
||||
setMatches(result);
|
||||
showInfoToast("Datenvalidierung abgeschlossen");
|
||||
} catch (err) {
|
||||
handleError(err);
|
||||
}
|
||||
@@ -63,7 +69,11 @@ export function NewCustomerModal() {
|
||||
try {
|
||||
const payload: CreateCustomerDto = {email, name, companyName, street, zip, city, phoneNumbers, notes};
|
||||
await addCustomer(payload);
|
||||
location.reload();
|
||||
showSuccessToast("Kunde erfolgreich erstellt");
|
||||
setOpen(false);
|
||||
if (onCustomerCreated) {
|
||||
onCustomerCreated();
|
||||
}
|
||||
} catch (err) {
|
||||
handleError(err);
|
||||
}
|
||||
@@ -215,14 +225,16 @@ export function NewCustomerModal() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<Dialog open={open} onOpenChange={() => {
|
||||
// Prevent closing on backdrop click - modal can only be closed explicitly
|
||||
}}>
|
||||
<DialogTrigger asChild>
|
||||
<Button onClick={() => {
|
||||
setOpen(true);
|
||||
setStep(1);
|
||||
}}>Neue Kunde</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="w-full max-w-5xl max-h-[95vh] overflow-y-auto">
|
||||
<DialogContent className="w-full max-w-5xl max-h-[95vh] overflow-y-auto" showCloseButton={false}>
|
||||
<DialogHeader><DialogTitle>Neuen Kunden anlegen</DialogTitle></DialogHeader>
|
||||
<div className="mb-4">
|
||||
<div className="text-xs font-semibold mb-2">Schritt {step} von 2</div>
|
||||
@@ -233,9 +245,14 @@ export function NewCustomerModal() {
|
||||
{matches.length > 0 && renderDuplicationCard()}
|
||||
|
||||
<div className="flex justify-between mt-6">
|
||||
<Button variant="secondary" disabled={step === 1} onClick={() => setStep(step - 1)}>
|
||||
Zurück
|
||||
</Button>
|
||||
<div className="flex gap-2">
|
||||
<Button variant="outline" onClick={() => setOpen(false)}>
|
||||
Abbrechen
|
||||
</Button>
|
||||
<Button variant="secondary" disabled={step === 1} onClick={() => setStep(step - 1)}>
|
||||
Zurück
|
||||
</Button>
|
||||
</div>
|
||||
{step === 1 ? (
|
||||
<Button
|
||||
onClick={() => setStep(2)}
|
||||
|
||||
Reference in New Issue
Block a user