-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
290 lines (247 loc) · 7.95 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
$(document).ready(function() {
// DECLARE SOME VARIABLES
// Get the buttons
let yummify = $('#yummify');
let reyummify = $('#reyummify');
// declare title, year, genre, poster, plot of chosen movie
let title = '';
let year = '';
let genre = '';
let poster = '';
var plot = '';
// variable to hold food type
let foodType = '';
// variable to hold plot sentiment
let movieSentiment = '';
// MODAL FUNCTIONALITY
// when modal is closed (either by x, esc, or by clicking a movie title)
$('#myModal').on('hidden.bs.modal', function() {
// remove items from modal list and show placeholder on search bar
$('.list-group').children().remove();
$('input').val('');
});
// generate show options search options
let toggleSearchResults = function() {
$('#myModal').modal('toggle');
};
// DECLARE SOME FUNCTIONS
// handle ajax errors
let ajaxErrorHandler = function(err) {
console.log('AJAX error', err);
};
// matches genre with food types
let foodMatches = {
Drama: 'pasta',
Biography: 'pie',
Western: 'mexican',
'History': 'steak',
Adventure: 'BBQ',
Comedy: 'wings',
Fantasy: 'pancakes',
Action: 'burger',
Documentary: 'pizza',
Romance: 'clams',
Horror: 'finger food',
Mystery: 'ice cream sundae',
Thriller: 'salad',
Animation: 'mac n cheese',
'Sci-Fi': 'milkshake',
Sport: 'nachos',
Crime: 'chinese'
};
let handleIngredientTabClick = function(event) {
if ($('.ingredientCard').hasClass('hide')) {
$('#ingredientTab').addClass('active');
$('#recipeTab').removeClass('active');
$('.ingredientCard').removeClass('hide');
$('.recipeCard').addClass('hide');
}
};
let handleRecipeTabClick = function(event) {
if ($('.recipeCard').hasClass('hide')) {
$('#recipeTab').addClass('active');
$('#ingredientTab').removeClass('active');
$('.recipeCard').removeClass('hide');
$('.ingredientCard').addClass('hide');
}
};
let getRecipe = function(event) {
// Reset to recipe pg
handleRecipeTabClick();
// getting first genre of movie
let genreToRecipe = genre.split(', ')[0];
// setting foodType, correcting for undefined genres
foodMatches[genreToRecipe] === undefined ? foodType = 'italian' : foodType = foodMatches[genreToRecipe];
$.ajax({
method: 'GET',
url: `https://api.edamam.com/search?q=${foodType}&app_id=1e590c2b&app_key=352ccf135e3d4e4ee7d8fb2195038326`,
dataType: 'json',
success: function(dataRecipes) {
$('#ingredientList').children().remove();
let recipeIndex = Math.floor(Math.random() * dataRecipes['hits'].length);
let recipe = dataRecipes['hits'][recipeIndex]['recipe'];
let label = recipe['label'];
let ingredientLines = recipe['ingredientLines'];
let source = recipe['source'];
let calories = recipe['calories'].toFixed(0);
let healthLabels = recipe['healthLabels'].join(', ');
let recipeImage = recipe['image'];
let yields = recipe['yield'];
let dietLabels = recipe['dietLabels'].join(', ');
let url = recipe['url'];
$('#recipeImage').attr("src", recipeImage);
$('#recipeTitle').text(label);
$('#recipeSource').text(`From ${source}`);
$('#recipeBtn').attr("href", url);
ingredientLines.forEach(ingred => {
$('#ingredientList').append(
`<li class="list-group-item">
${ingred}
</li>`);
});
$('#calories').text(`Calories: ${calories}`);
$('#yields').text(`Yield: ${yields}`);
$('#dietLabels').text(dietLabels);
},
error: ajaxErrorHandler
});
};
let handleMoviePosterClick = function(event) {
// display compare section
$('.compare').show();
$('.cuttingboard').show();
event.preventDefault();
$('html, body').animate({
scrollTop: $(".compare").offset().top
}, 500);
let id = $(event.target).attr("id");
$.ajax({
method: 'GET',
url: `http://omdbapi.com/?i=${id}&plot=short`,
dataType: 'json',
success: function(dataById) {
// remove present card
$('#movie').children().remove();
poster = dataById.Poster;
title = dataById.Title;
year = dataById.Year;
plot = dataById.Plot;
genre = dataById.Genre;
$('#movie').append(
`<div class="card">
<img class="card-img-top" src=${poster} alt="">
<div class="card-block">
<h5>${title} (${year})</h5>
<p class="card-text">${plot}</p>
</div>
</div>`);
// hide modal
toggleSearchResults();
$(function() {
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment/",
beforeSend: function(xhrObj) {
// Request headers
xhrObj.setRequestHeader("Content-Type", "application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", "644e5ed6f0a9434882158e6b91c70012");
xhrObj.setRequestHeader("Accept", "application/json");
},
type: "POST",
// Request body
data: `{
"documents": [{
"language": "en",
"id": "1",
"text": "${plot}"
}
]
}`
})
.done(function(dataSentiment) {
movieSentiment = 1 - dataSentiment['documents'][0]['score'];
if (movieSentiment < 0.2) {
$('.face img').attr('src', 'img/faces/1.png');
}
else if (movieSentiment < 0.4) {
$('.face img').attr('src', 'img/faces/3.png');
}
else if (movieSentiment < 0.6) {
$('.face img').attr('src', 'img/faces/5.png');
}
else if (movieSentiment < 0.8) {
$('.face img').attr('src', 'img/faces/7.png');
}
else if (movieSentiment < 1) {
$('.face img').attr('src', 'img/faces/9.png');
}
})
.fail(ajaxErrorHandler);
});
},
error: ajaxErrorHandler,
async: true
}).then(getRecipe);
};
let handleYummifySuccess = function(data) {
if (data.Error === 'Movie not found!') {
$('input').blur();
$('input').popover('show');
}
else {
let results = [];
let currentResult = {};
data.Search.forEach(result => {
currentResult = {
title: result.Title,
year: result.Year,
id: result.imdbID
};
results.push(currentResult);
});
results.forEach(show => {
$('.list-group').append(
`<li class="list-group-item" id=${show.id}>
${show.title} (${show.year})
</li>`
);
});
// show modal!
toggleSearchResults();
// create movie poster card!
$('.list-group-item').click(handleMoviePosterClick);
}
};
let getSentiment =
// EVENT LISTENERS
// listens for input click and hides popover
$('input').click(function() {
$('input').popover('hide');
});
// listener for ingredientTab click
$('#ingredientTab').click(handleIngredientTabClick);
// listener for recipeTab click
$('#recipeTab').click(handleRecipeTabClick);
// trigger yummify click with enter key
$('input').keydown(function(event) {
if (event.keyCode === 13) {
yummify.trigger('click');
}
});
// listener for yummify btn
yummify.click(event => {
// get user search
let userSearch = $('input').val();
// use omdbapi to get movie data & display array results in modal
$.ajax({
method: 'GET',
url: `http://omdbapi.com/?s=${userSearch}`,
dataType: 'json',
success: handleYummifySuccess,
error: ajaxErrorHandler
});
});
// listener for reyummify!
reyummify.click(event => {
getRecipe(genre);
});
});