From ab85db58e33bffb86597f76672ae8f93724ab169 Mon Sep 17 00:00:00 2001 From: Lescano713 <131562199+Lescano713@users.noreply.github.com> Date: Mon, 2 Sep 2024 01:28:26 -0500 Subject: [PATCH] restore function --- script.js | 99 ++++++++++++++++++++----------------------------------- 1 file changed, 36 insertions(+), 63 deletions(-) diff --git a/script.js b/script.js index a695db3..cbd3afe 100644 --- a/script.js +++ b/script.js @@ -34,8 +34,7 @@ function generateId(data){ function uploadProducts(products){ - // - + products.forEach(product => { // console.log(product); const div = document.createElement('div'); @@ -63,17 +62,16 @@ function uploadProducts(products){ }); } +//function to add and remove products + function takeOffProducts(id){ const product = findId(id); product.quantity -= 1; - uploadingCart(productsInCart); - } -function addAmount(id, p){ +function addAmount(id){ findId(id).quantity += 1; - p.textContent = findId(id).quantity; } @@ -159,36 +157,7 @@ function findId(id){ return existingProduct } -function buttonAddRemove(button,id,img){ - button.innerHTML = ""; - button.classList.add('buttonAdd'); - button.type = 'button'; - button.setAttribute('aria-label', `button ${id}`) - let amount; - // amount = findId(id).quantity || 0; - amount = !findId(id) ? 0 : findId(id).quantity; - - const iconPlus = document.createElement('img'); - iconPlus.src = icons.incrementQuantity; - const iconMinus = document.createElement('img'); - iconMinus.src = icons.decrementQuantity; - - const p = document.createElement('p'); - p.textContent = amount; - - - iconPlus.addEventListener('click', () =>{ - addAmount(id,p) - }); - - iconMinus.addEventListener('click', e =>{ - takeOffProducts(id, p); - targetProduct(id,img) - }); - button.append(iconMinus,p,iconPlus); - -} function removeProduct(id){ const index = productsInCart.findIndex( p => p.id === id) @@ -237,7 +206,7 @@ function addToArray(product){ }else if(product.quantity < 1){ removeProduct(product.id) } - if (productsInCart.length <= 0) { + if (productsInCart.length === 0) { restore() }else{ uploadingCart(productsInCart) @@ -254,24 +223,20 @@ function sumAmount(product){ function buttonInitial(container, product, img){ const button = document.createElement('button'); - button.classList.remove('buttonAdd') - const icon = document.createElement('img'); - icon.src = icons.addToCart; - button.appendChild(icon); - button.innerHTML += " Add to cart"; + button.innerHTML = ""; + button.classList.add('add-to-cart') + button.innerHTML = ` +
Add to cart
` button.addEventListener('click',e =>{ addToArray(product); - buttonAddRemove(button, product.id,img); + buttonAmount(button,product.id,img) targetProduct(product.id,img) + // console.log(e.target); }); container.append(button); } -function showButton(id){ - if(!findId(id) || findId(id).quantity < 1 ){ - // buttonInitial - } -} + function targetProduct(id,img){ if (!findId(id) || findId(id).quantity < 1) { @@ -289,32 +254,40 @@ function restore(){Your added items will apper here
` } -// function dependingButton(product){ -// if(!findId(product.id)) -// } - -function buttonAmount(){ +function buttonAmount(button,id,img){ + button.innerHTML = ""; const div = document.createElement('div'); - div.innerHTML = ` -