Updates
This commit is contained in:
13
app/_document.tsx
Normal file
13
app/_document.tsx
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Html, Head, Main, NextScript } from "next/document";
|
||||
|
||||
export default function Document() {
|
||||
return (
|
||||
<Html lang="en">
|
||||
<Head />
|
||||
<body>
|
||||
<Main />
|
||||
<NextScript />
|
||||
</body>
|
||||
</Html>
|
||||
);
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import { Plus } from "lucide-react";
|
||||
import {
|
||||
fetchProductData,
|
||||
saveProductData,
|
||||
saveProductImage,
|
||||
deleteProductData,
|
||||
} from "@/lib/productData";
|
||||
import { ProductModal } from "@/components/modals/product-modal";
|
||||
@@ -105,8 +106,11 @@ export default function ProductsPage() {
|
||||
setProductData({ ...productData, pricing: updatedPricing });
|
||||
};
|
||||
|
||||
|
||||
// Save product data after modal form submission
|
||||
const handleSaveProduct = async (data: Product) => {
|
||||
const handleSaveProduct = async (data: Product, file?: File | null) => {
|
||||
console.log("handleSaveProduct:", data, file);
|
||||
|
||||
const adjustedPricing = data.pricing.map((tier) => ({
|
||||
minQuantity: tier.minQuantity,
|
||||
pricePerUnit:
|
||||
@@ -114,26 +118,30 @@ export default function ProductsPage() {
|
||||
? parseFloat(tier.pricePerUnit)
|
||||
: tier.pricePerUnit,
|
||||
}));
|
||||
|
||||
|
||||
const productToSave: Product = {
|
||||
...data,
|
||||
pricing: adjustedPricing,
|
||||
image: data.image ?? "", // ✅ Prevents undefined error
|
||||
};
|
||||
|
||||
|
||||
try {
|
||||
const authToken = document.cookie.split("Authorization=")[1];
|
||||
const apiUrl = editing
|
||||
? `${process.env.NEXT_PUBLIC_API_URL}/products/${data._id}`
|
||||
: `${process.env.NEXT_PUBLIC_API_URL}/products`;
|
||||
|
||||
|
||||
const savedProduct = await saveProductData(
|
||||
apiUrl,
|
||||
productToSave,
|
||||
authToken,
|
||||
editing ? "PUT" : "POST"
|
||||
);
|
||||
|
||||
|
||||
if (file) {
|
||||
await saveProductImage(`${process.env.NEXT_PUBLIC_API_URL}/products/${savedProduct._id}/image`, file, authToken);
|
||||
}
|
||||
|
||||
// Update state with the saved product
|
||||
setProducts((prevProducts) => {
|
||||
if (editing) {
|
||||
return prevProducts.map((product) =>
|
||||
@@ -143,7 +151,7 @@ export default function ProductsPage() {
|
||||
return [...prevProducts, savedProduct];
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
setModalOpen(false);
|
||||
} catch (error) {
|
||||
console.error("Error saving product:", error);
|
||||
@@ -177,7 +185,7 @@ export default function ProductsPage() {
|
||||
minQuantity: tier.minQuantity,
|
||||
pricePerUnit: tier.pricePerUnit,
|
||||
}))
|
||||
: [{ minQuantity: 1, pricePerUnit: 0 }], // Fallback if undefined
|
||||
: [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
});
|
||||
setEditing(true);
|
||||
setModalOpen(true);
|
||||
|
||||
13
app/error.tsx
Normal file
13
app/error.tsx
Normal file
@@ -0,0 +1,13 @@
|
||||
"use client";
|
||||
|
||||
export default function Error({ error, reset }: { error: Error; reset: () => void }) {
|
||||
return (
|
||||
<div className="h-screen flex flex-col items-center justify-center">
|
||||
<h1 className="text-2xl font-semibold text-red-500">Something went wrong!</h1>
|
||||
<p className="text-gray-500">{error.message}</p>
|
||||
<button className="mt-4 px-4 py-2 bg-blue-600 text-white rounded" onClick={() => reset()}>
|
||||
Try Again
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -2,5 +2,4 @@ import { redirect } from "next/navigation"
|
||||
|
||||
export default function Home() {
|
||||
redirect("/dashboard")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user