-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcart.js.backup
105 lines (83 loc) · 3.2 KB
/
cart.js.backup
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
console.log('connexion javascript');
let increaseButton = document.getElementById("increase");
function increaseQuantity(productId){
console.log(productId);
}
function reduceQuantity(productId){
console.log(productId);
}
//for increase quantity of product in cart
let allQuantity=document.querySelectorAll('.quantity');
for (const btnQ of allQuantity){
console.log(btnQ);
let productQuantityId = btnQ.dataset.productQuantityId;
let productQuantity = btnQ.dataset.productQuantity;
console.log(productQuantityId);
console.log(productQuantity);
}
let totalDisplay = document.getElementById("total");
let allIncrease=document.querySelectorAll('.increase');
async function increaseProduct(userId, productId){
let response = await fetch(`cart/increase/${userId}/${productId}`, {method: 'PATCH'});
let responseData = await response.json();
console.log(responseData);
if(responseData.success == true){
// console.log(productId); PB productId et USERid sont inversés
// console.log(userId);
let productQuantityId = `product${userId}`;
console.log("productQuantityId "+productQuantityId);
let productQuantityDisplay=document.getElementById(productQuantityId);
let numb = productQuantityDisplay.innerText;
numb = parseInt(numb);
numb=numb+1;
productQuantityDisplay.innerText = numb;
let total = responseData.total;
totalDisplay.innerText = total/100;
console.log(total);
}
}
for (const btn of allIncrease){
console.log(btn);
console.log(allIncrease);
btn.addEventListener("click", (e) =>{
let idProduct = e.target.dataset.productId;
let idUser = e.target.dataset.userId;
console.log("id product "+idProduct)
increaseProduct(idProduct, idUser);
})
}
//---------------------------------------------------
//for reduce quantity of product in cart
let allReduce=document.querySelectorAll('.reduce');
async function reduceProduct(userId, productId){
let response = await fetch(`cart/reduce/${userId}/${productId}`, {method: 'PATCH'});
let responseData = await response.json();
console.log(responseData);
if(responseData.success == true){
// console.log(productId); PB productId et USERid sont inversés
// console.log(userId);
let productQuantityId = `product${userId}`;
console.log("productQuantityId "+productQuantityId);
let productQuantityDisplay=document.getElementById(productQuantityId);
let numb = productQuantityDisplay.innerText;
numb = parseInt(numb);
numb=numb-1;
productQuantityDisplay.innerText = numb;
let total = responseData.total;
total = parseFloat(total);
console.log(total);
let newTotal=total/100;
console.log("new total type "+typeof newTotal);
totalDisplay.innerText = newTotal;
}
}
for (const btn of allReduce){
console.log(btn);
console.log(allReduce);
btn.addEventListener("click", (e) =>{
let idProduct = e.target.dataset.productId;
let idUser = e.target.dataset.userId;
console.log("id product "+idProduct)
reduceProduct(idProduct, idUser);
})
}