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:
@@ -1,6 +1,9 @@
|
||||
"use server";
|
||||
|
||||
import {CreateCustomerDto} from "@/services/customers/dtos/createCustomer.dto";
|
||||
import {customerRoutes} from "@/app/api/customers/customerRoutes";
|
||||
import {callApi} from "@/lib/api/callApi";
|
||||
import {UUID} from "node:crypto";
|
||||
|
||||
export async function addCustomer(params: CreateCustomerDto): Promise<void> {
|
||||
const {email, name, companyName, street, zip, city, phoneNumbers, notes} = params;
|
||||
@@ -16,15 +19,6 @@ export async function addCustomer(params: CreateCustomerDto): Promise<void> {
|
||||
notes: notes.map(({text}) => ({text})),
|
||||
};
|
||||
|
||||
const response = await fetch('/api/customers', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to create customer: ${response.statusText}`);
|
||||
}
|
||||
const response = await callApi<UUID>(customerRoutes.create, "POST", payload);
|
||||
console.log(response);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user