-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
154 lines (130 loc) · 4.86 KB
/
Copy pathscript.js
File metadata and controls
154 lines (130 loc) · 4.86 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
// get cart items from local storage, if any - when web app first runs
function updateCart() {
if (localStorage.getItem('pizza-palace-cart')) {
var inCartItems = JSON.parse(localStorage.getItem('pizza-palace-cart'));
var cartItemsContainer = $('.cart-container .cart-items');
var itemsCount = $('.cart-icon .items-count');
var totalPriceContainer = $('.cart-container .total-price span');
itemsCount.html(inCartItems.length);
var containerInnerHtml = '';
var totalPrice = 0;
var id = 1;
for (let i = 0; i < inCartItems.length; i++) {
containerInnerHtml += `
<li class="item" id="item-${id}">
${inCartItems[i].size} pizza with ${inCartItems[i].crust} crust and ${inCartItems[i].toppings} toppings total of ksh.${inCartItems[i].totalPrice}
<i class="fas fa-times"></i>
</li>
`
id += 1;
totalPrice += inCartItems[i].totalPrice;
}
cartItemsContainer.html(containerInnerHtml);
totalPriceContainer.html(totalPrice);
}
else {
localStorage.setItem('pizza-palace-cart', JSON.stringify([]));
}
}
updateCart();
// clear cart functionality
var clearCartBtn = $('#clear-cart');
clearCartBtn.click(function() {
localStorage.setItem('pizza-palace-cart', JSON.stringify([]));
updateCart();
})
// pop up the cart modal if cart button is clicked
var cartIconBtn = $('.cart-icon');
var cartModal = $('.cart-container');
var closeCartModalBtn = $('.cart-close-btn .fa-times');
// opens cart
cartIconBtn.click(function() {
cartModal.addClass('active');
})
// closes cart
closeCartModalBtn.click(function() {
cartModal.removeClass('active');
})
// the order pizza functionality - create pizza
// create a pizza object
class Pizza {
constructor(size, crust, toppings, totalPrice) {
this.size = size;
this.crust = crust;
this.toppings = toppings;
this.totalPrice = totalPrice;
}
}
// get the form element
var form = $('form');
form.submit(function(event) {
event.preventDefault();
var sizeValue = $('.size input:checked').attr('id');
var sizePrice = parseInt($('.size input:checked').val());
var crustValue = $('.crust input:checked').attr('id');
var crustPrice = parseInt($('.crust input:checked').val());
// get all toppings
var toppingsList = document.querySelectorAll('.toppings input');
var toppings = [];
var toppingsPrice = 0;
for (let i = 0; i < toppingsList.length; i++) {
if (toppingsList[i].checked) {
toppings.push(toppingsList[i].id);
toppingsPrice += parseInt(toppingsList[i].value);
}
}
if (sizeValue && crustValue && (toppings.length > 0)) {
var totalPrice = sizePrice + crustPrice + toppingsPrice;
console.log(totalPrice);
var pizza = new Pizza(sizeValue, crustValue, toppings, totalPrice);
// update cart
var inCart = JSON.parse(localStorage.getItem('pizza-palace-cart'));
inCart.push(pizza);
localStorage.setItem('pizza-palace-cart', JSON.stringify(inCart));
updateCart();
// reset form
document.querySelector('form').reset();
var message = `You have created one ${pizza.size} with ${pizza.crust} crust and ${pizza.toppings} as toppings. Your total for this is ${pizza.totalPrice}`;
alert(message);
}
else {
alert('all fields are required!');
}
})
// checkout functionality
var openCheckOutBtn = $('#checkout');
openCheckOutBtn.click(function() {
$('.cart-container').removeClass('active');
$('.checkout-container').addClass('active');
})
// close checkout
var closeCheckoutBtn = $('.checkout-close-btn i');
closeCheckoutBtn.click(function() {
$('.checkout-container').removeClass('active');
})
// order functionality
var orderBtn = $('#order-btn');
orderBtn.click(function() {
var deliveryLocationValue = $('#delivery-location').val();
var inCartItems = JSON.parse(localStorage.getItem('pizza-palace-cart'));
// check if any items in the cart
if (inCartItems.length < 1) {
alert('you have no items to order!');
}
else {
var totalPrice = 0;
for (let i = 0; i < inCartItems.length; i++) {
totalPrice += parseInt(inCartItems[i].totalPrice);
}
if (deliveryLocationValue) {
totalPrice += 100;
var message = `your pizza(s) will be delivered at ${deliveryLocationValue} at a total of ksh.${totalPrice} including ksh.100 for delivery charges. thank you!`
alert(message);
}
else {
var message = `you can come pick up your pizza(s) for a total of ksh.${totalPrice}. thank you!`
alert(message);
}
$('.checkout-container').removeClass('active');
}
})