]> git repositories - simple-cash-register.git/commitdiff
added functionality on updating ui for product lists and product indicator
authorbochard <mail@tenkyuu.dev>
Sat, 1 Mar 2025 08:43:08 +0000 (16:43 +0800)
committerbochard <mail@tenkyuu.dev>
Sat, 1 Mar 2025 08:43:08 +0000 (16:43 +0800)
index.html
index.js

index 7b47696a4e4a5b944ecd2e5b95f140c93d743dd1..a858e2d86debda472d330ee8de277f2b6ae11cf3 100644 (file)
@@ -4,7 +4,7 @@
     <meta charset="utf-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Digital Cash Register</title>
-    <link rel="stylesheet" href="./styles.css">
+    <link rel="stylesheet" href="./index.css">
   </head>
   <body>
     <div class="customer-input-ctn">
@@ -19,9 +19,9 @@
         <div id="change-due"></div>
       </div>
 
-      <div class="product-list">
+      <div id="product-list" class="product-list">
         <div class="product-wrapper">
-          <p class="product-name">Water <span id="price-tag">$1.00</span></p>
+          <p class="product-name">Water Bottle <span class="price-tag">$1.00</span></p>
           <div class="quantity-controls">
             <button class="minus-btn">-</button>
             <p class="product-quantity">0</p>
@@ -29,7 +29,7 @@
           </div>
         </div>
         <div class="product-wrapper">
-          <p class="product-name">Bread <span id="price-tag">$2.50</span></p>
+          <p class="product-name">Bread <span class="price-tag">$2.50</span></p>
           <div class="quantity-controls">
             <button class="minus-btn">-</button>
             <p class="product-quantity">0</p>
@@ -41,6 +41,6 @@
       <div id="change-drawer"></div>
     </div>
 
-    <script src="./script.js"></script>
+    <script src="./index.js"></script>
   </body>
 </html>
\ No newline at end of file
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..ebeb5c1148846a0e8fb860865a1f129458da4415 100644 (file)
--- a/index.js
+++ b/index.js
@@ -0,0 +1,93 @@
+const cash = document.getElementById('cash');
+const purchaseBtn = document.getElementById('purchase-btn');
+const totalPrice = document.getElementById('total-price');
+const changeDue = document.getElementById('change-due');
+const productList = document.getElementById('product-list');
+const changeDrawer = document.getElementById('change-drawer');
+
+let price = 1.87;
+let cart = {};
+let cid = [
+  ['PENNY', 1.01],
+  ['NICKEL', 2.05],
+  ['DIME', 3.1],
+  ['QUARTER', 4.25],
+  ['ONE', 90],
+  ['FIVE', 55],
+  ['TEN', 20],
+  ['TWENTY', 60],
+  ['ONE HUNDRED', 100]
+];
+let productsPrice = {
+  'water bottle': 1.00,
+  'bread': 2.50
+}
+
+// ===== UPDATE THE TOTAL ON PRICE INDICATOR =====
+function updatePriceIndicator(price) {
+  totalPrice.textContent = `$${price}`;
+}
+
+// ===== REMOVE INPUT =====
+
+
+// ===== CALCULATE THE TOTAL OF PRODUCTS ADDED =====
+function calculateCartItems() {
+  let totalPriceOnCart = 0;
+
+  Object.entries(cart).forEach(([product, price]) => {
+    const numberOfItem = price;
+    totalPriceOnCart += numberOfItem * productsPrice[product];
+  })
+  console.log(`total price on cart: $${totalPriceOnCart}`);
+  updatePriceIndicator(totalPriceOnCart.toFixed(2));
+}
+
+// ===== WHEN PLUS BTN OR MINUS BTN IS CLICKED =====
+function plusOrMinusBtnIsClicked(event) {
+  const plusBtn = event.target.classList.contains('plus-btn');
+  const minusBtn = event.target.classList.contains('minus-btn');
+
+  if (plusBtn) { // plus btn is clicked
+    const quantity = event.target.previousElementSibling;
+    let quantityInt = parseInt(quantity.textContent);
+    const productName = event.target.parentElement.previousElementSibling.childNodes[0].textContent.trim().toLowerCase();
+
+    quantityInt += 1;
+    quantity.textContent = quantityInt;
+
+    if (!cart[productName]) {
+      cart[productName] = 1;
+    } else {
+      cart[productName] += 1;
+    }
+  }
+  
+  if (minusBtn) { // minus btn is clicked
+    const quantity = event.target.nextElementSibling;
+    let quantityInt = parseInt(quantity.textContent);
+    const productName = event.target.parentElement.previousElementSibling.childNodes[0].textContent.trim().toLowerCase();
+
+    if (quantityInt > 0) {
+      quantityInt -= 1;
+      quantity.textContent = quantityInt;
+    }
+
+    if (!cart[productName] || cart[productName] === 0) {
+      delete cart[productName];
+    } else {
+      cart[productName] -= 1;
+    }
+  }
+  console.log(cart);
+}
+
+// ===== LISTEN FOR UPDATE QUANTITY =====
+productList.addEventListener('click', (e) => {
+  plusOrMinusBtnIsClicked(e);
+  calculateCartItems();
+})
+
+// ===== LISTEN FOR PURCHASE CLICK =====
+purchaseBtn.addEventListener('click', () => {
+})
\ No newline at end of file