-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
204 lines (174 loc) · 4.71 KB
/
app.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
$(document).ready(function() {
window.onload = loadModal;
})
var userInput = ''
var baseURL = 'https://cors-anywhere.herokuapp.com/https://www.cannabisreports.com/api/v1.0/strains/'
var effectsFlavors = []
var effects = []
var flavors = []
$('.submit-button').click(completeSearch)
$(document).on('click', '.search-again', scrollToTop)
function getStrainData(){
$.ajax({
url: baseURL + userInput,
type: "GET",
dataType: "json",
headers: {
"x-api-key": "a1d66090af7e5d15f41a6007df11f4567ee91f3a"
},
success: function(data) {
if (data.data.length === 0) {
noStrainError(data)
} else {
$('#results').removeClass('loader-hide')
appendName(data);
appendImage(data);
checkGenetics(data);
var ucpc = data.data[0].ucpc
var ucpcURL = ucpc + "/effectsFlavors"
$.ajax({
url: baseURL + ucpcURL,
type: "GET",
dataType: "json",
headers: {
"x-api-key": "a1d66090af7e5d15f41a6007df11f4567ee91f3a"
},
success: function(data) {
if (!data.data) {
hideLoader(data);
emptyResults();
noStrainError(data);
} else {
renderData(data)
}
}
})
.then(function() {
scrollToResults()
})
}
},
error: function(error) {
var err = error.responseJSON.message
hideLoader();
$('.error-message').text(err)
}
});
}
function completeSearch(event){
event.preventDefault()
getInputAndResetPage()
getStrainData()
}
function getInputAndResetPage(){
getInput();
emptyResults();
showLoader();
emptySearchField();
}
function hideModal() {
$('#modal').hide()
}
function modalNoResponse() {
$('.modal-text').text("Sorry, you must be a valid Medical Marijuana patient or at least 21 years old.")
$('.open-buttons').hide()
}
function loadModal() {
$('#entrance-button').click(hideModal)
$('#exit-button').click(modalNoResponse)
}
function getInput() {
var getSearchQuery = $('.search-field')
userInput = 'search/' + (getSearchQuery.val()).replace(/ /g, "_")
}
function emptyResults() {
$('#test4').empty()
$('#results').addClass('loader-hide')
$('#test5').empty()
$('.error-message').empty()
}
function showLoader() {
$('.loader').removeClass('loader-hide')
}
function checkGenetics(data) {
if (!data.data[0].genetics.names) {
$('.card-text').text("Not Currently Available")
} else {
$('.card-text').text(data.data[0].genetics.names)
}
}
function appendName(data) {
$('.main-results-title').text(data.data[0].name)
}
function appendImage(data) {
$('.strain-image').attr('src', data.data[0].image)
}
function noStrainError(data) {
$('.error-message').text("Sorry, we currently do not have information available for this strain")
}
function hideLoader(data) {
$('.loader').addClass('loader-hide')
}
function scrollToResults() {
var container = $('body')
hideLoader()
container.scrollTop(
$('.leaf-border').offset().top - container.offset().top + container.scrollTop()
)
}
function emptySearchField() {
$('.search-field').val('');
}
function renderData(data) {
getAllEffectsAndFlavors(data)
sortAndDisplayFlavorsAndEffects()
$('.hide').removeClass()
}
function getAllEffectsAndFlavors(data){
$.each(data.data, function(key, value) {
effectsFlavors.push(key = {
key: formatKey(key),
value: formatValueToPercentage(value)
})
})
}
function formatKey(key){
return (key[0].toUpperCase() + key.slice(1)).replace(/_/g, " ")
}
function formatValueToPercentage(value){
return ((value / 10) * 100).toFixed(1) + '%'
}
function scrollToTop(){
$(window).scrollTop(0)
}
function getEffects(){
effects = []
for (var i = 0; i <= 5; i++) {
effects.push(effectsFlavors[i])
}
effects.push(effectsFlavors[12])
}
function getFlavors(){
flavors = []
for (var i = 6; i <= 11; i++) {
flavors.push(effectsFlavors[i])
}
}
function displayEffects(){
$.each(effects, function(key, value) {
$('#test4').append('<h5 class="results-para">' + this.key + ': ' + this.value + '</h5>')
$('#test4').append('<div class="progress result-data"><div class="determinate" style="width:' + this.value + '"></div></div>')
})
}
function displayFlavors(){
$.each(flavors, function(key, value) {
$('#test5').append('<h5 class="results-para">' + this.key + ': ' + this.value + '</h5>')
$('#test5').append('<div class="progress result-data"><div class="determinate" style="width:' + this.value + '"></div></div>')
})
}
function sortAndDisplayFlavorsAndEffects(){
getEffects()
getFlavors()
displayEffects()
displayFlavors()
}