-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
195 lines (158 loc) · 6.37 KB
/
script.js
File metadata and controls
195 lines (158 loc) · 6.37 KB
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
const container = document.querySelector("#container");
const sortPriceSelect = document.querySelector("#sort-price");
const BASE_URL = "https://v2.api.noroff.dev/rainy-days";
let products = [];
async function fetchProducts() {
try {
const response = await fetch(BASE_URL);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
products = data.data;
renderProducts(products);
updateCartCount();
} catch (error) {
console.error("Error fetching products:", error);
if (container) {
container.innerHTML = `<p>Failed to load products. Please try again later.</p>`;
}
}
}
function renderProducts(productList) {
if (!container) return;
container.innerHTML = "";
productList.forEach(product => {
const card = document.createElement("div");
const image = document.createElement("img");
const imageLink = document.createElement("a");
const content = document.createElement("div");
const title = document.createElement("h2");
const price = document.createElement("p");
const button = document.createElement("button");
card.className = "card";
image.className = "card-image";
content.className = "card-content";
title.className = "card-title";
price.className = "card-price";
button.className = "add-to-cart-btn";
image.src = product.image?.url || "placeholder.jpg";
image.alt = product.image?.alt || "Product image";
imageLink.href = `jacket1.html?id=${product.id}`;
imageLink.appendChild(image);
title.textContent = product.title || "No title available";
price.textContent = product.price ? `$${product.price}` : "Price not available";
button.textContent = "Add to Cart";
button.addEventListener("click", () => {
let cart = JSON.parse(localStorage.getItem("cart")) || [];
cart.push(product);
localStorage.setItem("cart", JSON.stringify(cart));
alert(`${product.title} has been added to your cart!`);
updateCartCount();
});
content.appendChild(title);
content.appendChild(price);
content.appendChild(button);
card.appendChild(imageLink);
card.appendChild(content);
container.appendChild(card);
});
}
// ------------------ SORTING ------------------
if (sortPriceSelect) {
sortPriceSelect.addEventListener("change", () => {
let sortedProducts = [...products];
if (sortPriceSelect.value === "low-high") {
sortedProducts.sort((a, b) => a.price - b.price);
} else if (sortPriceSelect.value === "high-low") {
sortedProducts.sort((a, b) => b.price - a.price);
}
renderProducts(sortedProducts);
});
}
// ------------------ UPDATE CART COUNT ------------------
function updateCartCount() {
const cart = JSON.parse(localStorage.getItem("cart")) || [];
const countElement = document.getElementById("cart-count");
if (countElement) {
countElement.textContent = cart.length;
}
}
// ------------------ REMOVE FROM CART ------------------
function removeFromCart(index) {
let cart = JSON.parse(localStorage.getItem("cart")) || [];
cart.splice(index, 1);
localStorage.setItem("cart", JSON.stringify(cart));
updateCartCount();
displayCartItems();
}
// ------------------ DISPLAY CART ITEMS ------------------
function displayCartItems() {
const cartPageContainer = document.querySelector(".cart");
if (!cartPageContainer) return;
const cartItems = JSON.parse(localStorage.getItem("cart")) || [];
cartPageContainer.innerHTML = "<h1>YOUR SHOPPING CART</h1><hr>";
if (cartItems.length === 0) {
cartPageContainer.innerHTML += "<p>Your cart is empty.</p>";
return;
}
const itemList = document.createElement("div");
itemList.className = "cart-items";
let total = 0;
cartItems.forEach((item, index) => {
const itemElement = document.createElement("div");
itemElement.className = "cart-item";
const img = document.createElement("img");
img.src = item.image?.url || "placeholder.jpg";
img.alt = item.image?.alt || "Product image";
img.className = "cart-item-image";
const itemTitle = document.createElement("h3");
itemTitle.textContent = item.title;
const itemPrice = document.createElement("p");
itemPrice.textContent = `$${item.price}`;
const removeBtn = document.createElement("button");
removeBtn.textContent = "REMOVE";
removeBtn.className = "remove-btn";
removeBtn.addEventListener("click", () => {
removeFromCart(index);
});
itemElement.appendChild(img);
itemElement.appendChild(itemTitle);
itemElement.appendChild(itemPrice);
itemElement.appendChild(removeBtn);
itemList.appendChild(itemElement);
total += item.price;
});
cartPageContainer.appendChild(itemList);
const totalDisplay = document.createElement("p");
totalDisplay.className = "cart-total";
totalDisplay.innerHTML = `<strong>Total:</strong> $${total.toFixed(2)}`;
cartPageContainer.appendChild(totalDisplay);
if (!window.location.href.includes("checkout.html")) {
const checkoutLink = document.createElement("a");
checkoutLink.href = "checkout.html";
checkoutLink.className = "checkout-btn";
checkoutLink.textContent = "CHECK OUT";
cartPageContainer.appendChild(checkoutLink);
}
}
// ------------------ FORM + PAGE LOAD HANDLING ------------------
document.addEventListener("DOMContentLoaded", () => {
const checkoutForm = document.querySelector("form");
if (checkoutForm) {
checkoutForm.addEventListener("submit", (event) => {
const confirmPurchase = confirm("Are you sure you want to complete this purchase?");
window.location.href = "confirmed.html";
if (!confirmPurchase) {
event.preventDefault();
alert("Purchase cancelled.");
} else {
localStorage.removeItem("cart");
alert("Thank you for your purchase!");
}
});
}
fetchProducts();
updateCartCount();
displayCartItems();
});