-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
260 lines (236 loc) · 7.83 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
let bookList = [],
basketList = [];
const toggleButton = document.getElementById('toggleButton');
const body = document.body;
toggleButton.addEventListener('click', () => {
body.classList.toggle('light-mode');
body.classList.toggle('dark-mode');
});
// toggle menu olusturma
function toggleModal() {
const basketModal = document.querySelector(".basket_modal");
console.log(basketModal);
basketModal.classList.toggle("active");
}
function getBooks() {
fetch("./products.json")
.then((res) => res.json())
.then((books) => (bookList = books))
.catch((err) => console.log(err));
}
getBooks();
//
const createBookStars = (starRate) => {
let starRateHtml = "";
for (let i = 1; i <= 5; i++) {
if (Math.round(starRate) >= i) {
starRateHtml += ` <i class="bi bi-star-fill active"></i>`;
} else {
starRateHtml += ` <i class="bi bi-star-fill"></i>`;
}
}
return starRateHtml;
};
//${index % 2 == 0 && "offset-2"} aynı zamanda ${index % 2 == 0 ? "offset-2" : ""} olarakda yazılır
const createBookItemsHtml = () => {
const bookListEl = document.querySelector(".book_list");
let bookListHtml = "";
bookList.forEach((book, index) => {
bookListHtml += `
<div class="col-5 ${index % 2 == 0 && "offset-2"} my-5">
<div class="row book_card">
<div class="col-6">
<img
src="${book.imgSource}"
alt=""
class="img-fluid shadow"
width="258px"
height="400px"
/>
</div>
<div class="col-6 d-flex flex-column justify-content-center gap-4">
<div class="book_detail">
<span class="fos gray fs-5">${book.author}</span><br />
<span class="fs-4 fw-bold">${book.name}</span><br />
<span class="book_star_rate">
${createBookStars(book.starRate)}
<span class="gray">1938 reviews</span>
</span>
<p class="book_desc fos gray">
${book.description}
</p>
<div>
<span class="black fw-bold fs-5 me-2">${book.price} TL</span>
<span class="fs-5 fw-bold old_price">${
book.oldPrice
? `<span class="fs-5 fw-bold old_price">${book.oldPrice} TL</span>
`
: ""
}</span>
</div>
<button class="btn_purple" onClick="addBookToBasket(${
book.id
})">Sepete Ekle</button>
</div>
</div>
</div>
</div>
`;
});
bookListEl.innerHTML = bookListHtml;
};
const BOOK_TYPES = {
ALL: "Tümü",
NOVEL: "Roman",
CHILDREN: "Çocuk",
HISTORY: "Tarih",
FINANCE: "Finans",
SCIENCE: "Bilim",
SELFIMPROVEMENT: "Kişisel Gelişim",
};
const createBookTypesHtml = () => {
const filterEle = document.querySelector(".filter");
let filterHtml = "";
// filtre türlerini tutacak dizi all türüyle başlatılmıştr
let filterTypes = ["ALL"];
bookList.forEach((book) => {
// !eğer filtre türleri dizisinde bu tür bulunmuyorsa ekleme işlemi yapar
if (filterTypes.findIndex((filter) => filter == book.type) == -1) {
filterTypes.push(book.type);
}
});
filterTypes.forEach((type, index) => {
filterHtml += ` <li onClick="filterBooks(this)" data-types="${type}" class="${
index == 0 ? "active" : null
}">${BOOK_TYPES[type] || type}</li>`;
});
filterEle.innerHTML = filterHtml;
};
const filterBooks = (filterEl) => {
document.querySelector(".filter .active").classList.remove("active");
filterEl.classList.add("active");
let bookType = filterEl.dataset.types;
console.log(bookType);
getBooks();
if (bookType != "ALL") {
bookList = bookList.filter((book) => book.type == bookType);
}
createBookItemsHtml();
};
const listBasketItems = () => {
const basketListEl = document.querySelector(".basket_list");
const basketCountEl = document.querySelector(".basket_count");
console.log(basketList);
const totalQuantity = basketList.reduce(
(total, item) => total + item.quantity,
0
);
basketCountEl.innerHTML = totalQuantity > 0 ? totalQuantity : null;
const totalPriceEl = document.querySelector(".total_price");
console.log(totalPriceEl);
let basketListHtml = "";
let totalPrice = 0;
basketList.forEach((item) => {
console.log(item);
totalPrice += item.product.price * item.quantity;
basketListHtml += `
<li class="basket_item">
<img
src="${item.product.imgSource}"
alt=""
width="100"
height="100"
/>
<div class="basket_item-info">
<h3 class="book_name">${item.product.name}</h3>
<span class="book_price">${item.product.price}tl</span> <br />
<span class="book_remove" onClick="removeItemBasket(${item.product.id})">Sepetten Kaldır</span>
</div>
<div class="book_count">
<span class="decrease" onClick="decreaseItemToBasket(${item.product.id})">-</span>
<span class="mx-2">${item.quantity}</span>
<span class="increase" onClick="increaseItemToBasket(${item.product.id})">+</span>
</div>
</li>
`;
});
basketListEl.innerHTML = basketListHtml
? basketListHtml
: `<li class="basket_item">Sepette herhangi bir ürün bulunmuyor.Sepete ürün ekleyiniz.</li>`;
totalPriceEl.innerHTML = totalPrice > 0 ? "Total:" + totalPrice + "tl" : null;
};
// !sepete ürün ekleme
const addBookToBasket = (bookId) => {
let findedBook = bookList.find((book) => book.id == bookId);
// console.log(findedBook);
if (findedBook) {
// !sepetteki ürünün zaten var olup olmadığını kontrol et
const basketAlreadyIndex = basketList.findIndex(
(basket) => basket.product.id == bookId
);
// ! eğer sepet boşsa ya daa eklenen kitap sepette yoksa
if (basketAlreadyIndex == -1) {
let addItem = { quantity: 1, product: findedBook };
basketList.push(addItem);
} else {
// sepette zaten var olan bir kitap ekleniyorsa miktarını artıtır
basketList[basketAlreadyIndex].quantity += 1;
}
}
const btnCheck = document.querySelector(".btnCheck");
btnCheck.style.display = "block";
// ! s epet içeriğini güncelleme ve görünteleme
listBasketItems();
};
//! sepetten ürünü kaldırır
const removeItemBasket = (bookId) => {
const findedIndex = basketList.findIndex(
(basket) => basket.product.id == bookId
);
// !eğer kitap sepet içinde bulunuyorsa
if (findedIndex != -1) {
// splice belirli sayıda eleman silmek için kullanıluyor
// sepet listesinden kitabı çıkar
basketList.splice(findedIndex, 1);
}
const btnCheck = document.querySelector(".btnCheck");
btnCheck.style.display = "none";
// sepet içeriğini günceller
listBasketItems();
};
// sepetteki ürünün miktarını azaltır
const decreaseItemToBasket = (bookId) => {
const findedIndex = basketList.findIndex(
(basket) => basket.product.id == bookId
);
// eğer kitap sepet içinde bulunuyorsa
if (findedIndex != -1) {
// eğer kitabın miktarı 1den büyükse
if (basketList[findedIndex].quantity != 1) {
basketList[findedIndex].quantity -= 1;
} else {
removeItemBasket(bookId);
}
listBasketItems();
}
// sepet içeriğini güncelleme
listBasketItems();
};
// sepetteki miktarı arttırırma
const increaseItemToBasket = (bookId) => {
const findedIndex = basketList.findIndex(
(basket) => basket.product.id == bookId
);
// eğer kitap sepet içinde bulunuyorsa
if (findedIndex != -1) {
// kitabın miktarını bir arttır
basketList[findedIndex].quantity += 1;
}
// sepet içeriğini güncelle
listBasketItems();
};
// !! datanın gelmesini bekle
setTimeout(() => {
createBookItemsHtml();
createBookTypesHtml();
}, 100);