Skip to content

Commit e17025b

Browse files
committed
fix error add product
1 parent 6b61a2c commit e17025b

File tree

3 files changed

+207
-19
lines changed

3 files changed

+207
-19
lines changed

app/(dashboard)/admin/products/new/page.tsx

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,18 @@ const AddNewProduct = () => {
4343
return;
4444
}
4545

46-
// Sanitize form data before sending to API
47-
const sanitizedProduct = sanitizeFormData(product);
46+
try {
47+
// Sanitize form data before sending to API
48+
const sanitizedProduct = sanitizeFormData(product);
4849

49-
const requestOptions: any = {
50-
method: "post",
51-
headers: { "Content-Type": "application/json" },
52-
body: JSON.stringify(sanitizedProduct),
53-
};
54-
apiClient.post(`/api/products`, requestOptions)
55-
.then((response) => {
56-
if (response.status === 201) {
57-
return response.json();
58-
}
59-
})
60-
.then((data) => {
50+
console.log("Sending product data:", sanitizedProduct);
51+
52+
// Correct usage of apiClient.post
53+
const response = await apiClient.post(`/api/products`, sanitizedProduct);
54+
55+
if (response.status === 201) {
56+
const data = await response.json();
57+
console.log("Product created successfully:", data);
6158
toast.success("Product added successfully");
6259
setProduct({
6360
merchantId: "",
@@ -68,12 +65,17 @@ const AddNewProduct = () => {
6865
mainImage: "",
6966
description: "",
7067
slug: "",
71-
categoryId: "",
68+
categoryId: categories[0]?.id || "",
7269
});
73-
})
74-
.catch((error) => {
75-
toast.error("Error adding product");
76-
});
70+
} else {
71+
const errorData = await response.json();
72+
console.error("Failed to create product:", errorData);
73+
toast.error(`"Error:" ${errorData.message || "Failed to add product"}`);
74+
}
75+
} catch (error) {
76+
console.error("Error adding product:", error);
77+
toast.error("Network error. Please try again.");
78+
}
7779
};
7880

7981
const fetchMerchants = async () => {

0 commit comments

Comments
 (0)