forked from Taker-Academy/KeDuFron-t
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panier.js
86 lines (78 loc) · 3.3 KB
/
panier.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
document.addEventListener('DOMContentLoaded', fetchAndDisplayCartItems);
function fetchAndDisplayCartItems() {
const productDetails = document.getElementById('productDetails');
productDetails.innerHTML = '';
const quantities = JSON.parse(localStorage.getItem('itemQuantities')) || {};
let hasItems = false;
Object.keys(quantities).forEach(itemId => {
if (quantities[itemId] > 0) {
hasItems = true;
fetch(`https://api.kedufront.juniortaker.com/item/${itemId}`)
.then(response => response.json())
.then(data => {
const item = data.item;
productDetails.innerHTML += `
<div class="cart_item" id="item-${itemId}">
<img src="https://api.kedufront.juniortaker.com/item/picture/${itemId}" alt="Image de ${item.name}" class="item_img_panier">
<h2 class="title_item_panier">${item.name}</h2>
<p class="item_size_police">Prix: ${item.price}€</p>
<div class="quantity_button">
<button class="button_moin" onclick="adjustQuantity('${itemId}', -1)">-</button>
<span class="quantity" id="quantity-${itemId}">${quantities[itemId]}</span>
<button class="button_plus" onclick="adjustQuantity('${itemId}', 1)">+</button>
</div>
</div>
`;
})
.catch(error => {
console.error(`Erreur lors du chargement du produit ${itemId}: ${error.message}`);
});
}
});
cleanUpQuantities();
if (hasItems) {
appendCheckoutButton();
} else {
removeCheckoutButton();
}
}
function adjustQuantity(itemId, change) {
let quantities = JSON.parse(localStorage.getItem('itemQuantities')) || {};
let currentQuantity = quantities[itemId] || 0;
let newQuantity = Math.max(currentQuantity + change, 0);
if (newQuantity > 0) {
quantities[itemId] = newQuantity;
} else {
delete quantities[itemId];
}
localStorage.setItem('itemQuantities', JSON.stringify(quantities));
fetchAndDisplayCartItems();
}
function cleanUpQuantities() {
let quantities = JSON.parse(localStorage.getItem('itemQuantities')) || {};
Object.keys(quantities).forEach(itemId => {
if (quantities[itemId] <= 0) {
delete quantities[itemId];
}
});
localStorage.setItem('itemQuantities', JSON.stringify(quantities));
}
function appendCheckoutButton() {
let checkoutButton = document.getElementById('checkoutButton');
if (!checkoutButton) {
const tabItem2 = document.querySelector('.tab_item2');
checkoutButton = document.createElement('button');
checkoutButton.id = 'checkoutButton';
checkoutButton.textContent = 'Valider ma commande';
checkoutButton.addEventListener('click', function() {
window.location.href = 'commande.html';
});
tabItem2.appendChild(checkoutButton);
}
}
function removeCheckoutButton() {
const checkoutButton = document.getElementById('checkoutButton');
if (checkoutButton) {
checkoutButton.remove();
}
}