-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetRandomCocktail.js
More file actions
171 lines (143 loc) · 7.55 KB
/
getRandomCocktail.js
File metadata and controls
171 lines (143 loc) · 7.55 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
let loadedCocktailNumber = 0;
let indexCocktail = 0;
let resultArea;
let resultMessage;
let resultAnimation;
let areaEmpty = true;
function getRandomCocktails() {
// Set all variable :
let resultIngredientsImage = null;
let resultRandom = null;
let resultImageContainer = null;
let resultImage = null;
let resultCocktailName = null;
let resultCocktailCategory = null;
let resultCocktailIngredients = null;
let resultCocktailAlcohol = null;
let resultIngredientsPath = "";
let resultIngredientsIterator = 0;
let resultCocktailInstruction = null;
let resultCocktailGlass = null;
let resultCocktailQuantity = null;
// Set all message :
let loadingMessage;
let changeMessage;
if (localStorage.getItem('lang') === 'fr') {
loadingMessage = "Préparation d\'une super recette...";
changeMessage = "Clique à nouveau pour découvrir un autre cocktail !";
} else {
loadingMessage = "Making some good receipt...";
changeMessage = "Click again to discover another cocktail !";
}
// All cocktail placeholder :
let categoryPlaceholder = "";
let glassPlaceholder = "";
let typePlaceholder = "";
let ingredientsPlaceholder = "";
let instructionsPlaceholder = "";
let videoPlaceholder = "";
if (localStorage.getItem('lang') === 'fr') {
categoryPlaceholder = "Catégorie";
glassPlaceholder = "Verre";
typePlaceholder = "Type";
ingredientsPlaceholder = "Ingrédients";
instructionsPlaceholder = "Instructions";
} else {
categoryPlaceholder = "Category";
glassPlaceholder = "Glass";
typePlaceholder = "Type";
ingredientsPlaceholder = "Ingredients";
instructionsPlaceholder = "Instructions";
}
// Change result message
resultMessage = document.getElementById("random-message");
resultMessage.innerHTML = loadingMessage;
// Define result area
resultArea = document.getElementById("random-container");
// Restart animation during loading
resultAnimation = document.getElementById('random-empty-result');
if (areaEmpty == false) { // If the result area is not empty
resultRandom = document.getElementById("resultRandom");
resultRandom.remove();
}
if (resultAnimation.classList.contains('random-hidden')) {
resultAnimation.classList.remove("random-hidden");
}
// Call API
fetch("https://www.thecocktaildb.com/api/json/v1/1/random.php")
.then(response => response.json())
.then(randomCocktail => {
// Prevent multi-API call
areaEmpty = false;
// Hide animation
resultAnimation.classList.add("random-hidden");
console.log(randomCocktail)
// Creating the different element of the result display :
resultRandom = document.createElement("div");
resultImage = document.createElement("img");
resultCocktailName = document.createElement("p");
resultCocktailCategory = document.createElement("p");
resultCocktailAlcohol = document.createElement("p");
resultCocktailGlass = document.createElement("p");
resultCocktailIngredients = document.createElement("p");
resultImageContainer = document.createElement("div");
resultCocktailInstruction = document.createElement("p");
// Adding class to elements :
resultRandom.classList.add("result-element");
resultImageContainer.classList.add("result-element-image-container");
resultImage.classList.add("result-element-image");
resultCocktailName.classList.add("result-element-title");
resultCocktailName.classList.add("visible");
resultCocktailCategory.classList.add("result-element-category");
resultCocktailAlcohol.classList.add("result-element-category");
resultCocktailGlass.classList.add("result-element-glass");
resultCocktailIngredients.classList.add("result-element-category");
resultCocktailInstruction.classList.add("result-element-instruction");
// Adding values :
resultRandom.setAttribute("id", "resultRandom");
resultImage.src = randomCocktail.drinks[indexCocktail].strDrinkThumb;
resultImage.style.borderRadius = "10px";
resultCocktailName.innerHTML = randomCocktail.drinks[indexCocktail].strDrink;
resultCocktailCategory.innerHTML = "<i class=\"fa-solid fa-boxes-stacked\"></i><span class=\n'coloredBold\'> " + categoryPlaceholder +" : </span>";
resultCocktailCategory.innerHTML += randomCocktail.drinks[indexCocktail].strCategory;
resultCocktailAlcohol.innerHTML = "<i class=\"fa-solid fa-wine-bottle\"></i><span class=\n'coloredBold\'> " + typePlaceholder + " : </span>";
resultCocktailAlcohol.innerHTML += randomCocktail.drinks[indexCocktail].strAlcoholic;
resultCocktailGlass.innerHTML = "<i class=\"fa-solid fa-martini-glass-citrus\"></i><span class=\n'coloredBold\'> "+ glassPlaceholder + " : </span>";
resultCocktailGlass.innerHTML += randomCocktail.drinks[indexCocktail].strGlass;
resultCocktailQuantity = randomCocktail.drinks[indexCocktail].strMeasure1;
resultCocktailGlass.innerHTML += " (" + resultCocktailQuantity + ")";
resultCocktailIngredients.innerHTML = "<i class=\"fa-solid fa-leaf\"></i><span class=\n'coloredBold\'> " + ingredientsPlaceholder + " : </span>";
resultCocktailInstruction.innerHTML = "<i class=\"fa-solid fa-paste\"></i><span class=\n'coloredBold\'> " + instructionsPlaceholder + " : </span>";
resultCocktailInstruction.innerHTML += randomCocktail.drinks[indexCocktail].strInstructions;
// Reset values and setting ingredients
resultIngredientsIterator = 0;
resultIngredientsPath = "";
if (randomCocktail.drinks[indexCocktail].strIngredient1 === null) { // Preventing errors
resultCocktailIngredients.innerHTML = "Not specified";
} else {
resultIngredientsIterator = 0;
do {
resultIngredientsIterator++;
resultIngredientsPath = randomCocktail.drinks[indexCocktail]['strIngredient' + resultIngredientsIterator];
if (resultIngredientsPath !== null) {
resultIngredientsImage = "http://www.thecocktaildb.com/images/ingredients/" + resultIngredientsPath.replace(' ', '%20') + ".png";
resultCocktailIngredients.innerHTML += '- ' + resultIngredientsPath + ' <img class = \"result-element-ingredient-image\" src= ' + resultIngredientsImage + '> <br>';
}
} while (resultIngredientsPath !== null && resultIngredientsIterator <= 15);
}
// Adding to element to structure
resultArea.appendChild(resultRandom);
resultRandom.appendChild(resultImageContainer);
resultImageContainer.appendChild(resultImage);
resultRandom.appendChild(resultCocktailName);
resultRandom.appendChild(resultCocktailCategory);
resultRandom.appendChild(resultCocktailAlcohol);
resultRandom.appendChild(resultCocktailGlass);
resultRandom.appendChild(resultCocktailIngredients);
resultRandom.appendChild(resultCocktailInstruction);
// Change message
resultMessage.innerHTML = changeMessage;
}).catch(error => {
throw(error);
});
}