From 62e2602d281b30a2329d3ef01dafe0914af66e5c Mon Sep 17 00:00:00 2001
From: NotII <46204250+NotII@users.noreply.github.com>
Date: Sun, 16 Feb 2025 16:30:57 +0000
Subject: [PATCH] :D
---
app/dashboard/products/page.tsx | 1 -
components/forms/pricing-tiers.tsx | 146 +++++++++++++++++++---------
components/modals/product-modal.tsx | 12 +--
components/tables/product-table.tsx | 10 +-
components/ui/dialog.tsx | 4 +-
5 files changed, 116 insertions(+), 57 deletions(-)
diff --git a/app/dashboard/products/page.tsx b/app/dashboard/products/page.tsx
index 7ef0d69..2823be1 100644
--- a/app/dashboard/products/page.tsx
+++ b/app/dashboard/products/page.tsx
@@ -105,7 +105,6 @@ export default function ProductsPage() {
};
- // Save product data after modal form submission
const handleSaveProduct = async (data: Product, file?: File | null) => {
console.log("handleSaveProduct:", data, file);
diff --git a/components/forms/pricing-tiers.tsx b/components/forms/pricing-tiers.tsx
index b7d4d83..eb01ce4 100644
--- a/components/forms/pricing-tiers.tsx
+++ b/components/forms/pricing-tiers.tsx
@@ -19,52 +19,106 @@ export const PricingTiers = ({
handleTierChange,
handleRemoveTier,
handleAddTier,
-}: PricingTiersProps) => (
-
-
Tiered Pricing
+}: PricingTiersProps) => {
+ const formatNumber = (num: number) => {
+ // For price per unit, show up to 6 decimal places if needed
+ return Number(num.toFixed(6)).toString();
+ };
- {pricing?.length > 0 ? (
- pricing.map((tier, index) => (
-
- handleTierChange(e, index)}
- className="h-8 text-sm px-2 flex-1"
- />
+ const formatTotal = (num: number) => {
+ // For total price, always show 2 decimal places
+ return num.toFixed(2);
+ };
- handleTierChange(e, index)}
- className="h-8 text-sm px-2 flex-1"
- />
-
-
- ))
- ) : (
-
No pricing tiers added.
- )}
+ const calculateTotal = (quantity: number, pricePerUnit: number) => {
+ return formatTotal(quantity * pricePerUnit);
+ };
-
-
-);
+ const handleTotalChange = (
+ e: React.ChangeEvent,
+ index: number,
+ minQuantity: number
+ ) => {
+ const totalPrice = Number(e.target.value);
+ const pricePerUnit = minQuantity > 0 ? totalPrice / minQuantity : 0;
+
+ const syntheticEvent = {
+ target: {
+ name: 'pricePerUnit',
+ value: formatNumber(pricePerUnit)
+ }
+ } as React.ChangeEvent;
+
+ handleTierChange(syntheticEvent, index);
+ };
+
+ return (
+
+
Tiered Pricing
+
+ {pricing?.length > 0 ? (
+ <>
+
+
Quantity
+
Price Per Unit
+
Total Price
+
+
+
+ {pricing.map((tier, index) => (
+
+ handleTierChange(e, index)}
+ className="h-8 text-sm px-2"
+ />
+
+ handleTierChange(e, index)}
+ className="h-8 text-sm px-2"
+ />
+
+ handleTotalChange(e, index, tier.minQuantity)}
+ className="h-8 text-sm px-2"
+ />
+
+
+
+ ))}
+ >
+ ) : (
+
No pricing tiers added.
+ )}
+
+
+
+ );
+};
diff --git a/components/modals/product-modal.tsx b/components/modals/product-modal.tsx
index 7fceb55..e6c500e 100644
--- a/components/modals/product-modal.tsx
+++ b/components/modals/product-modal.tsx
@@ -87,10 +87,10 @@ export const ProductModal: React.FC = ({
const handleAddTier = () => {
setProductData((prev) => ({
...prev,
- pricing: [...prev.pricing, { minQuantity: 0, pricePerUnit: 0 }],
+ pricing: [...prev.pricing, { minQuantity: 0, pricePerUnit: 0 }],
}));
};
-
+
const handleSave = async () => {
if (!productData.category) {
toast.error("Please select or add a category");
@@ -114,7 +114,7 @@ export const ProductModal: React.FC = ({
...prev,
pricing: prev.pricing.map((tier, i) =>
i === index
- ? { ...tier, [name]: value === "" ? 0 : Number(value) }
+ ? { ...tier, [name]: value === "" ? 0 : Number(value) }
: tier
),
}));
@@ -122,7 +122,7 @@ export const ProductModal: React.FC = ({
return (