-
Notifications
You must be signed in to change notification settings - Fork 0
/
addToCartAllProducts.js
61 lines (43 loc) · 2.13 KB
/
addToCartAllProducts.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
import { fetchProduct } from './fetchProduct.js';
import {assignCardData } from './assignCardData.js';
import { removeData } from './removeData.js';
const addProductBtn=document.getElementById('addCart');
const cart =document.getElementById('cart');
const img=document.querySelector('.cart .image');
const singleContent=document.querySelector('.cart .img-single-content .singleContent');
const closeIcon=document.createElement('i');
closeIcon.classList.add('fas', 'fa-times');
closeIcon.classList.add('close-icon');
closeIcon.style.cursor = 'pointer';
addProductBtn.addEventListener('click', async() => {
if (window.location.pathname.includes('singleProduct.html')) {
// Extract product ID from the query string
const urlParams = new URLSearchParams(window.location.search);
const productId = urlParams.get('id');
if (productId) {
// Display the single product that was clicked.
const product = await fetchProduct(productId);
if (product) {
assignCardData(product);
cart.style.display="flex";
img.classList.add('show');
closeIcon.style.color='white';
closeIcon.fontSize='19px';
cart.appendChild(closeIcon);
closeIcon.addEventListener('click',() => {
const cart=document.querySelector('.cart');
cart.style.display='none'
})
singleContent.innerHTML+=`<div class="containerOfTrash">
<i class="fas fa-trash"></i>
</div>`;
const trashIcon = document.querySelector('.fas.fa-trash');
trashIcon.addEventListener('click', () => {
removeData(productId);
})
}
} else {
console.error('Product ID not found in the URL');
}
}
});