-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathindex.js
More file actions
278 lines (254 loc) · 9.85 KB
/
index.js
File metadata and controls
278 lines (254 loc) · 9.85 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
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
//? Why is storing the shopping cart in sessionStorage not the best choice?
//* It is not the best choice because local storage is a much better option. If the customer leaves their tab in
//* any way (close tab/browser, shuts down computer, battery dies, etc) they would lose everything they placed in their cart */
//? What should happen when the Place Order button is clicked?
//* We should use the POST to send out data (cart and client info) to the server */
//! declaring variables for container, holding data for functions, and are you there feature
let container = document.querySelector('.product_list');
let holder = '';
let areYouThere = true;
// //! SetTimeout for 'Are you there'
// let promptTimeout = () => {
// // maintains timeout true each time a click occurs
// areYouThere = true;
// console.log('user click - 60 sec timer til prompt');
// setInterval(() => {
// // switch varaibale to false
// areYouThere = false;
// console.log('Are you still there? prompt initiated');
// // alert user if variable is false
// if (!areYouThere) {alert("Are you still there");};
// areYouThere = true;
// console.log('user returned - 60 sec timer til prompt');
// }, 600000);
// };
// document.body.addEventListener('click', promptTimeout);
//!! Change the container DOM
const changeContainer = (value) => {
container.innerHTML = `${value}`;
console.log('container changing to...');
$(document).ready(function(){
$('select').formSelect();
});
}
//!! Load product overview view when different functions are complete
const loadProducts = (prod) => {
holder = '';
for (let i = 0; i < prod.length; i++) {
let product = prod[i];
holder += `
<div class="col s12 m12 l6 xl6">
<div class="card grey darken-4">
<div class="card-content white-text">
<span class="card-title" style="font-size:20px;">${product.name}</span>
<p>${product.description}</p>
<br>
<p>Price: ${product.price}</p>
</div>
<div class="card=action">
<a class="waves-effect waves-light btn" style="margin:20px;" onClick="viewDetail(${product.id})">View Details</a>
</div>
</div>
</div>
`
}
changeContainer(holder);
console.log('products overview view');
}
//!! Search Functionality
let searchProduct = () => {
let searchInput = document.querySelector('#search').value;
let myRegEx = new RegExp(`${searchInput}`, 'gi');
let holderArray = [];
for (let i = 0; i < products.length; i++) {
let productName = products[i].name;
let productDesc = products[i].description;
// search for regex match in name and description
if(productName.match(myRegEx) || productDesc.match(myRegEx)) {
holderArray.push(products[i])
}
}
loadProducts(holderArray);
console.log('search results');
}
//!! Shopping Cart
let changeQuant = (name, id) => {
let newQuant = document.querySelector(`#${id}`).value;
let sessionItem = sessionStorage.getItem(`${name}`);
let sessionArray = JSON.parse(sessionItem);
sessionArray[0] = newQuant;
sessionStorage.setItem(`${name}`, JSON.stringify(sessionArray));
viewCart();
}
let viewCart = () => {
holder = '<h2>Cart</h2>';
let totalCost = 0;
// loop through session storage to grab all add to cart items
for(let i = 0; i < sessionStorage.length; i++) {
let cartSeshItem = sessionStorage.getItem(sessionStorage.key(i));
if(cartSeshItem !== 'true') {
let parsedItem = JSON.parse(cartSeshItem);
let removeDollar = parsedItem[1].price.slice(1);
// to build drop down in view cart
let optionHolder = '';
let itemNameId = parsedItem[1].id;
// build options
for (let j = 1; j <= 10; j++) {
j == parsedItem[0] ? optionHolder += `<option value=${j} selected>${j}</option>`
: optionHolder += `<option value=${j}>${j}</option>`
}
holder += `
<h3>Product: ${parsedItem[1].name}</h3>
<p><strong>Price:</strong> ${parsedItem[1].price}</p>
<p><strong>Quantity:</strong><select id="select_${itemNameId}" onChange="changeQuant('${parsedItem[1].name}', this.id)">${optionHolder}</select>
<p><strong>Cost:</strong> $${parseFloat(Math.round(parseFloat(removeDollar) * parseFloat(parsedItem[0] * 100)) / 100).toFixed(2)}</p>
<button onClick="removeCartItem('${parsedItem[1].name}')">Remove From Cart</button>
`
// multiply the quantity and price for each
let multiplyQuantity = parseFloat(removeDollar) * parseInt(parsedItem[0]);
// then add to totalCost variable
totalCost += multiplyQuantity;
}
}
// maintain 2 decimal points with total
totalCost = parseFloat(Math.round(totalCost * 100) / 100).toFixed(2);
holder += `
<h2>Cart Total: $${totalCost}<button style="margin-left: 30px;" onClick="checkoutFunc(${totalCost})">Checkout</button></h2>
`
changeContainer(holder);
}
const removeCartItem = (item) => {
sessionStorage.removeItem(item);
console.log(`${item} removed from cart`);
viewCart();
}
//!! Checkout Functionality
const checkoutFunc = (cost) => {
holder = '';
holder += `
<h3>Cart Total: $${cost}</h3>
<form>
First name:
<br>
<input type="text" name="firstname" required>
<br>
Last name:
<br>
<input type="text" name="lastname" required>
<br>
Email:
<br>
<input type="email" name="email" required>
<br>
<button>Place Order</button>
</form>
`
changeContainer(holder);
console.log('checkout form dislpayed')
}
//!! View Details
let viewDetail = (num) => {
// loop through array to find product id of item clicked
let filterProduct = products.filter(x => x.id == num);
let product = filterProduct[0];
holder = ''
let eachRating = '';
// loop to store each rating in object
for (let i = 0; i < product.reviews.length; i++) {
eachRating += `<li class="collection-item avatar black">
<i class="material-icons circle">person</i>
<span class="title yellow-text">Rating: ${product.reviews[i].rating}</span>
<p>${product.reviews[i].description}</p>
</li>`
}
holder += `
<div style="display:flex; justify-content:space-around;">
<div style="display:flex;">
<div style="margin-top: 1.9466666667rem;">
<img src="${product.imgUrl}" alt="${product.name}"/>
<p>
<button class="red btn" onClick="loadProducts(products)">Hide Details</button>
</p>
</div>
<div style="margin-left: 50px;">
<h3>${product.name}</h3>
<p>${product.description}</p>
<p>Rating: ${product.rating}</p>
<p>Number of Reviews: ${product.reviews.length}</p>
<p>Price: ${product.price}</p>
<p>Category: ${product.category}</p>
<div class="row">
<div class="col s6">
<select id="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="col s6">
<button class="green btn" onClick="addToCart(${product.id})">Add To Cart</button>
</div>
</div>
</div>
</div>
<div>
<h5>Ratings:</h5>
<ul class="collection">
${eachRating}
</ul>
</div>
</div>
<script>
$(document).ready(function(){
$('select').formSelect();
});
</script>
`
changeContainer(holder);
console.log('product detail view');
}
//!! Reset functionality
const resetFunc = () => {
console.log('reset clicked');
// empty search input
document.querySelector('#search').value = '';
//reload all products
loadProducts(products);
}
//!! Add to cart
let addToCart = (num) => {
// grab quantity so it can be stored in sessionStorage
let quantityItem = document.querySelector('#quantity').value;
let cartItem = products.filter(x => x.id == num);
let cartItemName = cartItem[0].name;
let holderArray = [];
// set key to item name for storage
//* if item already exist in sessionStorage, the new addToCart will override
holderArray.push(quantityItem, cartItem[0]);
sessionStorage.setItem(`${cartItemName}`, JSON.stringify(holderArray));
console.log('item added to cart')
}
//!! Category Filter
let categoryFilter = (category) => {
console.log(`filter select for ${category}`);
let holderArray = [];
// load all products if all categories is selected
category == '' ? holderArray = products :
// loop through products array to find matching categories
products.forEach(val => {
if(category === val.category) {
holderArray.push(val)
}
});
loadProducts(holderArray);
console.log('category results');
}
// initial product load
loadProducts(products);