Advertisement
Btwonu

Ski Shop

Oct 22nd, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   const addForm = document.getElementById('add-new');
  3.   const availableSect = document.getElementById('products');
  4.   const cartSect = document.getElementById('myProducts');
  5.   const filterDiv = document.querySelector('.filter');
  6.   const filterInput = document.getElementById('filter');
  7.  
  8.   // get totalPrice field
  9.   const totalPriceHead = document.querySelectorAll('h1')[1];
  10.  
  11.   // get new product fields
  12.   const [nameInput, quantityInput, priceInput] = Array.from(
  13.     addForm.querySelectorAll('input')
  14.   );
  15.  
  16.   // totalPrice
  17.   let totalPrice = 0;
  18.  
  19.   // get addBtn
  20.   const addBtn = addForm.querySelector('button');
  21.   // get cartBtn
  22.   const buyBtn = cartSect.querySelector('button');
  23.   // get filterBtn
  24.   const filterBtn = filterDiv.children[2];
  25.  
  26.   // listen for clicks on addBtn
  27.   addBtn.addEventListener('click', addProduct);
  28.  
  29.   // listen for clicks on buyBtn
  30.   buyBtn.addEventListener('click', buyProducts);
  31.  
  32.   // listen for clicks on filterBtn
  33.   filterBtn.addEventListener('click', filterFunctionality);
  34.  
  35.   // Helper functions
  36.   const createElement = (ele, text, cl) => {
  37.     let element = document.createElement(ele);
  38.  
  39.     if (text) {
  40.       element.textContent = text;
  41.  
  42.       if (!isNaN(parseFloat(text))) {
  43.         element.textContent = parseFloat(text).toFixed(2);
  44.       }
  45.     }
  46.  
  47.     if (cl) {
  48.       element.classList.add(cl);
  49.     }
  50.  
  51.     return element;
  52.   };
  53.  
  54.   // Declarations
  55.   function addProduct(e) {
  56.     e.preventDefault();
  57.     // the first child of the section is the ul
  58.     const availableList = availableSect.children[1];
  59.  
  60.     // create li with data from fields
  61.     let productLi = document.createElement('li');
  62.     let nameSpan = createElement('span', nameInput.value);
  63.     let quantityStrong = createElement(
  64.       'strong',
  65.       `Available: ${quantityInput.value}`
  66.     );
  67.  
  68.     let priceStrong = createElement('strong', priceInput.value);
  69.     let div = document.createElement('div');
  70.     let cartBtn = createElement('button', "Add to Client's List");
  71.  
  72.     // clear inputs
  73.     nameInput.value = '';
  74.     quantityInput.value = '';
  75.     priceInput.value = '';
  76.  
  77.     // create product's list item
  78.     div.appendChild(priceStrong);
  79.     div.appendChild(cartBtn);
  80.  
  81.     productLi.appendChild(nameSpan);
  82.     productLi.appendChild(quantityStrong);
  83.     productLi.appendChild(div);
  84.  
  85.     // append the product to the availableList
  86.     availableList.appendChild(productLi);
  87.  
  88.     // listen for clicks on add to client's list
  89.     cartBtn.addEventListener('click', addToCart);
  90.   }
  91.  
  92.   function addToCart(e) {
  93.     const currentProduct = e.target.parentElement.parentElement;
  94.     // the first child of the section is the ul
  95.     const cartList = cartSect.children[1];
  96.  
  97.     // get current available products
  98.     const availableStrong = currentProduct.children[1];
  99.     let availableItems = Number(availableStrong.textContent.split(' ')[1]);
  100.  
  101.     // create new li based on currentProduct
  102.     const productLi = document.createElement('li');
  103.     // get name span of current product
  104.     let name = currentProduct.children[0].textContent;
  105.     // get price strong of current product
  106.     let priceText = currentProduct.children[2].children[0].textContent;
  107.     let productData = `${name}<strong>${priceText}</strong>`;
  108.  
  109.     // for each item increase the totalPrice and decrease available products
  110.     availableItems--;
  111.  
  112.     // if product availability < 1 => remove it from the ul
  113.     if (availableItems < 1) {
  114.       currentProduct.remove();
  115.     }
  116.  
  117.     // add text data to li innerHTML
  118.     productLi.innerHTML = productData;
  119.  
  120.     // add the li to myProducts section ul
  121.     cartList.appendChild(productLi);
  122.  
  123.     // set the new available value
  124.     availableStrong.textContent = `Available: ${availableItems}`;
  125.  
  126.     totalPrice += Number(priceText);
  127.     totalPriceHead.textContent = `Total Price: ${totalPrice.toFixed(2)}`;
  128.   }
  129.  
  130.   function buyProducts(e) {
  131.     // when bought, remove all items from cart and reset the totalPrice
  132.     const allProducts = cartSect.children[1];
  133.     allProducts.textContent = '';
  134.     totalPrice = 0;
  135.     totalPriceHead.textContent = `Total Price: ${totalPrice.toFixed(2)}`;
  136.   }
  137.  
  138.   function filterFunctionality() {
  139.     // the first child of the section is the ul
  140.     const availableList = availableSect.children[1];
  141.     let keyword = filterInput.value.toLowerCase();
  142.     if (!keyword) return;
  143.  
  144.     // clear input
  145.     filterInput.value = '';
  146.  
  147.     // get all products
  148.     productsArr = Array.from(availableList.children);
  149.  
  150.     productsArr.forEach((productLi) => {
  151.       let productName = productLi.children[0].textContent.toLowerCase();
  152.  
  153.       if (productName.match(keyword) != null) {
  154.         productLi.style.display = 'block';
  155.       } else {
  156.         productLi.style.display = 'none';
  157.       }
  158.     });
  159.   }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement