-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (84 loc) · 3.35 KB
/
script.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
let cartCount = 0;
let cartItemsData = []; // Array untuk menyimpan data produk
const cartLink = document.getElementById('cart-link');
const cartCountSpan = document.getElementById('cart-count');
const cartDropdown = document.querySelector('.cart-dropdown');
const addToCartButtons = document.querySelectorAll('.add-to-cart');
const cartItems = document.getElementById('cart-items');
const checkoutButton = document.getElementById('checkout-button');
const clearCartButton = document.getElementById('clear-cart-button'); // Clear cart button
// Function to load cart from localStorage
function loadCart() {
const storedCartCount = localStorage.getItem('cartCount');
const storedCartItems = localStorage.getItem('cartItems');
cartCount = storedCartCount ? parseInt(storedCartCount) : 0;
cartItemsData = storedCartItems ? JSON.parse(storedCartItems) : [];
updateCart();
}
// Function to save cart to localStorage
function saveCart() {
localStorage.setItem('cartItems', JSON.stringify(cartItemsData)); // Simpan ke 'cartItems'
localStorage.setItem('cartCount', cartCount); // Menyimpan jumlah item
}
// Logout button action
document.getElementById('logout-button').addEventListener('click', function() {
window.location.href = 'index.html'; // Redirect to login page
});
// Function to update cart count and display items
function updateCart() {
cartCountSpan.textContent = cartCount;
if (cartCount > 0) {
cartItems.textContent = `Anda memiliki ${cartCount} item di keranjang`;
checkoutButton.disabled = false;
} else {
cartItems.textContent = 'Keranjang kosong';
checkoutButton.disabled = true; // Disable checkout if cart is empty
}
}
// Add items to cart
addToCartButtons.forEach(button => {
button.addEventListener('click', () => {
const productCard = button.closest('.product-card');
const productName = productCard.querySelector('h3').textContent;
const productPrice = productCard.querySelector('.price').textContent;
const productImage = productCard.querySelector('img').src;
// Create product object
const product = {
name: productName,
price: productPrice,
image: productImage,
};
// Add to cart array and save
cartItemsData.push(product);
cartCount++;
saveCart();
updateCart();
});
});
// Handle checkout button
checkoutButton.addEventListener('click', () => {
if (cartCount > 0) {
window.location.href = 'checkout.html'; // Redirect to checkout page
} else {
alert('Keranjang Anda kosong. Tambahkan item terlebih dahulu.');
}
});
// Handle clear cart button
clearCartButton.addEventListener('click', () => {
cartCount = 0;
cartItemsData = [];
saveCart();
updateCart();
});
// Dropdown hover logic with animation
function toggleDropdown(show) {
cartDropdown.style.display = show ? 'block' : 'none';
cartDropdown.style.opacity = show ? '1' : '0';
cartDropdown.style.transition = 'opacity 0.3s';
}
cartLink.addEventListener('mouseenter', () => toggleDropdown(true));
cartLink.addEventListener('mouseleave', () => toggleDropdown(false));
cartDropdown.addEventListener('mouseenter', () => toggleDropdown(true));
cartDropdown.addEventListener('mouseleave', () => toggleDropdown(false));
// Load initial cart state from localStorage
loadCart();