-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
395 lines (355 loc) · 13.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Books Store</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="js/lessons.js"></script>
<script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js"
integrity="sha384-rOA1PnstxnOBLzCLMcre8ybwbTmemjzdNlILg8O7z1lUkLXozs4DHonlDtnE7fpc"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" >
<link href="https://fonts.googleapis.com/css2?family=Lato&display=swap" rel="stylesheet">
<style>
.step-one {
margin-top: 10px;
height: 750px;
overflow-y: scroll;
}
.input {
border-radius: 8px;
border: 1px solid var(--neutral-100, #E7EAEE);
background: #FBFBFB;
width: 200px;
padding: 6px;
outline: none!important;
font-size: 13px;
}
label {
margin-bottom: 10px;
font-size: 14px;
}
main {
box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.075);
background-color: white;
width: 1000px;
border-radius: 6px;
}
body {
display: flex;
justify-content: center;
background-color: #f6f6f6;
font-family: 'Lato', sans-serif;
}
.badge:after{
content:attr(value);
font-size:12px;
color: #fff;
background: red;
border-radius:50%;
padding: 0 5px;
position:relative;
left:-8px;
top:-10px;
opacity:0.9;
}
.lds-dual-ring {
display: inline-block;
width: 80px;
height: 80px;
}
.lds-dual-ring:after {
content: " ";
display: block;
width: 64px;
height: 64px;
margin: 8px;
border-radius: 50%;
border: 6px solid #ffffff;
border-color: #000000 transparent #000000 transparent;
animation: lds-dual-ring 1.2s linear infinite;
}
@keyframes lds-dual-ring {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div id="app" class="p-5">
<header>
<div class="text-center" style="font-size: 25px; margin-bottom: 50px;" v-text="sitename"></div>
</header>
<main>
<p></p>
<div v-if="step === 1" class="step-one">
<div class="px-4 py-4 justify-content-center">
<div class="">
<div class="d-flex justify-content-between my-2">
<div style="margin-top: 5px;" class="me-5">
<label for="sort_by">Sort By:</label>
<select id="sort_by" v-model="sort_by" class="input ml-2">
<option disabled value="">None</option>
<option value="topic">Subject</option>
<option value="location">Location</option>
<option value="price">Price</option>
<option value="spaces">Spaces</option>
</select>
<div style="margin-top: 15px;">
<label for="sort_order">Sort Order:</label>
<select id="sort_order" v-model="sort_order" class="input ml-2">
<option disabled value="">None</option>
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
</select>
</div>
</div>
<div style="margin-top: 20px;" class="mb-4">
<label for="search">Search:</label>
<input @input="fetchLessons" id="search" type="search" name="search" v-model="search" class=" input ml-2" />
</div>
</div>
<div class="d-flex justify-content-between align-items-center border-bottom mt-5 py-2">
<div class="col-4">Subject</div>
<div class="col-2 justify-content-end ml-auto">Location</div>
<div class="col-2">Price</div>
<div class="col-2">Spaces</div>
<div class="col-2"></div>
</div>
<div v-if="!isLoading">
<div style="display: inline-block; padding: 30px 0px 30px; border-bottom: 1px solid rgb(235, 227, 227);" v-for="(lesson, index) in getLessons()"
class="d-flex justify-content-between">
<div class="col-4 d-flex ">
<p> <img style="height: 50px; width: 50px;" :alt="lesson.topic" :src="getImageUrl(lesson.image)"></img></p>
<p style="margin-left: 20px;">
<template v-for="(fragment, i) in lesson.topic.toString().split(new RegExp(`(?<=${search})|(?=${search})`, 'i'))">
<mark v-if="fragment.toLowerCase() === search.toLowerCase()" :key="i" class="text-warning">
{{ fragment }}
</mark>
<template v-else>{{ fragment }}</template>
</template>
</p>
</div>
<div class="col-2 justify-content-end ml-auto">
<template v-for="(fragment, i) in lesson.location.toString().split(new RegExp(`(?<=${search})|(?=${search})`, 'i'))">
<mark v-if="fragment.toLowerCase() === search.toLowerCase()" :key="i" class="text-warning">
{{ fragment }}
</mark>
<template v-else>{{ fragment }}</template>
</template>
</div>
<div class="col-2 ">
<p>${{lesson.price}}</p>
</div>
<div class="col-2">
<p class="mx-4">{{lesson.spaces}}</p>
</div>
<div class="col-2 text-center">
<button type="button" :disabled="!canAddToCart(lesson)" v-on:click="addToCart(lesson)" class="rounded-2 btn btn-info p-3 text-white" style="border-radius: 6px; cursor: pointer;">
<span class="fas fa-cart-plus"></span>
Add to cart
</button>
</div>
</div>
</div>
<div v-else class="d-flex mt-5 justify-content-center">
<div class="lds-dual-ring justify-content-center align-items-center "></div>
</div>
</div>
<div class="d-flex justify-content-center ml-auto mb-3 fixed-bottom" >
<button type="button" :disabled="cartItemCount < 1" v-on:click="showCheckout" class="btn btn-info p-3 rounded-2 text-white" style="border-radius: 6px; cursor: pointer;">
{{cartItemCount}}
<span class="fas fa-shopping-cart"></span>
View Cart
</button>
</div>
</div>
</div>
<div v-if="step === 2" class="px-4 py-4 justify-content-center">
<div class="d-flex justify-content-between align-items-center border-bottom mt-4 py-2" >
<div class="col-4">Subject</div>
<div class="col-2 justify-content-end ml-auto">Location:</div>
<div class="col-2 ">Price</div>
<div class="col-2">Quantity In Cart:</div>
<div class="col-2"></div>
</div>
<div style="display: inline-block;" v-for="(lesson, index) in cart" class="d-flex justify-content-between mt-4">
<div class="col-4 d-flex ">
<p> <img style="height: 50px; width: 50px;" :alt="lesson.topic" :src="getImageUrl(lesson.image)"></img></p> <p style="margin-left: 20px;">{{lesson.topic}} </p>
</div>
<div class="col-2 justify-content-end ml-auto ">
<p>{{lesson.location}}</p>
</div>
<div class="col-2">
<p>${{lesson.price}}</p>
</div>
<div class="col-2">
<p>{{lesson.cart_quantity}}</p>
</div>
<div class="col-2 text-center ">
<button v-on:click="removeFromCart(lesson)" class="border border-none p-2 rounded-2 bg-info text-white" style="border-radius: 6px; cursor: pointer; margin-left: -40px;">
Remove
</button>
</div>
</div>
</div>
<form @submit.prevent="submitForm" v-if="step === 2" class="px-5 py-4 justify-content-center ">
<div>
<h2 style="font-size: 21px; margin-bottom: 15px;">Checkout</h2>
<p >
<label for="name"><strong>Name:</strong></label>
<input id="name" v-model.trim="order.name" class="input ml-2" />
</p>
<p>
<label for="phone"><strong>Phone:</strong></label>
<input id="phone" v-model.trim="order.phone" class="input ml-2" />
</p>
<button :disabled="!canCheckout" type="submit" class="btn btn-info p-3 rounded-2 text-white mt-4" style="border-radius: 6px;">
Checkout
</button>
</div>
<div class="d-flex justify-content-center ml-auto mb-3 fixed-bottom" >
<button :disabled="isLoading" v-on:click="showLessons" class="border border-none p-3 rounded-2 bg-info text-white" style="border-radius: 6px; cursor: pointer;">
<span class="fas fa-arrow-left"></span>
{{isLoading ? 'Please wait...' : 'Back To Lessons'}}
</button>
</div>
</form>
</main>
</div>
<script type="application/javascript">
var webstore = new Vue({
el: '#app',
data() {
return {
isLoading: false,
step: 1,
lessons: [],
search: '',
sort_by: 'topic',
sort_order: 'asc',
sitename: 'Books Store',
order: {
name: '',
phone: '',
},
cart: [],
base_url: 'https://cw2.devnode.ng',
image_base_url: `https://cw2.devnode.ng/public`
}
},
mounted(){
this.fetchLessons()
},
methods: {
fetchLessons(){
this.isLoading = true;
let endpoint = this.search.length > 0 ? 'search?q='+this.search : 'lessons'
fetch(`${this.base_url}/${endpoint}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
.then(response => response.json())
.then(responseJSON => {
this.lessons = responseJSON;
})
.catch((error) => {
console.log(error)
}).finally( () => this.isLoading = false)
},
getImageUrl(image_path){
return this.image_base_url+'/'+image_path
},
addToCart(lesson) {
let item = this.cart.find(({ id }) => id === lesson.id);
if (item) {
item.cart_quantity += 1;
item.spaces -= 1;
}
else {
lesson.spaces -= 1;
lesson.cart_quantity = 1;
this.cart.push(lesson);
}
},
showCheckout() {
this.step = 2;
},
showLessons() {
this.step = 1;
},
submitForm() {
this.isLoading = true;
let lessonsInOrder = this.cart.map(item => ({id: item._id, topic: item.topic,location: item.location, numberOfSpaces: item.cart_quantity}));
fetch(`${this.base_url}/order`, {
method: 'POST', // set the HTTP method as 'POST'
headers: {
'Content-Type': 'application/json', // set the data type as JSON
},
body: JSON.stringify({...this.order,lessonsInOrder}), // need to stringify the JSON object
})
.then(response => response.json())
.then(responseJSON => {
//loop through all lessons bought and subtract the quantity bought
this.cart.forEach((lesson) => {
this.reduceLessonQuantity(lesson._id, lesson.cart_quantity);
});
alert("Order Submitted.");
location.reload();
})
.catch((error) => {
console.log(error)
}).finally(() => this.isLoading = false)
},
reduceLessonQuantity(lessonId, reduceByQuantity){
fetch(`${this.base_url}/lesson`, {
method: 'PUT', // set the HTTP method as 'PUT'
headers: {
'Content-Type': 'application/json', // set the data type as JSON
},
body: JSON.stringify({lessonId: lessonId, reduceByQuantity: reduceByQuantity}), // need to stringify the JSON object
})
},
canAddToCart(lesson) {
return lesson.spaces > 0;
},
removeFromCart(lesson) {
this.cart.splice(this.cart.indexOf(lesson), 1);
let item = this.lessons.find(({ id }) => id === lesson.id);
item.spaces = 5;
item.cart_quantity = 0;
if (this.cartItemCount < 1) {
this.step = 1;
}
},
getLessons() {
let transformedLessons = [...this.lessons];
if (this.sort_by === 'topic')
transformedLessons.sort((a, b) => (a.topic > b.topic) ? this.sort_order === 'asc' ? 1 : -1 : this.sort_order === 'asc' ? -1 : 1);
if (this.sort_by === 'location')
transformedLessons.sort((a, b) => (a.location > b.location) ? this.sort_order === 'asc' ? 1 : -1 : this.sort_order === 'asc' ? -1 : 1);
if (this.sort_by === 'price')
transformedLessons.sort((a, b) => (a.price > b.price) ? this.sort_order === 'asc' ? 1 : -1 : this.sort_order === 'asc' ? -1 : 1);
if (this.sort_by === 'spaces')
transformedLessons.sort((a, b) => (a.spaces > b.spaces) ? this.sort_order === 'asc' ? 1 : -1 : this.sort_order === 'asc' ? -1 : 1);
return transformedLessons;
},
},
computed: {
cartItemCount: function () {
return this.cart.length;
},
canCheckout() {
return !this.isLoading && this.order.name.length > 0 && this.order.phone.length > 0 && /^[a-zA-Z\s]*$/.test(this.order.name) && /^\d+$/.test(this.order.phone);
}
}
});
</script>
</body>
</html>