-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
171 lines (160 loc) · 5.69 KB
/
script.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
// images display on landing page
const img = document.querySelector(".carousel-img");
const cycleImages = [
"./images/bluedrink.jpg",
"./images/greendrink.jpg",
"./images/pinkdrink.jpg",
"./images/2drinks.jpg",
"./images/2drinksGreenAndRed.jpg",
];
// carousel
async function cycle() {
img.src = cycleImages[0];
await new Promise((r) => setTimeout(r, 2000));
img.src = cycleImages[1];
await new Promise((r) => setTimeout(r, 2000));
img.src = cycleImages[2];
await new Promise((r) => setTimeout(r, 2000));
img.src = cycleImages[3];
await new Promise((r) => setTimeout(r, 2000));
img.src = cycleImages[4];
await new Promise((r) => setTimeout(r, 2000));
cycle();
}
cycle();
function changeIcon() {
if (document.getElementById("favbtn").textContent == "favorite_border") {
document.getElementById("favbtn").textContent = "favorite";
} else if ((document.getElementById("favbtn").textContent = "favorite")) {
document.getElementById("favbtn").textContent = "favorite_border";
}
favorite();
}
// favorites to local storage function
if (!localStorage.getItem("favorites")) {
localStorage.setItem("favorites", JSON.stringify([]));
}
let currentDrink;
function favorite() {
let favoriteStorage = JSON.parse(localStorage.getItem("favorites"));
let removed = false; // if something is deleted it shouldnt save to local
for (let i = 0; i < favoriteStorage.length; i++) {
if (favoriteStorage[i].idDrink === currentDrink.idDrink) {
favoriteStorage.splice(i, 1); // affecting original arrays length
localStorage.setItem("favorites", JSON.stringify(favoriteStorage));
removed = true;
}
}
if (!removed) {
localStorage.setItem(
"favorites",
JSON.stringify([...favoriteStorage, currentDrink])
);
}
console.log("FAVORITE");
console.log(JSON.parse(localStorage.getItem("favorites")));
}
$(document).ready(function () {
$("#favbtn").on("click", function () {
localStorage.setItem("Favorites", $("#cocktail-name"));
localStorage.getItem("Favorites");
});
});
// API functions
var baseUrl = "https://www.thecocktaildb.com/api/json/v2/9973533/";
var apiKey = "9973533";
var searchInput = document.getElementsByClassName("input");
var searchBtn = document.getElementById("searchBtn");
var cocktailSearch = document.querySelector("cocktailSearch");
var searchResult = document.querySelector(".search-results");
searchResult.style.display = "none";
// fetching API
searchBtn.addEventListener("click", function (event) {
event.preventDefault();
searchResult.style.display = "block";
img.style.display = "none";
var userSearch = document.getElementById("cocktailSearch").value;
console.log(userSearch);
fetch(
`https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${userSearch}`
)
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error("NETWORK RESPONSE ERROR");
}
})
.then((data) => {
console.log(data);
displayCocktail(data);
currentDrink = data.drinks[0]; //only stores first index
})
.catch((error) => console.error("FETCH ERROR:", error));
});
// display card one.
// Gets the data from the drink index
function displayCocktail(data) {
// const cocktailDiv = document.getElementById("cocktail");
// cocktailDiv.innerText = "";
// const heading = document.createElement("h3");
// heading.innerText = cocktailName;
// cocktailDiv.appendChild(heading);
// cocktailDiv.appendChild(cocktailImg);
// const cocktailIngredients = document.createElement("dl");
// cocktailDiv.appendChild(cocktailIngredients);
// const getIngredients = Object.keys(cocktail)
// // the filter method creates an array from an existing array.
// // In this case the array of drinks
// //proper syntax: .filter(function(item))
// .filter(function (ingredient) {
// return ingredient.indexOf("strIngredient") == 0;
// })
// //Only display the ingredients that dont have a value of null
// .reduce(function (ingredients, ingredient) {
// if (cocktail[ingredient] != null) {
// ingredients[ingredient] = cocktail[ingredient];
// }
// return ingredients;
// }, {});
// for (let key in getIngredients) {
// let value = getIngredients[key];
// listItem = document.createElement("dt");
// listItem.innerHTML = value;
// cocktailIngredients.appendChild(listItem);
// }
// Display Card two
// using inner HTML to display data from the index
const cocktail = data.drinks[0];
const cocktailName = cocktail.strDrink;
const cocktailImg = document.createElement("img");
cocktailImg.src = cocktail.strDrinkThumb;
var cocktailEl = document.getElementById("cocktail1");
var str1 = `
<div class="col s6 push-s6 m6">
<div class="card large">
<h4>${cocktailName}</h4>
<img id="cardImg" src="${cocktailImg.src}" />
<div class="card-content">
<dl>
<dt>Type of Glass:</dt>
<dd>${data.drinks[0].strGlass}</dd>
<dt>Ingredients:</dt>
<dd>
${data.drinks[0].strIngredient1}, ${data.drinks[0].strIngredient2}, ${data.drinks[0].strIngredient3}, ${data.drinks[0].strIngredient4}
</dd>
<dt>Measurements:</dt>
<dd>${data.drinks[0].strMeasure1}, ${data.drinks[0].strMeasure2}, ${data.drinks[0].strMeasure3}, ${data.drinks[0].strMeasure4}</dd>
<dt>Instruction:</dt>
<dd>${data.drinks[0].strInstructions}</dd>
</dl>
</div>
<button class="favbtn">
<i onclick="changeIcon()" class="material-icons left" id="favbtn"
>favorite_border</i
>Favorite
</button>
</div>
</div>`;
cocktailEl.innerHTML = str1;
}