-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
475 lines (445 loc) · 15.4 KB
/
index.html
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>After School Activities</title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="products.js"></script>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"
/>
</head>
<style>
#showproduct {
text-align: center;
background-color: #eeeeee;
padding-bottom: 10px;
padding-top: 10px;
}
#header {
text-align: center;
background-color: #eeeeee;
padding-bottom: 10px;
padding-top: 9px;
}
</style>
<body>
<!-- Mounting the app-->
<div id="app">
<header id="header">
<h1 v-text="sitename"></h1>
<!-- Cart item count button-->
<!-- After every click from the addItem button, the value will increase by 1 (increments)-->
<button v-if="cartItemCount" v-on:click = "showCheckout">
{{cartItemCount}}
<span class="fas fa-cart-plus"></span>
</button>
</header>
<main>
<div v-if="showproduct" id="showproduct">
<div>
<input type="text" v-model="search" placeholder="Search" />
<i class="fa fa-search"></i>
</div>
<div>
<select name="sortby" id="select" v-model="sortby">
<option value="alphabetically">Title</option>
<option value="alphabetically2">Location</option>
<option value="price">Price</option>
<option value="slots">Slots</option>
</select>
<button v-on:click="ascending = !ascending" class="sort-button">
<i v-if="ascending" class="fa fa-sort-up"></i>
<i v-else class="fa fa-sort-down"></i>
</button>
</div>
<div v-for="product in filteredProductsName">
<figure>
<img v-bind:src="product.image" style="height: 80px" />
</figure>
<!-- Product details-->
<!-- V-model : enables two way data-binding to form input elements -->
<h2 v-text="product.title"></h2>
<!-- updates elements text content with the data in Vuejs-->
<p v-html="product.description"></p>
<!-- updates html elements text content, but it actually renders it, while v-text only treats it as a string -->
<p>Price:{{product.price}}</p>
<p>Location:{{product.location}}</p>
<p>Left:{{product.space}}</p>
<!-- Buttons -->
<!-- Button when product.space is not 0 -->
<button v-on:click="addItem(product)" v-if="canAddToCart(product)">
<!-- binds js snippets with vue js-->
Add to Cart please
<span class="fas fa-cart-plus"></span>
</button>
<button disabled="disabled" v-else>
Add to Cart please
<span class="fas fa-cart-plus"></span>
</button>
<div>
<span v-for="n in product.rating"
><i class="fa-solid fa-star"></i
></span>
<span v-for="n in 5-product.rating"
><i class="fa-regular fa-star"></i
></span>
</div>
<!-- Button when space left is 0 becomes diasbled -->
<div>
<span v-if="product.availableInventory === cartCount(product.id)">
All out
</span>
<span
v-else-if="product.availableInventory - cartCount(product.id) <= 5 "
>
Only {{product.availableInventory-cartCount(product.id)}} left
</span>
<span v-else>Buy now !!!</span>
</div>
</div>
</div>
<div v-else>
<!-- user information input -->
<h2>checkout</h2>
<p>
<strong> Name </strong>
<input
v-model.trim="order.Name"
type="text"
@keyup="namevalidation"
/>
<strong>Phone Number</strong>
<input
v-model.trim="order.phoneNumber "
name="phonenumber"
@keyup="phoneValidation"
/>
</p>
<p>
<strong>Adress:</strong>
<input v-model="order.address" />
<strong>City</strong>
<input v-model="order.city" />
</p>
<p>
<strong>ZIP code:</strong>
<input v-model.number="order.zip" type="number" />
</p>
<p>
<input
v-model="order.gift"
value="gift"
id="gift"
type="checkbox"
v-bind:true-value="order.sendGift"
v-bind:false-value="order.donotGift"
/>
<label for="gift">Gift</label>
</p>
<p>
<input v-model="order.method" value="home" id="home" type="radio" />
<label for="home">Home</label>
<input
v-model="order.method"
value="business"
id="business"
type="radio"
/>
<label for="business">Business</label>
<!-- checks for validation -->
<button v-on:click="finalValidation()">Checkout</button>
</p>
<!-- CART -->
<div id="productInCart">
<div v-for="i in this.cart" id="cart">
<figure>
<img v-bind:src="i.image" style="height: 50px" />
</figure>
<p>{{i.title}}</p>
<p>{{i.description}}</p>
<p>{{i.price}}</p>
<button v-on:click="cartremove(i.id,i.title)">Delete</button>
</div>
</div>
<div v-if="ishidden">
<!-- user information is displayed -->
<h2>Order Information</h2>
<p>First Name: {{order.Name}}</p>
<p>Phone Number:{{order.phoneNumber}}</p>
<p>Zip {{order.zip}}</p>
<p>Adress: {{order.address}}</p>
<p>City: {{order.city}}</p>
<p>Method:{{order.method}}</p>
<p>Gift:{{order.gift}}</p>
<button v-on:click="submittedForm()">Place order</button>
</div>
<div v-else></div>
</div>
</main>
</div>
<script>
var webstore = new Vue({
// mounts the Vue application on a given DOM element.
el: "#app",
data: {
ascending: true,
sortby: "alphabetically",
sitename: "After School Activities",
products: products,
search: "",
cart: [],
cartitem: [],
ishidden: false,
showproduct: true,
order: {
Name: "",
city: "",
zip: "",
method: "home",
sendGift: "Send as a gift",
donotGift: "Do not send as a gift",
},
},
methods: {
// addItem is called after the first button is clicked
// method which decrements the product.space value after
//every click on the "Add to cart please" button
addItem(product) {
// products are added to the cart
this.cart.push({
title: product.title,
price: product.price,
description: product.description,
image: product.image,
}),
this.cartitem.push(product.id);
product.space = product.space - 1;
product.availableInventory = product.availableInventory - 1; // decrementing the product.space
},
showCheckout() {
// shows the checkout
this.showproduct = this.showproduct ? false : true;
},
submittedForm: function () {
alert("order placed successfully"), window.location.reload();
},
canAddToCart(product) {
// checking if product.available inventory is greater than cartCount value
return product.availableInventory > this.cartCount(product.id);
},
showcart() {
if (this.cartisHidden == true) {
this.cartisHidden = false;
} else {
this.cartisHidden = true;
}
},
cartremove(id, title) // removes the item from the cart
{
this.cart.splice(this.cart.indexOf(id), 1);
this.cartitem.splice(this.cartitem.indexOf(id), 1);
products[
products.indexOf(products.find((o) => o.title === title))
].space += 1;
if(this.cart.length === 0 )
{
this.showproduct = true;
}
},
cartCount(id) {
// checks the number of products in the cart
let count = 0;
for (let i = 0; i < this.cart.length; i++) {
if (this.cart[i] === id) {
count++;
}
}
return count;
},
phoneValidation() {
// checks if the mobile number
const regex =
/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im;
if (this.order.phoneNumber.match(regex)) {
this.isvalidNumber = true;
} else {
this.isvalidNumber = false;
}
},
namevalidation() {
const regex = /^[a-z ,.'-]+$/i;
if (this.order.Name.match(regex)) {
return (this.isvalidname = true);
} else {
return (this.isvalidname = false);
}
},
finalValidation() {
// check if all the input fields are valid
if (
this.order.firstName == "" ||
this.order.lastName == "" ||
this.order.phoneNumber == "" ||
this.order.address == "" ||
this.order.state == "" ||
this.order.city == ""
) {
this.ishidden = false;
alert("Please fill out the form");
} else if (!this.isvalidNumber) {
alert("Please enter a valid number");
} else if (!this.isvalidname) {
alert("please enter valid name");
} else {
this.ishidden = true;
}
},
},
// computed properties enable you to create a property
computed: {
cartItemCount: function () {
// getting the size of cart array
return this.cartitem.length;
},
sortedProducts() {
let productsArray = this.products.slice(0);
function compare(a, b) {
if (a.price > b.price) return 1;
if (a.price < b.price) return -1;
return 0;
}
return productsArray.sort(compare);
},
filteredProductsName() {
if (this.search != "" && this.search) {
let productsArray = this.products;
productsArray = productsArray.filter((item) => {
return (
item.title
.toUpperCase()
.includes(this.search.toUpperCase()) ||
item.location
.toUpperCase()
.includes(this.search.toUpperCase())
);
});
return productsArray;
} else if (this.sortby == "price") {
// sorts the products by price , location , and the space left
if (!this.ascending) {
let productsArray = this.products.slice(0);
function compare(a, b) {
if (a.price > b.price) {
return 1;
}
if (a.price < b.price) {
return -1;
}
return 0;
}
return productsArray.sort(compare).reverse();
} else {
let productsArray = this.products.slice(0);
function compare(a, b) {
if (a.price > b.price) {
return 1;
}
if (a.price < b.price) {
return -1;
}
return 0;
}
return productsArray.sort(compare);
}
} else if (this.sortby == "alphabetically") {
if (!this.ascending) {
let productsArray = this.products.slice(0);
function compare(a, b) {
if (a.title > b.title) {
return 1;
}
if (a.title < b.title) {
return -1;
}
return 0;
}
return productsArray.sort(compare).reverse();
} else {
let productsArray = this.products.slice(0);
function compare(a, b) {
if (a.title > b.title) {
return 1;
}
if (a.title < b.title) {
return -1;
}
return 0;
}
return productsArray.sort(compare);
}
}else if (this.sortby == "alphabetically2") {
if (!this.ascending) {
let productsArray = this.products.slice(0);
function compare(a, b) {
if (a.title > b.title) {
return 1;
}
if (a.location < b.location) {
return -1;
}
return 0;
}
return productsArray.sort(compare).reverse();
} else {
let productsArray = this.products.slice(0);
function compare(a, b) {
if (a.location > b.location) {
return 1;
}
if (a.location < b.location) {
return -1;
}
return 0;
}
return productsArray.sort(compare);
}
}
else if (this.sortby == "slots") {
if (!this.ascending) {
let productsArray = this.products.slice(0);
function compare(a, b) {
if (a.space > b.space) {
return 1;
}
if (a.space < b.space) {
return -1;
}
return 0;
}
return productsArray.sort(compare).reverse();
} else {
let productsArray = this.products.slice(0);
function compare(a, b) {
if (a.space > b.space) {
return 1;
}
if (a.space < b.space) {
return -1;
}
return 0;
}
return productsArray.sort(compare);
}
}
},
},
});
</script>
</body>
</html>