-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
110 lines (83 loc) · 3.27 KB
/
cart.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let cartDiv = document.querySelector("#all_products");
let cartSelect = document.querySelector("#select");
let totalPrice = document.querySelector("#total_price");
let cartBuy = document.querySelector("#cart_buy");
drawSelect();
async function drawSelect() {
let cart = JSON.parse(localStorage.getItem("cartCategories")) || [];
let filteredCategories = [];
for(let item of cart){
filteredCategories.push(item.category);
}
let unique = [... new Set(filteredCategories)]
cartSelect.innerHTML += `<option value="">Выберите категорию:</option>`
for(let i = 0; i < unique.length; i++){
cartSelect.innerHTML += `<option value="${unique[i]}">${unique[i]}</option>`
}
console.log(filteredCategories)
};
drawCartCategories();
async function drawCartCategories(category){
let cart = JSON.parse(localStorage.getItem("cartProductIds")) || [];
let response = await fetch("https://dummyjson.com/products?limit=100");
let data = await response.json();
// console.log(data.products);
let totalSum = 0;
let cartArray = [];
for(let item of cart){
cartArray.push(data.products.filter(product => product.id == item.productId)[0]);
}
for(let products of cartArray){
if(category){
if(category == products.category){
cartDiv.innerHTML += `
<div class="products">
<div class ="image" style="background-image: url(${products.images[0]});"></div>
<span class="title">${products.title}</span><hr>
<span class="price">${products.price}$</span><br><br>
<span class="title">Category: </span>${products.category}
</div>
`
}
}
totalSum += products.price
}
};
const sendEmail = (email, subject, message, price) => {
const templateParams = {
message: message,
subject: subject,
to_email: email,
price: price,
};
emailjs.send('service_aozbob6', 'template_42ducdk', templateParams)
.then(function(response) {
console.log('SUCCESS!', response.status, response.text);
}, function(error) {
console.log('FAILED...', error);
});
}
cartSelect.addEventListener("change", () => {
drawCartCategories(cartSelect.value)
cartDiv.innerHTML = "";
})
cartBuy.addEventListener("click", async () => {
let cart = JSON.parse(localStorage.getItem("cartProductIds")) || [];
let response = await fetch("https://dummyjson.com/products?limit=100");
let data = await response.json();
let cartArray = [];
let title = []
let totalSum = 0;
for(let item of cart){
cartArray.push(data.products.filter(product => product.id == item.productId)[0]);
}
for(let item of cartArray){
title.push(item.title)
totalSum += item.price
}
console.log(title)
let currentUserEmail = localStorage.getItem("currentUserEmail");
console.log(totalSum)
sendEmail(currentUserEmail,"Поздравляем с покупкой",`Ваши покупки: ${title}
Общая сумма: ${totalSum}$`)
})