Remove callApi, refactor API integrations, and adjust error handling

- Delete unused `callApi` utility and related imports across components.
- Replace `callApi` with direct `fetch` usage in `validateCustomer` and `addCustomer`.
- Update `customerRoutes` to include `/api` prefix for consistency.
- Refactor `useErrorHandler` to ensure comprehensive state management during errors.
- Improve `ErrorBoundary` component text for better clarity in fallback UI.
- Align `CustomersPage` logic with `useCallback` for optimized dependency management.
This commit is contained in:
2025-07-11 20:21:45 +02:00
parent 86be1e8920
commit 0724f3b1e7
9 changed files with 84 additions and 54 deletions

View File

@@ -1,8 +1,4 @@
"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> {
@@ -19,6 +15,28 @@ export async function addCustomer(params: CreateCustomerDto): Promise<void> {
notes: notes.map(({text}) => ({text})),
};
const response = await callApi<UUID>(customerRoutes.create, "POST", payload);
const res = await fetch('/api/customers', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
const contentType = res.headers.get("content-type") ?? "";
const isJson = contentType.includes("application/json");
const rawBody = isJson ? await res.json() : await res.text();
console.log(`[api /api/customers] Response:`, res.status, rawBody);
if (!res.ok) {
const errorMessage = isJson
? (rawBody?.message ?? rawBody?.errors?.join(", ")) ?? "Unbekannter Fehler"
: String(rawBody);
throw new Error(errorMessage);
}
const response = rawBody as UUID;
console.log(response);
}