From 117a2c0317eab12e7026714f1344fedd2563b489 Mon Sep 17 00:00:00 2001
From: Maxime Princelle <maxime@princelle.org>
Date: Fri, 17 Dec 2021 11:36:16 +0100
Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20improve=20stock=20display=20of?=
 =?UTF-8?q?=20prices?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/pages/stock.tsx | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/pages/stock.tsx b/src/pages/stock.tsx
index 68238ff..36ec83f 100644
--- a/src/pages/stock.tsx
+++ b/src/pages/stock.tsx
@@ -26,14 +26,14 @@ const Stock = () => {
     });
   }, []);
 
-  let updateProductQuantity = (productId, quantity) => {
+  const updateProductQuantity = (productId, quantity) => {
     let productsTemp = productsList;
     let product = productsTemp[productId];
     product.stock = quantity;
     setProductsList([...productsTemp]);
   };
 
-  let toggleEditMode = (productId) => {
+  const toggleEditMode = (productId) => {
     let productsTemp = productsList;
     let product = productsTemp[productId];
     product.edit = product.edit === true ? false : true;
@@ -45,6 +45,17 @@ const Stock = () => {
     }
   };
 
+  const getPrice = (product: Product):string => {
+    let price = `${product.price.toFixed(2).replace(".", ",")} €`;
+    if (product.subscriberPrice !== undefined) {
+      price += ` / ${product.subscriberPrice.toFixed(2).replace(".", ",")} €`;
+    } else {
+      // Display the same price if there is not subscription price
+      price += ` / ${product.price.toFixed(2).replace(".", ",")} €`;
+    }
+    return price;
+  };
+
   return (
     <div className="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
       {rawProductList.length > 0 &&
@@ -121,7 +132,7 @@ const Stock = () => {
                   <tr key={product.name} className={productIdx % 2 === 0 ? 'bg-white' : 'bg-gray-50'}>
                     <td className="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{product.name}</td>
                     <td className="px-4 py-4 whitespace-nowrap text-sm text-gray-500">{product.category}</td>
-                    <td className="px-4 py-4 whitespace-nowrap text-sm text-gray-500">{product.price.toFixed(2).replace(".", ",")}€ / {product?.subscriberPrice?.toFixed(2).replace(".", ",") ?? product.price.toFixed(2).replace(".", ",")} €</td>
+                    <td className="px-4 py-4 whitespace-nowrap text-sm text-gray-500">{getPrice(product)}</td>
                     <td className="px-4 py-4 whitespace-nowrap text-sm text-gray-500">
                       {product.edit ?
                         <input
-- 
GitLab