-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
113 lines (70 loc) · 2.66 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
const app_id = "e95074c5"
const app_key = "40da165683bc54f45d0e3ffa9834cf4a"
const button = document.querySelector("#search")
const input = document.querySelector("#type")
const loader = document.querySelector("div.loader")
const recipeList = document.querySelector('.cards')
const gif = document.querySelector('#scroller')
const footer = document.querySelector('.footer')
// function to show/remove the animation
const animation = (astate) => {
gif.style = `display: ${astate == true ? 'block' : "none"}`
}
animation(true)
// function to show/remove the loader
const loading = (state) => {
loader.style = `display: ${state == true ? 'block' : "none"}`
}
loading(false)
// Api call
const getRecipe = async (query) => {
try {
loading(true)
animation(false)
footer.style = `display : none`
const endpoint = `https://api.edamam.com/api/recipes/v2?type=public&q=${query}&app_id=${app_id}&app_key=${app_key}`;
const response = await fetch(endpoint)
const { hits } = await response.json()
if (hits.length === 0)
{swal("No recipe found, SearchBox is Empty 👨🍳!", {
buttons: false,
timer: 3000,
});};
{ animation(true) } ;
{footer.style = `display : block`}
hits.map(({ recipe }) => {
console.log(recipe)
const { image, calories, label, url, yield, ingredientLines } = recipe
const ele = document.createElement('li')
ele.classList.add('cards_item')
ele.innerHTML = `
<div class="card">
<div class="card_image"><img src=${image} alt="mixed vegetable salad in a mason jar. " /></div>
<div class="card_content">
<h4 class="card_title">${label} <span class="orange">${calories.toFixed(0)} kcal</span></h4>
<br>
<span class="blue">Serving : </span>${yield} people
<br><br>
<h3>Ingredients : </h3>
<li> ${ingredientLines}</li>
<button class="card_btn orange"><a target="_blank" href=${url} >See more </a></button>
</div>
</div>
`
recipeList.appendChild(ele)
})
loading(false)
} catch (error) {
console.log(error)
loading(false)
animation(true)
}
};
const searchRecipie = () => {
// remove the previous results
recipeList.innerHTML = null
const query = input.value
getRecipe(query);
};
button.addEventListener('click', searchRecipie)
input.addEventListener('keydown', e => e.key === 'Enter' ? searchRecipie() : null)