fixed order and cart req
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user