-
Notifications
You must be signed in to change notification settings - Fork 2
/
drink.js
201 lines (152 loc) · 5.36 KB
/
drink.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
var submitBtn1 = document.querySelector("#submit");
var drinkNameSpan = document.querySelector(".drinkNameSpan");
var drinkIngrUL = document.querySelector(".drinkIngrUL");
var drinkInstSpan = document.querySelector(".drinkInstSpan");
var drinksavelist = document.querySelector('#drinklist');
var drinksaveBtn = document.querySelector('.drink-save');
var drinkarray=[];
var saveddrinkarray=[];
function drinkChoice() {
var d = document.getElementById("Drink");
var inputPosition = d.options[d.selectedIndex];
var drinkPref = inputPosition.value;
getCocktail(drinkPref);
}
function getCocktail(drinkPref) {
var requestUrl =
"https://www.thecocktaildb.com/api/json/v1/1/filter.php?a=" + drinkPref;
fetch(requestUrl)
.then(function (response) {
return response.json();
})
.then(function (data) {
displayCocktail(data);
});
}
function displayCocktail(cocktail) {
var num = Math.floor(Math.random() * cocktail.drinks.length);
let drinkSection = document.querySelector(".drink-name");
let drinkName = document.createElement("span");
drinkNameSpan.innerHTML = cocktail.drinks[num].strDrink;
drinkSection.appendChild(drinkName);
var cocktailname = cocktail.drinks[num].strDrink;
drinkarray.push(cocktailname);
let img = document.getElementById("drink-pic");
img.src = cocktail.drinks[num].strDrinkThumb;
var requestUrl2 =
"https://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=" +
cocktail.drinks[num].idDrink;
fetch(requestUrl2)
.then(function (response2) {
return response2.json();
})
.then(function (drink) {
drinkIngrUL.innerHTML = "";
console.log("test");
for (let i = 1; i < 16; i++) {
console.log(drink);
if (
drink.drinks[0][`strIngredient${i}`] == null &&
drink.drinks[0][`strMeasure${i}`] == null
) {
break;
}
if (drink.drinks[0][`strIngredient${i}`] == null) {
drink.drinks[0][`strIngredient${i}`] = "";
} else if (drink.drinks[0][`strMeasure${i}`] == null) {
drink.drinks[0][`strMeasure${i}`] = "";
}
let listEl = document.createElement("li");
listEl.innerHTML =
drink.drinks[0][`strMeasure${i}`] +
" " +
drink.drinks[0][`strIngredient${i}`];
drinkIngrUL.appendChild(listEl);
}
drinkInstSpan.innerHTML = drink.drinks[0].strInstructions;
});
}
function saveDrinkRecipe() {
var drinklength = drinkarray.length -1;
var title = drinkarray[drinklength];
console.log(title);
console.log (drinkarray.includes(title));
console.log(saveddrinkarray);
if (!saveddrinkarray.includes(title)){
var newdrink= document.createElement('button');
newdrink.textContent = title;
newdrink.setAttribute('name', title)
drinksavelist.appendChild(newdrink);
saveddrinkarray.push(title);
localStorage.setItem('drink recipes', JSON.stringify(saveddrinkarray));
}
};
function getsavedDrinkRecipe() {
var storeddrinkarray = JSON.parse(localStorage.getItem("drink recipes"));
console.log(title);
console.log (drinkarray.includes(title));
console.log(saveddrinkarray);
console.log(storeddrinkarray);
if (storeddrinkarray === null){
return
}
if (storeddrinkarray !== null) {
saveddrinkarray = storeddrinkarray;
console.log(saveddrinkarray);
}
if (saveddrinkarray !== ""){
for (var i = 0; i < saveddrinkarray.length; i++){
var title = saveddrinkarray[i];
var newdrink= document.createElement('button');
newdrink.textContent = title;
newdrink.setAttribute('name', title)
drinksavelist.appendChild(newdrink);
}}
};
function DrinksavedApi(event) {
var name = event.target.getAttribute ('name');
var requestUrl =
"https://www.thecocktaildb.com/api/json/v1/1/search.php?s=" +
name ;
fetch(requestUrl)
.then(function (response) {
return response.json();
})
.then(function (data) {
let drinkSection = document.querySelector(".drink-name");
let drinkName = document.createElement("span");
console.log(data)
console.log(data.drinks[0].strDrink)
drinkNameSpan.innerHTML = data.drinks[0].strDrink;
drinkSection.appendChild(drinkName);
let img = document.getElementById("drink-pic");
img.src = data.drinks[0].strDrinkThumb;
drinkIngrUL.innerHTML = "";
for (let i = 1; i < 16; i++) {
console.log(drink);
if (
data.drinks[0][`strIngredient${i}`] == null &&
data.drinks[0][`strMeasure${i}`] == null
) {
break;
}
if (data.drinks[0][`strIngredient${i}`] == null) {
data.drinks[0][`strIngredient${i}`] = "";
} else if (data.drinks[0][`strMeasure${i}`] == null) {
data.drinks[0][`strMeasure${i}`] = "";
}
let listEl = document.createElement("li");
listEl.innerHTML =
data.drinks[0][`strMeasure${i}`] +
" " +
data.drinks[0][`strIngredient${i}`];
drinkIngrUL.appendChild(listEl);
}
drinkInstSpan.innerHTML = data.drinks[0].strInstructions;
});
}
submitBtn1.addEventListener("click", drinkChoice);
diffDrink.addEventListener("click", drinkChoice);
drinksaveBtn.addEventListener('click',saveDrinkRecipe);
drinksavelist.addEventListener("click", DrinksavedApi);
getsavedDrinkRecipe()