fixed order and cart req

This commit is contained in:
Jelaletdin12
2026-03-13 15:39:24 +05:00
parent c623fd19d6
commit 69e3e2e0ab
3 changed files with 27 additions and 10 deletions

View File

@@ -40,21 +40,34 @@ function transformCartResponse(response: any): CartResponse {
(response.trim().startsWith("<!DOCTYPE") ||
response.trim().startsWith("<html"))
) {
return {
message: "error",
data: [],
errorDetails: "Server returned HTML instead of JSON.",
};
return { message: "error", data: [], errorDetails: "Server returned HTML instead of JSON." };
}
if (typeof response === "object") {
if (response.data) return response;
return { message: "success", data: [] };
const normalize = (raw: any): CartItem[] => {
if (Array.isArray(raw)) return raw;
// GET response: data = { tmpost: [...], ... } — merge all channel arrays
if (typeof raw === "object" && raw !== null) {
return Object.values(raw).flat() as CartItem[];
}
return [];
};
if (typeof response === "object" && response !== null) {
return {
message: response.message || "success",
data: normalize(response.data),
};
}
if (typeof response === "string") {
try {
return JSON.parse(response);
const parsed = JSON.parse(response);
return {
message: parsed.message || "success",
data: normalize(parsed.data),
};
} catch {
return { message: "error", data: [] };
}
@@ -471,6 +484,7 @@ export function useCreateOrder() {
delivery_at?: string;
region: string;
notes?: string;
product_ids: number[];
}) => {
const response = await apiClient.post("/orders", payload);
return response.data;