-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-code.js
166 lines (141 loc) · 4.68 KB
/
app-code.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
'use strict'
const searchURL = 'https://www.thecocktaildb.com/api/json/v1/1/search.php?s=';
const searchURL2 = 'https://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=';
const ingredientURLFront = 'https://www.thecocktaildb.com/images/ingredients/';
const ingredientURLEnd = '-Small.png';
const ingredientImageArray = [];
const ingredientArray = []
/* Functions for clearing or hiding what was previously displayed */
function fullReset() {
if (!$('.results').hasClass('hidden')) {
$('.results').addClass('hidden');
}
if (!$('.details').hasClass('hidden')) {
$('.details').addClass('hidden');
}
$('#drink-search').val('');
}
function clearDetails() {
$('.drink-name').empty();
$('.cocktail-photo').empty();
$('.ingredients').empty();
$('.instructions').empty();
}
/* Display functions */
function displayDrinkInfo(responseJson) {
clearDetails();
if (!$('.error-msg').hasClass('hidden')) {
$('.error-msg').addClass('hidden');
}
handleIngredients(responseJson);
$('.results').addClass('hidden');
$('.drink-name').append(`${responseJson.drinks[0].strDrink}`);
$('.cocktail-photo').append(`<img src='${responseJson.drinks[0].strDrinkThumb}'>`);
for (let j = 0; j < ingredientImageArray.length; j++) {
$('.ingredients').append(`
<li>
<img src='${ingredientImageArray[j]}'>
<p>${ingredientArray[j]}</p>
</li>`);
};
$('.instructions').append(`
<p>${responseJson.drinks[0].strInstructions}</p>`);
$('.details').removeClass('hidden');
}
function displayResults(responseJson) {
$('.details').addClass('hidden');
$('.results').empty();
if (!$('.error-msg').hasClass('hidden')) {
$('.error-msg').addClass('hidden');
}
if (responseJson.drinks === null) {
return $('.error-msg').removeClass('hidden');
}
for (let i = 0; i < responseJson.drinks.length; i++) {
const base = responseJson.drinks[i];
$('.results').append(`
<li>
<img src=${base.strDrinkThumb}>
<button class='result' onclick='getCocktailById(${base.idDrink})'>${base.strDrink}</button>
</li>`
);
};
$('.results').removeClass('hidden');
}
/* All the functions for calling the API */
function getCocktails(search) {
const url = searchURL + search;
fetch(url)
.then (response => {
if (response.ok) {
return response.json();
}
throw new Error(response.statusText);
})
.then(responseJson => displayResults(responseJson))
.catch(err => {
console.log(`${err.message}`);
});
}
function getCocktailById(id) {
const url = searchURL2 + id;
console.log(url)
fetch(url)
.then (response => {
if (response.ok) {
return response.json();
}
throw new Error(response.statusText);
})
.then(responseJson => displayDrinkInfo(responseJson))
.catch(err => {
console.log(`${err.message}`);
});
}
function getRandomCocktail() {
fetch('https://www.thecocktaildb.com/api/json/v1/1/random.php')
.then(response => {
if (response.ok) {
return response.json();
}
throw new Error(response.statusText);
})
.then(responseJson => displayDrinkInfo(responseJson))
.catch(err => {
console.log(`${err.message}`);
});
}
/* Handlers */
function handleIngredients(responseJson) {
ingredientArray.splice(0,ingredientArray.length);
ingredientImageArray.splice(0,ingredientImageArray.length);
for (let k = 1; k < 16; k++) {
const lastPart = 'strIngredient' + k;
const lastPart2 = 'strMeasure' + k;
if (responseJson.drinks[0][lastPart] != null) {
//generate image URLs
ingredientImageArray.push(ingredientURLFront + responseJson.drinks[0][lastPart] + ingredientURLEnd);
//generate ingredient name and respective amounts
if (responseJson.drinks[0][lastPart2] != null) {
ingredientArray.push(responseJson.drinks[0][lastPart2].trim() + ' ' + responseJson.drinks[0][lastPart].trim());
}
else {
ingredientArray.push(responseJson.drinks[0][lastPart]);
}
}
else {
k = 16;
}
};
}
/* Event listeners */
function watchForm() {
$('form').submit(event => {
event.preventDefault();
const searchTerm = $('#drink-search').val();
getCocktails(searchTerm);
});
}
$(function() {
watchForm();
});