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