fix
This commit is contained in:
@@ -51,9 +51,14 @@ export default function ProductsPage() {
|
||||
|
||||
const fetchDataAsync = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
const productsUrl = `${process.env.NEXT_PUBLIC_API_URL}/products`;
|
||||
const categoriesUrl = `${process.env.NEXT_PUBLIC_API_URL}/categories`;
|
||||
|
||||
const [fetchedProducts, fetchedCategories] = await Promise.all([
|
||||
fetchProductData(`${process.env.NEXT_PUBLIC_API_URL}/products`, authToken),
|
||||
fetchProductData(`${process.env.NEXT_PUBLIC_API_URL}/categories`, authToken),
|
||||
fetchProductData(productsUrl, authToken),
|
||||
fetchProductData(categoriesUrl, authToken),
|
||||
]);
|
||||
|
||||
console.log("Fetched Products:", fetchedProducts);
|
||||
@@ -66,14 +71,15 @@ export default function ProductsPage() {
|
||||
|
||||
setProducts(processedProducts);
|
||||
setCategories(fetchedCategories);
|
||||
setLoading(false);
|
||||
} catch (error) {
|
||||
console.error("Error loading data:", error);
|
||||
} finally {
|
||||
console.error("Error fetching data:", error);
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchDataAsync();
|
||||
}, []);
|
||||
}, [router]);
|
||||
|
||||
const handleAddTier = () => {
|
||||
setProductData((prev) => ({
|
||||
@@ -121,8 +127,8 @@ export default function ProductsPage() {
|
||||
|
||||
try {
|
||||
const url = editing
|
||||
? `/api/products/${data._id}`
|
||||
: "/api/products";
|
||||
? `${process.env.NEXT_PUBLIC_API_URL}/products/${data._id}`
|
||||
: `${process.env.NEXT_PUBLIC_API_URL}/products`;
|
||||
|
||||
// Save product data
|
||||
const savedProduct = await saveProductData(
|
||||
@@ -142,8 +148,9 @@ export default function ProductsPage() {
|
||||
);
|
||||
|
||||
if (file) {
|
||||
const imageUrl = `${process.env.NEXT_PUBLIC_API_URL}/products/${savedProduct._id}/image`;
|
||||
await saveProductImage(
|
||||
`/api/products/${savedProduct._id}/image`,
|
||||
imageUrl,
|
||||
file,
|
||||
authToken
|
||||
);
|
||||
@@ -151,8 +158,9 @@ export default function ProductsPage() {
|
||||
|
||||
// If editing and stock values were updated, update stock in the dedicated endpoint
|
||||
if (editing && data.stockTracking !== undefined) {
|
||||
const stockUrl = `${process.env.NEXT_PUBLIC_API_URL}/stock/${data._id}`;
|
||||
await saveProductData(
|
||||
`/api/stock/${data._id}`,
|
||||
stockUrl,
|
||||
{
|
||||
stockTracking: data.stockTracking,
|
||||
currentStock: data.currentStock || 0,
|
||||
@@ -164,7 +172,8 @@ export default function ProductsPage() {
|
||||
}
|
||||
|
||||
// Refresh products list
|
||||
const fetchedProducts = await fetchProductData("/api/products", authToken);
|
||||
const productsUrl = `${process.env.NEXT_PUBLIC_API_URL}/products`;
|
||||
const fetchedProducts = await fetchProductData(productsUrl, authToken);
|
||||
setProducts(fetchedProducts);
|
||||
|
||||
setModalOpen(false);
|
||||
@@ -182,19 +191,27 @@ export default function ProductsPage() {
|
||||
|
||||
// Handle delete product
|
||||
const handleDeleteProduct = async (productId: string) => {
|
||||
const authToken = document.cookie.split("Authorization=")[1];
|
||||
try {
|
||||
await deleteProductData(
|
||||
`${process.env.NEXT_PUBLIC_API_URL}/products/${productId}`,
|
||||
authToken
|
||||
);
|
||||
if (!confirm("Are you sure you want to delete this product?")) return;
|
||||
|
||||
// Remove the product from the state
|
||||
setProducts((prevProducts) =>
|
||||
prevProducts.filter((product) => product._id !== productId)
|
||||
);
|
||||
const authToken = document.cookie
|
||||
.split("; ")
|
||||
.find((row) => row.startsWith("Authorization="))
|
||||
?.split("=")[1];
|
||||
|
||||
if (!authToken) {
|
||||
router.push("/login");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const url = `${process.env.NEXT_PUBLIC_API_URL}/products/${productId}`;
|
||||
await deleteProductData(url, authToken);
|
||||
|
||||
setProducts(products.filter((p) => p._id !== productId));
|
||||
toast.success("Product deleted successfully");
|
||||
} catch (error) {
|
||||
console.error("Error deleting product:", error);
|
||||
toast.error("Failed to delete product");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -32,7 +32,8 @@ export default function StockManagementPage() {
|
||||
|
||||
const fetchDataAsync = async () => {
|
||||
try {
|
||||
const fetchedProducts = await fetchProductData("/api/products", authToken);
|
||||
const apiUrl = `${process.env.NEXT_PUBLIC_API_URL}/products`;
|
||||
const fetchedProducts = await fetchProductData(apiUrl, authToken);
|
||||
setProducts(fetchedProducts);
|
||||
|
||||
// Initialize stock values
|
||||
|
||||
@@ -43,7 +43,6 @@ export const saveProductImage = async(url: string, file:File, authToken: string)
|
||||
method: "PUT",
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
//"Content-Type": "multipart/form-data",
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
@@ -70,9 +69,9 @@ export const deleteProductData = async (url: string, authToken: string) => {
|
||||
};
|
||||
|
||||
// Stock management functions
|
||||
export const fetchStockData = async (authToken: string) => {
|
||||
export const fetchStockData = async (url: string, authToken: string) => {
|
||||
try {
|
||||
return await fetchData('/api/stock', {
|
||||
return await fetchData(url, {
|
||||
headers: { Authorization: `Bearer ${authToken}` },
|
||||
credentials: "include",
|
||||
});
|
||||
@@ -92,7 +91,8 @@ export const updateProductStock = async (
|
||||
authToken: string
|
||||
) => {
|
||||
try {
|
||||
return await fetchData(`/api/stock/${productId}`, {
|
||||
const url = `${process.env.NEXT_PUBLIC_API_URL}/stock/${productId}`;
|
||||
return await fetchData(url, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
|
||||
Reference in New Issue
Block a user