generated from kxxg1/scooby-doos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
134 lines (114 loc) · 3.26 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
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
// Product objects
const products = [
{
id: 1,
name: "Cat Tree",
price: 100,
image: "images/cat_tree.webp",
},
{
id: 2,
name: "Cat Food",
price: 1.5,
image: "images/cat_food.webp",
},
{
id: 3,
name: "Dog Lead",
price: 10,
image: "images/dog_lead.webp",
},
{
id: 4,
name: "Pet Bed",
price: 30,
image: "images/cat_bed.webp",
},
{
id: 5,
name: "Veggiedent",
price: 12,
image: "images/dog_treat.webp",
},
];
// Cart array to store selected products
let cart = JSON.parse(localStorage.getItem("cart")) || [];
console.log(cart);
// Function to dynamically render product cards
function renderProducts() {
const productContainer = document.querySelector(".product-container");
// Check if the productContainer exists before proceeding
if (productContainer) {
console.log("Product cards rendered successfully.");
} else {
console.error("Product container not found.");
return;
}
// Clear existing content in the product container
productContainer.innerHTML = "";
// Render product cards
products.forEach((product) => {
const cardItems = document.createElement("div");
cardItems.classList.add("card");
cardItems.innerHTML = `
<img src="${product.image}" alt="${product.name}" />
<div class="product-row">
<h3>${product.name}</h3>
<p>$${product.price}</p>
</div>
<button class="product-button" onclick="addToCart(${product.id})">Add to Cart</button>
`;
productContainer.appendChild(cardItems);
});
}
function addToCart(productId) {
// Find the selected product based on productId using a for loop
let selectedProduct;
for (let i = 0; i < products.length; i++) {
// Check if the current product's id matches the given productId
if (products[i].id === productId) {
selectedProduct = products[i];
break;
}
}
// Find if item exists in cart
const existingItemIndex = cart.findIndex((item) => item.id === productId);
if (existingItemIndex !== -1) {
cart[existingItemIndex].quantity++;
} else {
cart.push({
id: productId,
name: selectedProduct.name,
price: selectedProduct.price,
quantity: 1,
image: selectedProduct.image
});
}
// Save to localStorage and update display
localStorage.setItem("cart", JSON.stringify(cart));
updateCartUI();
renderProducts();
// Log information to the console
console.log("Added to Cart:", {
id: productId,
name: selectedProduct.name,
price: selectedProduct.price,
quantity: 1,
image: selectedProduct.image
});
}
// Function to update the cart UI
function updateCartUI() {
const total = cart.reduce((sum, item) => sum + item.price * item.quantity, 0);
console.log("Cart Total:", total);
// Update the cart UI as needed
}
document.addEventListener("DOMContentLoaded", renderProducts);
const time = () => {
let currentTime = new Date()
// the toLocaleTimeString formats the time nicely, into 00:00:00 style, can edit later on for if we don't want the seconds.
let localTime = currentTime.toLocaleTimeString('en-AU')
//then pin to doc, assuming we just have a section or something that makes us a clock
document.getElementById('clock').innerHTML = localTime
}
setInterval(time, 1000)