-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
137 lines (119 loc) · 4.26 KB
/
script.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const olCartItems = document.querySelector('.cart__items');
const buttonEmptyCard = document.querySelector('.empty-cart');
const sectionCart = document.querySelector('section.cart');
let storage = [];
let prices = [];
const createProductImageElement = (imageSource) => {
const img = document.createElement('img');
img.className = 'item__image';
img.src = imageSource;
return img;
};
const createCustomElement = (element, className, innerText) => {
const e = document.createElement(element);
e.className = className;
e.innerText = innerText;
return e;
};
const createProductItemElement = ({ sku, name, image }) => {
const section = document.createElement('section');
section.className = 'item';
section.appendChild(createCustomElement('span', 'item__sku', sku));
section.appendChild(createCustomElement('span', 'item__title', name));
section.appendChild(createProductImageElement(image));
section.appendChild(createCustomElement('button', 'item__add', 'Adicionar ao carrinho!'));
return section;
};
const appendProduto = async (product) => {
const resultadoFetch = await fetchProducts(product);
const produtos = resultadoFetch.results;
const sectionItens = document.querySelector('.items');
return produtos.forEach((produto) => {
const itemElement = { sku: produto.id, name: produto.title, image: produto.thumbnail };
sectionItens.appendChild(createProductItemElement(itemElement));
});
};
const chamaPrice = async (itemID) => {
const resultadoFetch = await fetchItem(itemID);
return resultadoFetch.price;
};
const alteraValorPrice = () => {
const price = document.querySelector('.total-price');
let priceTotal = 0;
if (prices.length !== 0) {
priceTotal = prices.reduce((acc, number) => acc + number);
}
price.innerText = `${priceTotal}`;
};
const getSkuFromProductItem = (item) => item.querySelector('span.item__sku').innerText;
const cartItemClickListener = async (event) => {
const itemCard = event.target;
itemCard.remove();
const idItemCard = itemCard.innerText.substr(5, 13);
storage = storage.filter((item) => item !== idItemCard);
saveCartItems(JSON.stringify(storage));
const price = await chamaPrice(idItemCard);
prices = prices.filter((preco) => preco !== price);
await alteraValorPrice();
};
const createCartItemElement = ({ sku, name, salePrice, image }) => {
const li = document.createElement('li');
const div = document.createElement('div');
li.className = 'cart__item';
li.innerText = `SKU: ${sku} | NAME: ${name} | PRICE: $${salePrice}`;
li.addEventListener('click', cartItemClickListener);
div.className = 'image__cart__item';
div.appendChild(createProductImageElement(image));
li.appendChild(div);
return li;
};
const appendCartItem = async (itemID) => {
const retorno = await fetchItem(itemID);
const { id: sku, title: name, price: salePrice, thumbnail: image } = retorno;
const objeto = { sku, name, salePrice, image };
olCartItems.appendChild(createCartItemElement(objeto));
prices.push(Number(retorno.price));
await alteraValorPrice();
};
const addEventListenerButton = () => {
const button = document.querySelectorAll('.item__add');
button.forEach((botao) => {
botao.addEventListener('click', (event) => {
const item = event.target.parentNode;
const sku = getSkuFromProductItem(item);
storage.push(sku);
saveCartItems(JSON.stringify(storage));
appendCartItem(sku);
});
});
};
buttonEmptyCard.addEventListener('click', () => {
const itens = document.querySelectorAll('.cart__item');
itens.forEach((item) => item.remove());
localStorage.clear();
storage = [];
prices = [];
alteraValorPrice();
});
const carregaLocalStorage = () => {
if (localStorage.length > 0) {
const local = JSON.parse(localStorage.getItem('cartItems'));
local.forEach((item) => appendCartItem(item));
}
};
const criaElementPrice = () => {
const h1 = document.createElement('h1');
const span = document.createElement('span');
span.className = 'total-price';
h1.className = 'h1-price';
h1.innerText = 'Preço Total: R$ ';
span.innerText = '0';
h1.appendChild(span);
sectionCart.appendChild(h1);
};
window.onload = async () => {
await appendProduto('computador');
await addEventListenerButton();
await carregaLocalStorage();
await criaElementPrice();
};