-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex-interface.js
80 lines (73 loc) · 3.14 KB
/
index-interface.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
import 'bootstrap/dist/css/bootstrap.min.css';
import { flavorDropdown } from './api_calls/dropdown.flavors.js';
import { effectsDropdown } from './api_calls/dropdown.effects.js';
import { flavorQuery } from './api_calls/query.flavors.js';
import { filterAll } from './api_calls/filter.all.js';
$(document).ready(function () { // jQuery Flavor dropdown
let flavors = flavorDropdown();
let effects = effectsDropdown();
let filter = filterAll();
// jQuery flavors dropdown
flavors.then(function (response) {
let body = JSON.parse(response);
let i = 0;
for (i = 0; i < body.length; i++) {
$('#flavor-choice').append(`<option value=${body[i]}>${body[i]}</option>`);
}
}, function (error) {
$('.showErrors').text(`There was an error processing your request: ${error.message}`);
});
// jQuery Effects dropdown
effects.then(function (response) {
let body = JSON.parse(response);
let i = 0;
for (i = 0; i < body.length; i++) {
if (body[i].type == "positive") {
$('#effect-choice').append(`<option value=${body[i].effect}>${body[i].effect}</option>`);
} else if (body[i].type == "medical") {
$('#type-choice').append(`<option value=${body[i].effect}>${body[i].effect}</option>`);
}
}
}, function (error) {
$('.showErrors').text(`There was an error processing your request: ${error.message}`);
});
/* * * * * * * * * * * FORM SUBMIT * * * * * * * * * * */
$('.strain-query').submit(function (event) {
event.preventDefault();
$("#table").empty();
$("#table").append(`<tr><th>Name</th><th>Race</th><th>Flavor</th><th>Positive Effects</th><th>Used For</th><th>More Info</th></tr>`);
let userFlavor = $('#flavor-choice option:selected').text();
let userPositiveEffect = $('#effect-choice option:selected').text();
let userMedicalEffect = $('#type-choice option:selected').text();
filter.then(function (response) {
let i = 0;
let body = JSON.parse(response);
//Regex function for Leafly URL endpoints
Object.keys(body).forEach(function (key) {
let name = key;
let race = body[key].race;
let flavor = body[key].flavors.toString().replace(/,/g, ', ');
let positive = body[key].effects.positive.toString().replace(/,/g, ', ');
let medical = body[key].effects.medical.toString().replace(/,/g, ', ');
let removeSymbols = `${name}`.replace(/[^(a-zA-Z)\d\s-]/g, "");
let parseName = removeSymbols.replace(/\s/g, '-').toLowerCase();
let leaflyUrl = `https://www.leafly.com/${race}/${parseName}`;
if ((flavor.includes(userFlavor) && positive.includes(userPositiveEffect) && medical.includes(userMedicalEffect))) {
$('#table').append(`<tr>
<td>${name}</td>
<td>${race}</td>
<td>${flavor}</td>
<td>${positive}</td>
<td>${medical}</td>
<td><a href=${leaflyUrl}><button class=btn btn-success> Learn More </button></a></td>
</tr>`);
} else {
$('#error').empty().append(`<h4>Please enter select an option in each box.</h4>`);
}
},
function (error) {
$('.showErrors').text(`There was an error processing your request: ${error.message}`);
})
}); //End of the document.ready function
});
});