-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (53 loc) · 1.56 KB
/
script.js
File metadata and controls
59 lines (53 loc) · 1.56 KB
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
const $items = document.querySelector('.items');
const $btns = document.querySelector('.btns');
const $logo = document.querySelector('.logo');
const displayItems = (items) => {
$items.innerHTML = '';
const elements = [];
items.forEach((item) => {
const { brand, name, color, image } = item;
const $item = document.createElement('li');
$item.dataset.brand = `${brand}`;
$item.dataset.color = `${color}`;
$item.className = `item show ${color.toLowerCase()}`;
$item.innerHTML = `
<span class="item_thumbnail">
<img src="${image}" alt="${brand}" />
</span>
<span class="item_description"> ${brand} ,${name} ,${color} Lipstick</span>
`;
elements.push($item);
});
return elements;
};
const updateItems = (items, key, value) => {
items.forEach((item) => {
if (item.dataset[key] === value) {
item.classList.add('show');
} else {
item.classList.remove('show');
}
});
};
const onButtonClick = (event, items) => {
const { key, value } = event.target.dataset;
if (key == null || value == null) return;
updateItems(items, key, value);
};
const getItems = async () => {
const response = await fetch('./data/data.json');
const data = await response.json();
return data.items;
};
const init = async () => {
try {
const items = await getItems();
const elements = displayItems(items);
$items.append(...elements);
$btns.addEventListener('click', (event) => onButtonClick(event, elements));
$logo.addEventListener('click', () => init());
} catch {
console.log;
}
};
init();