-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
438 lines (402 loc) · 13.8 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
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
// A $( document ).ready() block.
$(document).ready(function () {
//$("#order-sectionn").hide();
$("#the-cart").hide();
$(".overlay").hide();
$(".badge").hide();
$(".carted").hide();
$(".missing").hide();
$("#order button").click(function () {});
$(".overlay").click(function () {
$(".overlay").hide();
$("#the-cart").hide();
});
$(".d-btn").click(function () {
$("#the-cart").hide();
});
$(".times").click(function () {
console.log("hello");
$(".overlay").hide();
$("#the-cart").hide();
});
$(".carda").click(function () {
$("#order-sectionn").show();
$(".overlay").show();
});
$(".cart-btn button").click(function () {
const selector = $("#inputGroupSelect01").find(":selected").text();
const selector2 = $("#inputGroupSelect02").find(":selected").text();
console.log(selector2);
if (selector === "Choose..." || selector2 == "Choose...") {
$(".missing").show();
$(".missing").text("Please choose preffered size");
} else {
$("#selection").hide();
$(".overlay").hide();
$(".carted").show().delay(300);
}
});
let carda = $(".carda").length;
for (let i = 0; i < carda; i++) {
$(".carda").click(function () {
return carda + 1;
console.log(carda);
});
}
/*CART CLICKED*/
$(".fa-cart-plus").click(function () {
$("#the-cart").show();
$(".overlay").show();
});
const pizzas = [
{ name: "Baked Ziti" },
{ name: "Chicago pizza" },
{ name: "French bread" },
{ name: "Grandma slice" },
];
//Pizza Sizes
const pizzaSizes = [
{
size: "Small",
price: 600,
},
{
size: "Medium",
price: 800,
},
{
size: "Large",
price: 1000,
},
];
//Pizza Crusts
const pizzaCrusts = [
{
name: "Crispy",
price: 200,
},
{
name: "Stuffed",
price: 250,
},
{
name: "Glutten Free",
price: 150,
},
];
//Toppings
const pizzaToppings = ["pepperoni", "Mushroom", "Black"];
//Pizza Constructor
function Pizza(name) {
this.name = name;
this.price = 0;
this.quantity = 1; //This is the minimum quantity
this.toppings = [];
}
//Pizza Size Prototype
Pizza.prototype.setSize = function (size) {
const pizzaSize = pizzaSizes.find((pizzaSize) => pizzaSize.size == size);
if (pizzaSize) {
this.size = pizzaSize;
this.calculateTotal();
}
};
//Pizza Crust Prototype
Pizza.prototype.setCrust = function (name) {
const pizzaCrust = pizzaCrusts.find(
(pizzaCrust) => pizzaCrust.name == name
);
if (pizzaCrust) {
this.crust = pizzaCrust;
this.calculateTotal();
}
};
//Pizza Toppings Prototype
Pizza.prototype.setTopings = function (toppings) {
this.toppings = toppings;
this.calculateTotal();
};
//Pizza Quantity Prototype
Pizza.prototype.setQuantity = function (quantity) {
this.quantity = +quantity;
this.calculateTotal();
};
// Pizza Prototype Total
Pizza.prototype.calculateTotal = function () {
const toppingPrice = 100;
if (this.size) {
this.price = this.size.price;
}
if (this.crust) {
this.price = this.price + this.crust.price;
}
// Add Topping Price
this.price += this.toppings.length * toppingPrice;
this.price *= this.quantity;
};
//Appending Function
$(function () {
//pizza names
pizzas.forEach((pizza) => {
$("#pizza").append(
`<option value="${pizza.name}">${pizza.name}</option>`
);
});
//pizza sizes
pizzaSizes.forEach((pizzaSize) => {
$("#size").append(
`<option value="${pizzaSize.size}">${pizzaSize.size}-${pizzaSize.price}</option>`
);
});
//pizza crusts
pizzaCrusts.forEach((pizzaCrust) => {
$("#crust").append(
`<option value="${pizzaCrust.name}">${pizzaCrust.name}-${pizzaCrust.price}</option>`
);
});
//pizza toppings
pizzaToppings.forEach((topping) => {
$(".toppings").append(`<div class="col-md-6">
<div class="form-check">
<input class="form-check-input" name="toppings[]" type="checkbox" id="${topping}" value="${topping}">
<label class="form-check-label" for="${topping}">
${topping}
</label>
</div>
</div>`);
});
//Calculating Grand Total
function calculateGrandTotal() {
let total = 0;
cart.forEach((pizza) => {
total += pizza.price;
});
$(".grand-total").html(`Ksh <span class="text-bold">${total}</span> `);
}
//Cart Array
const cart = [];
//Check if cart is empty
if (cart.length == 0) {
$(".empty-cart").show();
$(".delivery-button").hide();
} else {
$(".empty-cart").hide();
}
$("#order-form").on("submit", function (event) {
//Prevent Default Action
event.preventDefault();
//Get Selected Values
const selectedPizzaName = $("#pizza").val();
const selectedSize = $("#size").val();
const selectedCrust = $("#crust").val();
const selectedToppings = $("input[name='toppings[]']:checkbox:checked")
.map(function () {
return $(this).val();
})
.get();
//Field Validation
if (!selectedPizzaName || !selectedSize || !selectedCrust) {
$("#error").text("*Flavor, size and crust fields required* ");
return;
} else {
$(".carted").fadeIn(1500, function () {
$(".carted").delay(1000).fadeOut(1000);
$(".badge").show();
});
}
//Cart Details
//Check if selected pizza exists in cart
const cartPizza = cart.find((pizza) => {
const sameToppings =
JSON.stringify(pizza.toppings) == JSON.stringify(selectedToppings);
return (
pizza.name == selectedPizzaName &&
pizza.size.size == selectedSize &&
sameToppings
);
});
//Increase Quantity if Exists
if (cartPizza) {
cartPizza.setQuantity(cartPizza.quantity + 1);
} else {
const pizza = new Pizza(selectedPizzaName);
pizza.setSize(selectedSize);
pizza.setCrust(selectedCrust);
pizza.setTopings(selectedToppings);
cart.push(pizza);
}
//Empty table body
$(".order-table tbody").html("");
//loop and append
cart.forEach((pizza, cartIndex) => {
$(".order-table tbody").append(`
<tr>
<td >${pizza.name}</td>
<td >${pizza.size.size}</td>
<td >${pizza.crust.name}</td>
<td>${pizza.toppings.join(", ")}</td>
<td>
<input type="number" min="1" class="input-sm form-control pizza-quantity" data-cart-index="${cartIndex}" value="${
pizza.quantity
}" />
</td>
<td>Ksh ${pizza.price}</td>
</tr>
`);
//Show proceed button
$(".delivery-button").show();
// console.log(pizza);
//update grand total
calculateGrandTotal();
});
});
//Pizza Quantity Change
$("body").on("change", ".pizza-quantity", function () {
const quantity = $(this).val();
const cartIndex = $(this).data("cart-index");
const pizza = cart[cartIndex];
if (quantity > 0) {
pizza.setQuantity(quantity);
// update line total
$(this)
.parent()
.next()
.html(`Ksh <span class="text-bold">${pizza.price}</span> `);
}
//update grand total
calculateGrandTotal();
});
//Delivery Modal
$("#delivery-form").on("submit", function (event) {
//prevent default action
event.preventDefault();
//Radio Button Selection Validation
const selectd = $("input[name='deliveryMethod']:checked");
if (selectd.val() == undefined) {
$(".delivery-option").html(
"<p class='text-danger'>*Delivery method required*</p>"
);
return;
} else {
$(".delivery-option").text("");
//Check Selected Radio Button
if (selectd.val() == "delivery") {
$("#location-input-details").show();
//Constant User Input Variables
const customerName = $("#customerName").val();
const customerPhone = $("#customerPhone").val();
const customerLocation = $("#customerLocation").val();
const additionalInfo = $("#additionalInfo").val();
//User Input Validation
if (!customerName || !customerPhone || !customerLocation) {
$(".error-delivery-location").text(
"Fill in all input fields with * to proceed!"
);
return;
} else {
$(".error-delivery-location").text("");
}
function calculateGrandTotal() {
let total = 0;
cart.forEach((pizza) => {
total += pizza.price;
});
const getTotalPlusDeliveryFee = total + 200;
console.log(getTotalPlusDeliveryFee);
console.log(cart);
$("#select-delivery-method").hide();
$(".delivery-head").append(`
<div class="alert alert-success" role="alert">Hello ${customerName}. Order successfully processed. Your order will be delivered to your location(${customerLocation})</div>
<div class="d-flex justify-content-between">
<div>
<h5>Order Summary</h5>
</div>
<div>
<p class="color-palace float-right">Total Ksh <span class="text-bold">${getTotalPlusDeliveryFee}</span></p>
</div>
</div>
`);
//loop and append
cart.forEach((pizza, cartIndex) => {
$(".delivery-bottom").append(`
<div>
<div class="row">
<div class="col-md-12">
<ol class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-start">
<div class="ms-2 me-auto">
<div class="fw-bold">${
pizza.name
}(${pizza.size.size})</div>
Crust - ${pizza.crust.name} <br>
Toppings - ${pizza.toppings.join(
", "
)}
</div>
<span class="badge bg-primary rounded-pill">${
pizza.quantity
}</span>
</li>
</ol>
</div>
</div>
</div>
`);
});
}
calculateGrandTotal();
// $("#deliveryMethodModal").hide();
} else if (selectd.val() == "pickup") {
function calculateGrandTotal() {
let total = 0;
cart.forEach((pizza) => {
total += pizza.price;
});
const getTotalPlusDeliveryFee = total;
console.log(getTotalPlusDeliveryFee);
$("#select-delivery-method").hide();
$(".delivery-head").append(`
<div class="alert alert-success" role="alert">Hello. Order successfully processed. Your order will be delivered to your pickup point</div>
<div class="d-flex justify-content-between">
<div>
<h5>Order Summary</h5>
</div>
<div>
<p class="color-palace float-right">Total Ksh <span class="text-bold">${getTotalPlusDeliveryFee}</span></p>
</div>
</div>
`);
//loop and append
cart.forEach((pizza, cartIndex) => {
$(".delivery-bottom").append(`
<div>
<div class="row">
<div class="col-md-12">
<ol class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-start">
<div class="ms-2 me-auto">
<div class="fw-bold">${
pizza.name
}(${pizza.size.size})</div>
Crust - ${pizza.crust.name} <br>
Toppings - ${pizza.toppings.join(
", "
)}
</div>
<span class="badge bg-primary rounded-pill">${
pizza.quantity
}</span>
</li>
</ol>
</div>
</div>
</div>
`);
});
}
calculateGrandTotal();
}
}
});
});
});