-
Notifications
You must be signed in to change notification settings - Fork 14
/
detail.js
54 lines (48 loc) · 1.82 KB
/
detail.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
import products from '/products.js';
import cart from './cart.js';
let listProduct = document.getElementById('listProduct');
let app = document.getElementById('app');
let temporaryContent = document.getElementById('temporaryContent');
const loadTemplate = () => {
fetch('/template.html')
.then(response => response.text())
.then(html => {
app.innerHTML = html;
let contentTab = document.getElementById('contentTab');
contentTab.innerHTML = temporaryContent.innerHTML;
temporaryContent.innerHTML = null;
cart();
initApp();
})
}
loadTemplate();
const initApp = () => {
let productId = new URLSearchParams(window.location.search).get('id');
let thisProduct = products.filter(value => value.id == productId)[0];
if(!thisProduct){
window.location.href = "/";
}
let detail = document.querySelector('.detail');
detail.querySelector('.image img').src = thisProduct.image;
detail.querySelector('.name').innerText = thisProduct.name;
detail.querySelector('.price').innerText = '$' + thisProduct.price;
detail.querySelector('.description').innerText = '$' + thisProduct.description;
detail.querySelector('.addCart').dataset.id = thisProduct.id;
let listProductHTML = document.querySelector('.listProduct');
products.forEach(product => {
let newProduct = document.createElement('div');
newProduct.classList.add('item');
newProduct.innerHTML =
`<a href="/detail.html?id=${product.id}">
<img src="${product.image}">
</a>
<h2>${product.name}</h2>
<div class="price">$${product.price}</div>
<button
class="addCart"
data-id='${product.id}'>
Add To Cart
</button>`;
listProductHTML.appendChild(newProduct);
});
}