-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaproveproduct.js
50 lines (44 loc) · 1.84 KB
/
aproveproduct.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
let listProductHTML = document.querySelector('.listProduct');
let listCartHTML = document.querySelector('.listCart');
let iconCart = document.querySelector('.icon-cart');
let iconCartSpan = document.querySelector('.icon-cart span');
let body = document.querySelector('body');
let closeCart = document.querySelector('.close');
let butDetalhes = document.querySelector('.Details');
let products = JSON.parse(localStorage.getItem("pedidos_produto"));
let cart = [];
$("document").ready(function(){
if (JSON.parse(localStorage.getItem("produtos"))==null){
localStorage.setItem("produtos", JSON.stringify([]))
}
})
const addDataToHTML = () => {
// remove datas default from HTML
// add new datas
if (products.length > 0) // if has data
{
products.forEach(product => {
let newProduct = document.createElement('div');
newProduct.dataset.id = product.id;
newProduct.classList.add('item');
newProduct.innerHTML =
`<img src="${product.image}" alt="" style="height: 220px; max-width: 240px; object-fit: cover; background-position: center center; background-repeat: no-repeat; background-size: cover;">
<h2>${product.name}</h2>
<button class="Details">Detalhes</button>`;
listProductHTML.appendChild(newProduct);
});
}
}
listProductHTML.addEventListener('click', (event) => {
let positionClick = event.target;
if (positionClick.classList.contains('addCart')) {
let id_product = positionClick.parentElement.dataset.id;
addToCart(id_product);
}
else if (positionClick.classList.contains('Details')) {
let id_detalhes = positionClick.parentElement.dataset.id;
localStorage.setItem("produto", id_detalhes);
window.location.href = "newprodutosDetails.html";
}
})
addDataToHTML()