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

@@ -128,6 +128,8 @@ export default function CartPage() {
); );
if (!selectedProvinceData) return; if (!selectedProvinceData) return;
const product_ids = cartItems.map((item) => item.product.id);
createOrder( createOrder(
{ {
customer_name: `${name} ${lastName}`.trim(), customer_name: `${name} ${lastName}`.trim(),
@@ -137,6 +139,7 @@ export default function CartPage() {
payment_type_id: paymentType.id, payment_type_id: paymentType.id,
region: selectedRegion, region: selectedRegion,
notes: note || undefined, notes: note || undefined,
product_ids,
}, },
{ {
onSuccess: () => { onSuccess: () => {

View File

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

View File

@@ -12,7 +12,7 @@ const nextConfig: NextConfig = {
remotePatterns: [ remotePatterns: [
{ {
protocol: "https", protocol: "https",
hostname: "hyzmat.app", hostname: "smartelektronika.com",
// port: "8080", // port: "8080",
}, },
], ],