-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
169 lines (140 loc) · 5.17 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
'use strict';
const EDAMAM_SEARCH_URL = 'https://api.edamam.com/search';
function formatQueryParams(params) {
const queryItems = Object.keys(params)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
return queryItems.join('&');
};
function getRecipes(query, limit=10) {
const params = {
'q': query,
'app_id': EDAMAM_APP_ID,
'app_key': EDAMAM_API_KEY,
'to': limit
};
const queryString = formatQueryParams(params)
const url = EDAMAM_SEARCH_URL + '?' + queryString;
fetch(url)
.then(response => {
if (response.ok) {
return response.json();
}
throw new Error(response.statusText);
})
.then(responseJson => displayResults(responseJson))
.catch(err => {
console.log(err);
$('#js-error-message').text(`Search limit exceeded: 5 searches per minute. ${err.message}.`);
});
};
function displayResults(responseJson) {
$('#results-list').empty();
$('#js-error-message').empty();
$('.site-preview').addClass('hidden');
if (responseJson.hits.length === 0) {
$('#js-error-message').text(`No one wants to eat that... Try something else!`);
} else {
for (let i = 0; i < responseJson.hits.length; i++) {
let ingredients = "";
let healthInfo = "";
let dietInfo = "";
for (let j = 0; j < responseJson.hits[i].recipe.ingredientLines.length; j++) {
ingredients = ingredients + `<li role="listitem">
<div class="items">
<p class="item">${responseJson.hits[i].recipe.ingredientLines[j]}</p>
</div>
</li>`
};
for (let j = 0; j < responseJson.hits[i].recipe.healthLabels.length; j++) {
healthInfo = healthInfo + `<li role="listitem">
<div class="items">
<p class="item">${responseJson.hits[i].recipe.healthLabels[j]}</p>
</div>
</li>`
};
for (let j = 0; j < responseJson.hits[i].recipe.dietLabels.length; j++) {
dietInfo = dietInfo + `<li role="listitem">
<div class="items">
<p class="item">${responseJson.hits[i].recipe.dietLabels[j]}</p>
</div>
</li>`
};
$('#results-list').append(
`<li role="listitem">
<div id="search-result" class="search-result">
<a href="${responseJson.hits[i].recipe.url}" target="_blank" role="link"><img src="${responseJson.hits[i].recipe.image}" class="recipe-img" alt="Image of recipe"></a>
<a href="${responseJson.hits[i].recipe.url}" target="_blank" role="link" class="recipe-label-a"><p class="recipe-label">${responseJson.hits[i].recipe.label}</p></a>
<p class="recipe-source">${responseJson.hits[i].recipe.source}</p>
<div class="expand-buttons">
<button type="button" id="show-ingredients" class="show-ingredients" data-ingredients=${i}" role="button">Ingredients +</button>
<button type="button" id="show-health-info" class="show-health-info" data-health-info=${i}" role="button">Diet Info +</button>
</div>
</div>
<div class="ingredients-container">
<section id="ingredients-${i}" class="ingredients hidden" role="region">
<ul id="ingredients-list" class="ingredients-list" role="list">
${ingredients}
</ul>
</section>
</div>
<div class="health-info-container">
<section id="health-info-${i}" class="health-info hidden" role="region">
<ul id="health-info-list" class="health-info-list" role="list">
${healthInfo}
</ul>
<ul id="diet-info-list" class="diet-info-list" role="list">
${dietInfo}
</ul>
</section>
</div>
</li>`
);
};
};
$('#results').removeClass('hidden');
};
function watchSearchForm() {
$('form').submit(event => {
event.preventDefault();
const searchTerm = $('#js-search-term').val();
const limit = $('#js-max-results').val();
getRecipes(searchTerm, limit);
$('header').css('margin-top', '2vh');
});
};
function watchShowIngredientsButton() {
$('#results-list').on('click', '#show-ingredients', event => {
let recipeNum = $(event.currentTarget).data('ingredients');
if (!$(this).find('#health-info-' + recipeNum.replace('"', '')).hasClass('hidden')) {
$(this).find('#health-info-' + recipeNum.replace('"', '')).addClass('hidden');
};
if (!$(this).find('#ingredients-' + recipeNum.replace('"', '')).hasClass('hidden')) {
$(this).find('#ingredients-' + recipeNum.replace('"', '')).addClass('hidden');
} else {
$(this).find('#ingredients-' + recipeNum.replace('"', '')).removeClass('hidden');
};
});
};
function watchShowHealthInfoButton() {
$('#results-list').on('click', '#show-health-info', event => {
let recipeNum = $(event.currentTarget).data('health-info');
if (!$(this).find('#ingredients-' + recipeNum.replace('"', '')).hasClass('hidden')) {
$(this).find('#ingredients-' + recipeNum.replace('"', '')).addClass('hidden');
};
if (!$(this).find('#health-info-' + recipeNum.replace('"', '')).hasClass('hidden')) {
$(this).find('#health-info-' + recipeNum.replace('"', '')).addClass('hidden');
} else {
$(this).find('#health-info-' + recipeNum.replace('"', '')).removeClass('hidden');
};
});
};
function watchHomeButton() {
$('h1').on('click', event => {
location.reload();
});
};
$(watchHomeButton);
$(watchShowIngredientsButton);
$(watchShowHealthInfoButton);
$(watchSearchForm);
console.log('app loaded');