/
script.js
368 lines (299 loc) · 12.7 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
// step1: declare empty objects to store event ajax call, event handler
// ajax call
const makeupApp = {};
//event handler
const eventHandler = {};
//call back functions
const dataProcessor = {};
//generate 6 colors from each color arrays
dataProcessor.colorGenerator = function(array) {
let randomColor = [];
for (let i = 1; i <= 6; i++) {
let randomNum = Math.floor(Math.random() * array.length);
randomColor.push(array[randomNum]);
}
return randomColor;
}
//generate 1 product from product array
dataProcessor.productGenerator = function(array) {
let randomNum = Math.floor(Math.random() * array.length);
let randomProduct = array[randomNum];
return randomProduct;
}
//store color in local
let colorHolder = {
blush: [],
eyeshadow: [],
eyebrow: [],
lipstick: []
};
//store product in local
let productHolder = {
blush: [],
eyeshadow: [],
eyebrow: [],
lipstick: []
};
// step2: build api call so they can be called in event listener
makeupApp.url = 'https://makeup-api.herokuapp.com/api/v1/products.json'
makeupApp.getProducts = function(type, price) {
//filter user's choice of price
let priceFilter = null;
if (price == '$') {
priceFilter = {
price_less_than: 10
};
} else {
priceFilter = {
price_greater_than: 10
};
}
//define the query string of URL
return $.ajax({
url: makeupApp.url,
method: 'GET',
dataType: 'json',
data: {
product_type: type,
...priceFilter
}
});
}
// step3: filter the api data.
makeupApp.getColors = function(type, price) {
return makeupApp.getProducts(type, price)
.then(products => {
let colors = [];
for (let i in products) {
let colorCollection = products[i].product_colors;
colorCollection.map((color) => {
colors.push(color["hex_value"].split(','));
});
}
return colors.flat();
});
}
makeupApp.getProductsByColor = function(type, price, color) {
return makeupApp.getProducts(type, price)
.then(products => {
return products.filter(product => {
return product.product_colors.some((item) => {
return item.hex_value === color;
})
});
});
}
// step4: submit the first form to take user's input
eventHandler.submitProductFilter = function() {
$('form').on('submit', function(event) {
event.preventDefault();
//find the product type
let $checkBoxInputs = $('input[name = product-type]:checked');
let productTypes = $checkBoxInputs.map(number => $checkBoxInputs[number].value);
//find the product price
let $productPrice = $('input[name = product-price]:checked').val();
// clean the palette section inorder to print new result
eventHandler.initPalette();
// step4: call API with each product type as query string
for (let i = 0; i < productTypes.length; i++) {
makeupApp.getColors(productTypes[i], $productPrice)
//resolve the promise, get all the hex colors
.then((colors) => {
colorHolder[`${productTypes[i]}`] = colors;
//give alert if user didn't choose anything.
if (colors.length != 0) {
// randomize the color
let colorPalette = dataProcessor.colorGenerator(colors);
//print the color on page
colorPalette.map((color) => {
//give each radio an id, so they can attach to each label
let $radioId = "radio-" + productTypes[i] + colorPalette.indexOf(color);
//append radio button into palette
let $colorRadio = `<input type="radio" id=${$radioId} name=${productTypes[i]} value= ${color}>`;
//append label into palette
let $colorLabel = `<label for=${$radioId} class='color-circle'></label>`;
$(`.${productTypes[i]}-palette .palette-color`)
.append($colorRadio)
.append($colorLabel);
//change the label to each hex color
$(`.${productTypes[i]}-palette label[for=${$radioId}]`)
.css('background', color);
})
} else {
swal('Please choose at least one product!');
}
})
.fail((error) => {
console.log(error);
swal('Sorry, please refresh your browser and try again');
});
};
})
}
// step5: clean the current palette to generate the new one
eventHandler.initPalette = function() {
$('.palette-color').empty();
}
// step:6 add eventlistener to palette radio, to collect user's input of color
// print to SVG
eventHandler.printColorToFace = function() {
$('.palette-color').on('click', 'input', function() {
//find the value of clicked radio hex code
let inputColor = $(this).val();
let inputType = $(this).attr("name");
if (inputType === 'blush') {
$('.st2').css('fill', inputColor);
}
if (inputType === 'eyebrow') {
$('.st4').css('fill', inputColor);
}
if (inputType === 'lipstick') {
$('#lip').css('fill', inputColor);
}
if (inputType === 'eyeshadow') {
$('.st3').css('fill', inputColor);
}
})
}
// step7: add event listener to clear face button to clear the color
eventHandler.clearColor = function() {
$('button[type="reset"]').on('click', function() {
$('#lip').css('fill', 'none');
$('.st2').css('fill', 'none');
$('.st3').css('fill', 'none');
$('.st4').css('fill', 'none');
})
}
// step8: add eventlistener to 'give me another one' button
eventHandler.newPaletteGenerator = function() {
$('button[name="palette"]').on('click', function() {
//find the value of clicked button
let $anotherButton = $(this).val();
//clean the color-palette from last input
$(`.${$anotherButton}-palette .palette-color`).empty();
//find the color related to this button
let colorPalette = dataProcessor.colorGenerator(colorHolder[$anotherButton]);
colorPalette.map((color) => {
//give each radio an id, so they can attach to each label
let $radioId = "radio-" + $anotherButton + colorPalette.indexOf(color);
//append radio button into palette
let $colorRadio = `<input type="radio" name=${$anotherButton} id=${$radioId} value= ${color}>`;
//append label into palette
let $colorLabel = `<label for=${$radioId} class='color-circle'></label>`;
$(`.${$anotherButton}-palette .palette-color`)
.append($colorRadio)
.append($colorLabel);
//change the color of current label
$(`.${$anotherButton}-palette label[for=${$radioId}]`).css('background', color);
})
})
}
// step:9 add eventlistener to confirm button, to collect user's choice of color
eventHandler.confirmProduct = function() {
$('button[name = "confirm-button"]').on('click', function() {
//take the user's choice of colors
let $userBlush = $('.blush-palette input[type = "radio"]:checked').val();
let $userEyebrow = $('.eyebrow-palette input[type = "radio"]:checked').val();
let $userEyeshadow = $('.eyeshadow-palette input[type = "radio"]:checked').val();
let $userLipstick = $('.lipstick-palette input[type = "radio"]:checked').val();
//take the user's choice of types
let $checkBoxInputs = $('input[name = product-type]:checked');
let productTypes = $checkBoxInputs.map(number => $checkBoxInputs[number].value);
//take the user's choice of price
let $productPrice = $('input[name = product-price]:checked').val();
//loop through types that have been selected,
for (let i = 0; i < productTypes.length; i++) {
//takethe user's choice of color, under each type
let userColor;
if (productTypes[i] === 'blush') {
userColor = $userBlush;
} else if (productTypes[i] === 'eyebrow') {
userColor = $userEyebrow;
} else if (productTypes[i] === 'eyeshadow') {
userColor = $userEyeshadow;
} else if (productTypes[i] === 'lipstick') {
userColor = $userLipstick;
};
//call api
makeupApp.getProductsByColor(productTypes[i], $productPrice, userColor)
.then(products => {
// empty the array to wait for new call
productHolder[productTypes[i]].length = 0;
for (let j = 0; j < products.length; j++) {
productHolder[productTypes[i]].push(products[j]);
//random generate 1 product
for (let productType in productHolder) {
if (productHolder[productType].length > 0) {
let generatedProduct = dataProcessor.productGenerator(productHolder[productType]);
//print the result on page
let $brand = generatedProduct.brand;
let $name = generatedProduct.name;
let $price = generatedProduct.price;
let $currency = generatedProduct.currency;
let $imgUrl = generatedProduct.image_link;
let $link = generatedProduct.product_link;
$(`.${productType}-result .brand-name`).text($brand);
$(`.${productType}-result .product-name`)
.text($name)
.attr('href', $link);
$(`.${productType}-result img`).attr('src', $imgUrl);
$(`.${productType}-result .product-price`).text($price);
$(`.${productType}-result .product-price span`).text($currency);
}
}
}
})
.fail((error) => {
console.log(error);
swal('Sorry, please refresh your browser and try again');
});
}
})
}
// step 10: add eventlistener to new option button, to generate another new product
eventHandler.newProductGenerator = function() {
$('button[name = "result-button"]').on('click', function() {
let $currentType = $(this).val();
let productArray = productHolder[$currentType];
let generatedProduct = dataProcessor.productGenerator(productArray);
if (productArray.length === 1) {
swal("Sorry, this is the only product that holds this color.");
} else if (productArray.length === 0) {
swal("Please choose at least one product/color.");
}
let $brand = generatedProduct.brand;
let $name = generatedProduct.name;
let $price = generatedProduct.price;
let $currency = generatedProduct.currency;
let $imgUrl = generatedProduct.image_link;
let $link = generatedProduct.product_link;
$(`.${$currentType}-result .brand-name`).text($brand);
$(`.${$currentType}-result .product-name`)
.text($name)
.attr('href', $link);
$(`.${$currentType}-result img`).attr('src', $imgUrl);
$(`.${$currentType}-result .product-price`).text($price);
$(`.${$currentType}-result .product-price .currency`).text($currency);
})
}
// step11: init the page once refreshed
// step12: add eventlistener to confirm button, to collect user's choice of color
$(function() {
eventHandler.submitProductFilter();
eventHandler.newPaletteGenerator();
eventHandler.printColorToFace();
eventHandler.clearColor();
eventHandler.confirmProduct();
eventHandler.newProductGenerator();
});
// scroll functions
$('.submit-btn').click(function() {
$('html, body').animate({
scrollTop: $('.palette').offset().top
}, 1000);
});
$('.confirm-btn').click(function() {
$('html, body').animate({
scrollTop: $('.result').offset().top
}, 1000);
});