-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
389 lines (327 loc) · 13.2 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import './styles/style.scss';
//-- Körs när hela dokumentet laddat.--//
document.addEventListener('DOMContentLoaded', () => {
printSprayCans();
initializeCart();
addCartEventListener();
startInactivityTimer();
//-- Event listener för formulärsubmittet--//
const orderForm = document.querySelector('form');
if (orderForm) {
orderForm.addEventListener('submit', handleFormSubmit);
//--Event listeners för alla formulärfält--//
const formFields = orderForm.querySelectorAll('input[required], select[required], textarea[required]');
formFields.forEach(field => {
field.addEventListener('input', checkFormValidity);
});
//--Kontroll av initial formulärstatus--//
checkFormValidity();
}
});
//-- Väljare för huvudcontainrar --//
const sprayCansContainer = document.querySelector('#sprayCansForSale');
const cartContainer = document.querySelector('#cartSection');
//Visar pop-up-meddelande
function showPopup(message) {
const popup = document.createElement('div');
popup.className = 'popup';
popup.textContent = message;
document.body.appendChild(popup);
setTimeout(() => {
popup.remove();
}, 3000); // Meddelandet försvinner efter 3 sekunder
}
// Funktion för att hantera formulärsubmitt
function handleFormSubmit(event) {
event.preventDefault(); //--Stoppa standardformulärsubmitt
showPopup('Thanks for you order!!');
resetForm();
}
// --Formulärfältens giltighet--//
function checkFormValidity() {
const orderForm = document.querySelector('form');
const submitButton = document.querySelector('#sendForm');
const isValid = orderForm.checkValidity();
submitButton.disabled = !isValid;
}
//--Hårdkodat sprayburkar/produkter--//
const sprayCans = [
{ id: 1, name: 'CanCan in Sunset', price: 100, image: {src:'assets/pictures/orange.jpg', alt: 'Spray Can 1 Image'}, amount: 0, stars: 4, category: 'light' },
{ id: 2, name: 'Yellow Mr. Sunshine', price: 120, image: {src: 'assets/pictures/yellow.jpg', alt: 'Spray Can 2 Image'}, amount: 0, stars: 3.5, category: 'light' },
{ id: 3, name: 'Red Rush ', price: 140, image: {src: 'assets/pictures/red.jpg', alt: 'Spray Can 3 Image' }, amount: 0, stars: 5, category: 'light' },
{ id: 4, name: 'Pink that Party', price: 110, image: { src: 'assets/pictures/pink.jpg', alt: 'Spray Can 4 Image' }, amount: 0, stars: 4.5, category: 'pastel' },
{ id: 5, name: 'Bloom Fusia, bloom', price: 110, image: { src: 'assets/pictures/fusia.jpg', alt: 'Spray Can 5 Image' }, amount: 0, stars: 3, category: 'light' },
{ id: 6, name: 'Cast a spell', price: 100, image: { src: 'assets/pictures/chrome.jpg', alt: 'Spray Can 6 Image' }, amount: 0, stars: 4, category: 'light' },
{ id: 7, name: 'Blue Lagoon', price: 140, image: { src: 'assets/pictures/blue.jpg', alt: 'Spray Can 7 Image' }, amount: 0, stars: 4.5, category: 'light' },
{ id: 8, name: 'Blue Velvet', price: 130, image: { src: 'assets/pictures/darkblue.jpg', alt: 'Spray Can 8 Image' }, amount: 0, stars: 3.5, category: 'dark' },
{ id: 9, name: 'Greener than grass', price: 140, image: { src: 'assets/pictures/green.jpg', alt: 'Spray Can 9 Image' }, amount: 0, stars: 4, category: 'dark' },
{ id: 10, name: 'Green Moon', price: 160, image: { src: 'assets/pictures/lightgreen.jpg', alt: 'Spray Can 10 Image' }, amount: 0, stars: 4, category: 'pastel' },
];
//--------------------------------------------------------------------//
/* ---------------------------LOCAL STORAGE-------------------------- */
//--------------------------------------------------------------------//
//-- Hämtar varukorgen från localstorage--//
function getCartFromStorage() {
const cart = localStorage.getItem('cart');
return cart ? JSON.parse(cart) : [];
}
//--Spara till localstorage--//
function saveCartToStorage(cart) {
localStorage.setItem('cart', JSON.stringify(cart));
}
//--Lägg till en händelselyssnare för att spara varukorgen varje gång den uppdateras--//
function addCartEventListener() {
window.addEventListener('beforeunload', () => {
saveCartToStorage(sprayCans);
});
}
//-- Initialiserar varukorgen--//
function initializeCart() {
const savedCart = getCartFromStorage();
if (savedCart.length > 0) {
sprayCans.forEach(can => {
const savedItem = savedCart.find(item => item.id === can.id);
if (savedItem) {
can.amount = savedItem.amount;
}
});
}
}
//--------------------------------------------------------------------//
/* ---------------------------SORTERING------------------------------ */
//--------------------------------------------------------------------//
//--Sortera produkter efter betyg--//
function sortByRating() {
sprayCans.sort((a, b) => b.stars - a.stars);
printSprayCans();
}
//--Sortera produkter efter kategori--//
function sortByCategory(category) {
const filteredCans = sprayCans.filter(can => can.category === category);
sprayCansContainer.innerHTML = ''; //--Rensa container innan uppdatering--//
filteredCans.forEach(can => {
sprayCansContainer.innerHTML += `
<article>
<h1>${can.name}</h1>
<img src="${can.image.src}" alt="${can.image.alt}">
<div>Price: <span>${can.discountedPrice.toFixed(2)}</span> kr</div>
<div>Amount: <span>${can.amount}</span></div>
<button class="minus" data-id="${can.id}">-</button>
<button class="plus" data-id="${can.id}">+</button>
</article>
`;
});
addEventListenersToButtons();
}
//--Sortera produkter efter namn--//
function sortByName() {
sprayCans.sort((a, b) => a.name.localeCompare(b.name));
printSprayCans();
}
//--Sortera produkter efter pris--//
function sortByPrice() {
sprayCans.sort((a, b) => a.price - b.price);
printSprayCans();
}
//--Event listeners för sorteringsknappar--//
document.querySelector('#sortByRating').addEventListener('click', sortByRating);
document.querySelector('#sortByCategory').addEventListener('change', (event) => {
const selectedCategory = event.target.value;
sortByCategory(selectedCategory);
});
document.querySelector('#sortByName').addEventListener('click', sortByName);
document.querySelector('#sortByPrice').addEventListener('click', sortByPrice);
//--------------------------------------------------------------------//
/* --------------------------RABATTER------------------------------- */
//--------------------------------------------------------------------//
//--Hämta aktuell rabatt--//
function getCurrentDiscount() {
const now = new Date();
const day = now.getDay();
const hour = now.getHours();
if (day === 1 && hour < 10) {
return { type: 'discount', value: 0.10 }; // 10% rabatt på måndagar före kl. 10
} else if ((day === 5 && hour >= 15) || (day === 6) || (day === 0) || (day === 1 && hour < 3)) {
return { type: 'surcharge', value: 0.15 }; // 15% påslag från fredag kl. 15 till måndag kl. 03
}
return { type: 'none', value: 0 };
}
//--Tillämpa rabatter på sprayburkar--//
function applyDiscounts() {
const discountInfo = getCurrentDiscount();
sprayCans.forEach(can => {
if (discountInfo.type === 'surcharge') {
can.discountedPrice = can.price * (1 + discountInfo.value);
} else {
can.discountedPrice = can.price;
}
//--Hantera mängd-rabatt--//
if (can.amount >= 10) {
can.discountedPrice = can.discountedPrice * 0.90;
can.bulkDiscountApplied = true; //--Lägger till flagga för mängd-rabatt--//
} else {
can.bulkDiscountApplied = false; //--Återställ--//
}
});
}
//--Beräkna totalbelopp--//
function calculateTotal() {
let total = 0;
sprayCans.forEach(can => {
total += can.discountedPrice * can.amount;
});
const discountInfo = getCurrentDiscount();
if (discountInfo.type === 'discount') {
total *= (1 - discountInfo.value);
}
return total;
}
//--------------------------------------------------------------------//
/* -----------------------VARUKORG UPPDATERING------------------------ */
//--------------------------------------------------------------------//
//--Uppdatera varukorgen--//
function updateCart() {
cartContainer.innerHTML = '<h1>Shoppingcart</h1>';
let totalItems = 0;
let totalCost = 0;
sprayCans.forEach(can => {
if (can.amount > 0) {
cartContainer.innerHTML += `
<article class="cart-item">
<img src="${can.image.src}" alt="${can.image.alt}" class="cart-item-image">
<div class="cart-item-details">
<h1>${can.name}</h1>
<div>Price per unit: <span>${can.discountedPrice.toFixed(2)}</span> kr</div>
<div>Amount: <span>${can.amount}</span></div>
${can.bulkDiscountApplied ? '<div>10% discount!</div>' : ''}
</div>
</article>
`;
totalItems += can.amount;
totalCost += can.discountedPrice * can.amount;
}
});
//--Uppdatera antalet produkter på shoppingcart ikonen--//
document.querySelector('.cart-count').innerText = totalItems;
//--Lägg till rabatterad total--//
const discountInfo = getCurrentDiscount();
if (discountInfo.type === 'discount') {
cartContainer.innerHTML += `<div>Monday discount: 10% on your total!</div>`;
}
//Visar fraktkostnad--//
let shippingCost = 0;
if (totalItems > 15) {
shippingCost = 0;
} else {
shippingCost = 25 + (totalCost * 0.10);
}
cartContainer.innerHTML += `<div>Shipping: ${shippingCost.toFixed(2)} kr</div>`;
totalCost += shippingCost;
cartContainer.innerHTML += `<div>Total: ${totalCost.toFixed(2)} kr</div>`;
//--Betalningsmetod där faktura ej tillgängligt över 800kr--//
if (totalCost > 800) {
cartContainer.innerHTML += `<div>Payment method: Invoice not able over 800 kr</div>`;
} else {
cartContainer.innerHTML += `
<div class="form-group">
<label for="paymentMethod">Payment:</label>
<select id="paymentMethod">
<option value="card">Card</option>
<option value="invoice">Invoice</option>
</select>
</div>
`;
}
}
//--------------------------------------------------------------------//
/* ------------------------INAKTIVITETSTIMER------------------------- */
//--------------------------------------------------------------------//
//--Timer för att rensa varukorgen efter 15 minuter--//
let inactivityTimer;
function startInactivityTimer() {
clearTimeout(inactivityTimer);
inactivityTimer = setTimeout(() => {
alert('Sorry, you have been gone for too long!');
sprayCans.forEach(can => can.amount = 0); // Rensa varukorgen
saveCartToStorage(sprayCans); // Uppdatera localStorage
printSprayCans(); // Uppdatera visningen
}, 15 * 60 * 1000); // 15 minuter
}
//--------------------------------------------------------------------//
/* ----------------------KÖPHANTERINGSFUNKTIONER---------------------- */
//--------------------------------------------------------------------//
//--Minska mängden av en produkt--//
function decreaseAmount(e) {
const index = Number(e.currentTarget.dataset.id);
const arrayIndex = sprayCans.findIndex(item => item.id === index);
if (sprayCans[arrayIndex].amount > 0) {
sprayCans[arrayIndex].amount -= 1;
}
printSprayCans();
startInactivityTimer();
}
//--Öka mängden av en produkt--//
function increaseAmount(e) {
const index = Number(e.currentTarget.dataset.id);
const arrayIndex = sprayCans.findIndex(item => item.id === index);
sprayCans[arrayIndex].amount += 1;
printSprayCans();
startInactivityTimer();
}
//--------------------------------------------------------------------//
/* ------------------------VISNINGSFUNKTIONER------------------------- */
//--------------------------------------------------------------------//
//--Visa sprayburkarna--//
function printSprayCans() {
sprayCansContainer.innerHTML = '';
applyDiscounts();
sprayCans.forEach(can => {
sprayCansContainer.innerHTML += `
<article>
<h1>${can.name}</h1>
<img src="${can.image.src}" alt="${can.image.alt}">
<div>Price: <span>${can.discountedPrice.toFixed(2)}</span> kr</div>
<div>Amount: <span>${can.amount}</span></div>
<div>Rating: ${generateStars(can.stars)}</div>
<button class="minus" data-id="${can.id}">-</button>
<button class="plus" data-id="${can.id}">+</button>
</article>
`;
});
const minusBtns = document.querySelectorAll('button.minus');
const plusBtns = document.querySelectorAll('button.plus');
minusBtns.forEach(btn => {
btn.addEventListener('click', decreaseAmount);
});
plusBtns.forEach(btn => {
btn.addEventListener('click', increaseAmount);
});
updateCart();
}
//--Generera stjärnor baserat på betyg--//
function generateStars(rating) {
const fullStar = '★';
const halfStar = '☆';
const maxStars = 5;
let stars = '';
for (let i = 1; i <= maxStars; i++) {
if (i <= rating) {
stars += fullStar;
} else if (i - rating === 0.5) {
stars += halfStar;
} else {
stars += '☆';
}
}
return stars;
}
//--------------------------------------------------------------------//
/* -------------------------FORMULÄRHANTERING------------------------- */
//--------------------------------------------------------------------//
//--Återställer formuläret--//
function resetForm() {
const form = document.querySelector('form');
form.reset();
}
//--Lyssnare för att återställa formuläret--//
document.querySelector('#resetForm').addEventListener('click', resetForm);