datnguyentien204 commited on
Commit
3f7b01f
·
verified ·
1 Parent(s): 9376308

Upload main.js

Browse files
Files changed (1) hide show
  1. static/web2/assets/js/main.js +63 -31
static/web2/assets/js/main.js CHANGED
@@ -52,7 +52,7 @@ if(loginButton){
52
  loginContent.classList.add('show-login')
53
  })
54
  }
55
-
56
  /*===== LOGIN HIDDEN =====*/
57
  /* Validate if constant exists */
58
  if(loginClose){
@@ -112,87 +112,92 @@ let swiperFeatured = new Swiper('.featured__swiper', {
112
  })
113
 
114
  /*=============== FEATURED ADD TO CART ===============*/
 
 
115
  document.addEventListener('DOMContentLoaded', function() {
116
  const addToCartButtons = document.querySelectorAll('.featured__card .button');
117
  const clearCartButton = document.getElementById('clearCart');
118
  const checkoutButton = document.getElementById('checkOut');
119
 
120
  addToCartButtons.forEach(button => {
121
- button.addEventListener('click', addToCartHandler);
122
  });
123
 
124
- clearCartButton.addEventListener('click', clearCartHandler);
125
- checkoutButton.addEventListener('click', checkoutHandler);
126
 
127
  updateCartUI();
128
  });
129
 
 
130
  function addToCartHandler(event) {
131
  const featuredCard = event.target.closest('.featured__card');
132
 
133
- const productName = featuredCard.querySelector('.featured__title').innerText;
134
- const productPrice = parseFloat(featuredCard.querySelector('.featured__discount').dataset.price) || 0;
135
 
136
- alert(`Đã thêm vào giỏ hàng: ${productName}`);
137
 
138
- addToCart(productName, productPrice);
139
 
140
  updateCartUI();
141
  }
142
 
 
143
  function addToCart(productName, productPrice) {
144
- let cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];
145
 
146
- cartItems.push({ name: productName, price: productPrice });
147
 
148
- localStorage.setItem('cartItems', JSON.stringify(cartItems));
149
  }
150
 
 
151
  function updateCartUI() {
152
- const cartItemsContainer = document.getElementById('cartItemsContainer');
153
- const cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];
154
 
155
- cartItemsContainer.innerHTML = '';
156
 
157
  cartItems.forEach(item => {
158
- const cartItemElement = document.createElement('div');
159
- cartItemElement.innerText = `${item.name} - ${item.price} VND`;
160
- cartItemsContainer.appendChild(cartItemElement);
161
  });
162
 
163
- const totalAmount = calculateTotalAmount(cartItems);
164
- displayTotalAmount(totalAmount);
165
  }
166
 
167
  function calculateTotalAmount(cartItems) {
168
- return cartItems.reduce((total, item) => total + item.price, 0);
169
  }
170
 
171
  function displayTotalAmount(amount) {
172
- let totalAmountContainer = document.getElementById('totalAmountContainer');
173
 
174
  if (!totalAmountContainer) {
175
- totalAmountContainer = document.createElement('div');
176
- totalAmountContainer.id = 'totalAmountContainer';
177
- const bagForm = document.querySelector('.bag__form');
178
- bagForm.appendChild(totalAmountContainer);
179
  }
180
 
181
- totalAmountContainer.innerText = `Tổng cộng: ${amount} VND`;
182
  }
183
 
184
  function clearCartHandler() {
185
- localStorage.removeItem('cartItems');
186
 
187
  updateCartUI();
188
  }
189
 
190
  function checkoutHandler() {
191
- alert('Đã thanh toán thành công!');
192
 
193
- resetCart();
194
 
195
- updateCartUI();
196
  }
197
 
198
  function resetCart() {
@@ -308,8 +313,35 @@ sr.reveal(`.discount__images`, {origin: 'right'})
308
  /*=============== OPEN DETAIL TAB ===============*/
309
  function openDetailPage() {
310
  // Đường dẫn tới trang detail.html
311
- var detailPageURL = "detail.html";
312
 
313
  // Mở tab mới
314
  window.open(detailPageURL, '_blank');
315
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  loginContent.classList.add('show-login')
53
  })
54
  }
55
+
56
  /*===== LOGIN HIDDEN =====*/
57
  /* Validate if constant exists */
58
  if(loginClose){
 
112
  })
113
 
114
  /*=============== FEATURED ADD TO CART ===============*/
115
+
116
+ //Định nghĩa các hàm xử lý thêm sản phẩm vào giỏ hàng, xóa giỏ hàng, thanh toán và cập nhật giao diện giỏ hàng
117
  document.addEventListener('DOMContentLoaded', function() {
118
  const addToCartButtons = document.querySelectorAll('.featured__card .button');
119
  const clearCartButton = document.getElementById('clearCart');
120
  const checkoutButton = document.getElementById('checkOut');
121
 
122
  addToCartButtons.forEach(button => {
123
+ button.addEventListener('click', addToCartHandler); // Thêm sự kiện click cho từng nút thêm vào giỏ hàng
124
  });
125
 
126
+ clearCartButton.addEventListener('click', clearCartHandler); // Thêm sự kiện click cho nút xóa giỏ hàng
127
+ checkoutButton.addEventListener('click', checkoutHandler); // Thêm sự kiện click cho nút thanh toán
128
 
129
  updateCartUI();
130
  });
131
 
132
+ //Hàm xử lý thêm sản phẩm vào giỏ hàng
133
  function addToCartHandler(event) {
134
  const featuredCard = event.target.closest('.featured__card');
135
 
136
+ const productName = featuredCard.querySelector('.featured__title').innerText; // Lấy tên sản phẩm
137
+ const productPrice = parseFloat(featuredCard.querySelector('.featured__discount').dataset.price) || 0; // Lấy giá sản phẩm
138
 
139
+ alert(`Add to cart successfully: ${productName}`); // Thông báo thêm sản phẩm vào giỏ hàng thành công
140
 
141
+ addToCart(productName, productPrice); // Thêm sản phẩm vào giỏ hàng
142
 
143
  updateCartUI();
144
  }
145
 
146
+ //Hàm thêm sản phẩm vào giỏ hàng
147
  function addToCart(productName, productPrice) {
148
+ let cartItems = JSON.parse(localStorage.getItem('cartItems')) || []; // Lấy danh sách sản phẩm trong giỏ hàng từ localStorage
149
 
150
+ cartItems.push({ name: productName, price: productPrice }); // Thêm sản phẩm vào danh sách sản phẩm trong giỏ hàng
151
 
152
+ localStorage.setItem('cartItems', JSON.stringify(cartItems)); // Lưu danh sách sản phẩm trong giỏ hàng vào localStorage
153
  }
154
 
155
+ //Hàm xử lý xóa giỏ hàng
156
  function updateCartUI() {
157
+ const cartItemsContainer = document.getElementById('cartItemsContainer'); // Lấy ra container chứa danh sách sản phẩm trong giỏ hàng
158
+ const cartItems = JSON.parse(localStorage.getItem('cartItems')) || []; // Lấy danh sách sản phẩm trong giỏ hàng từ localStorage
159
 
160
+ cartItemsContainer.innerHTML = ''; // Xóa danh sách sản phẩm trong giỏ hàng
161
 
162
  cartItems.forEach(item => {
163
+ const cartItemElement = document.createElement('div'); // Tạo một thẻ div chứa thông tin sản phẩm
164
+ cartItemElement.innerText = `${item.name} - ${item.price} VND`; // Hiển thị thông tin sản phẩm
165
+ cartItemsContainer.appendChild(cartItemElement); // Thêm sản phẩm vào danh sách sản phẩm trong giỏ hàng
166
  });
167
 
168
+ const totalAmount = calculateTotalAmount(cartItems); // Tính tổng số tiền cần thanh toán
169
+ displayTotalAmount(totalAmount); // Hiển thị tổng số tiền cần thanh toán
170
  }
171
 
172
  function calculateTotalAmount(cartItems) {
173
+ return cartItems.reduce((total, item) => total + item.price, 0); // Tính tổng số tiền cần thanh toán
174
  }
175
 
176
  function displayTotalAmount(amount) {
177
+ let totalAmountContainer = document.getElementById('totalAmountContainer'); // Lấy ra container chứa tổng số tiền cần thanh toán
178
 
179
  if (!totalAmountContainer) {
180
+ totalAmountContainer = document.createElement('div'); // Tạo một thẻ div chứa tổng số tiền cần thanh toán
181
+ totalAmountContainer.id = 'totalAmountContainer'; // Đặt id cho thẻ div
182
+ const bagForm = document.querySelector('.bag__form'); // Lấy ra form giỏ hàng
183
+ bagForm.appendChild(totalAmountContainer); // Thêm thẻ div vào form giỏ hàng
184
  }
185
 
186
+ totalAmountContainer.innerText = `Total: ${amount} VND`; // Hiển thị tổng số tiền cần thanh toán
187
  }
188
 
189
  function clearCartHandler() {
190
+ localStorage.removeItem('cartItems'); // Xóa danh sách sản phẩm trong giỏ hàng
191
 
192
  updateCartUI();
193
  }
194
 
195
  function checkoutHandler() {
196
+ alert('Payment Successfully!');
197
 
198
+ resetCart(); // Xóa danh sách sản phẩm trong giỏ hàng
199
 
200
+ updateCartUI(); // Cập nhật giao diện giỏ hàng
201
  }
202
 
203
  function resetCart() {
 
313
  /*=============== OPEN DETAIL TAB ===============*/
314
  function openDetailPage() {
315
  // Đường dẫn tới trang detail.html
316
+ var detailPageURL = "static/web2/detail.html";
317
 
318
  // Mở tab mới
319
  window.open(detailPageURL, '_blank');
320
  }
321
+
322
+
323
+ /*=============== CHECK LOGIN INFORMATION ===============*/
324
+ document.addEventListener('DOMContentLoaded', function() {
325
+ const loginForm = document.getElementById('loginForm');
326
+
327
+ loginForm.addEventListener('submit', function(event) {
328
+ event.preventDefault(); // Ngăn form không tải lại trang
329
+
330
+ const email = document.getElementById('login-email').value;
331
+ const password = document.getElementById('login-pass').value;
332
+
333
+ fetch('/login', {
334
+ method: 'POST',
335
+ headers: {
336
+ 'Content-Type': 'application/json'
337
+ },
338
+ body: JSON.stringify({ email: email, password: password })
339
+ })
340
+ .then(response => response.json())
341
+ .then(data => {
342
+ const loginMessage = document.getElementById('loginMessage');
343
+ alert(data.message); // Show alert message
344
+ })
345
+ .catch(error => console.error('Error:', error));
346
+ });
347
+ });