forked from hamery93/Cinema-Sips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (136 loc) · 5.56 KB
/
script.js
File metadata and controls
171 lines (136 loc) · 5.56 KB
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
//Starting URL's for the API's. Search parameters to be added based on user choice.
var movieURL =
"https://api.themoviedb.org/3/discover/movie?api_key=5e90ef02ac18551f90f8c1c7cf3f5e91&language=en-US&with_genres=";
var cocktailURL = "https://www.thecocktaildb.com/api/json/v1/1/filter.php?i=";
//Search through movies in for loop - if genre = BLAH then print
//This function will be fed the user's choice from the dropdown list and return an array
//that contains the names of the liquor and movie genre that corresponds to that mood.
function delegateUserInput(mood) {
//this array variable will store 2 values: the liquor and movie genre that matches the mood selected by the user
var pairings = [];
//these if else statements push the liquor/genre corresponding to the given mood
if (mood === "happy") {
// pairings.push("comedy");
pairings.push("35");
pairings.push("tequila");
liquor = "tequila";
} if (mood === "sad") {
// pairings.push("drama");
pairings.push("18");
pairings.push("wine");
} if (mood === "scared") {
// pairings.push("horror");
pairings.push("27");
pairings.push("rum");
} if (mood === "romantic") {
// pairings.push("romantic comedy");
pairings.push("10749");
pairings.push("champagne");
} if (mood === "angry") {
// pairings.push("action");
pairings.push("28");
pairings.push("whiskey");
}
//returns the array when this function is called
return pairings;
}
//takes in an array containing liquor and movie genre strings.
//returns a complete api url for the cocktailsDB API.
function createDrinkAPI(array) {
// appends cocktailURL declared above with the liquor designated by user's mood choice and returns the completed string.
return cocktailURL + array[1];
}
//takes in an array containing liquor and movie genre strings.
//returns a complete api url for the omdb API.
function createMovieAPI(array) {
// appends movieURL declared above with the genre designated by user's mood choice and returns the completed string.
return movieURL + array[0];
}
//Takes in the completed cocktailDB URL and the array containing the delegated liquor and movie genre choices
//creates divs containing information and appends them to the page
function cocktailPair(apiUrl, array, i) {
var liquor = array[1];
//ajax call retrieves the information from the cocktailsDB API
$.ajax({
url: apiUrl,
method: "GET",
}).then(function (response) {
//variables created to store information on the drink in html elements
var pictureDiv = $("<div class='basic-card-image text-center'>");
var infoDiv = $(
"<div class ='basic-card-content content callout secondary' id='drink-paragraph'>"
);
var drinkName = $("<h5>" + response.drinks[i].strDrink + "</h5>");
var drinkImage = $("<img src='" + response.drinks[i].strDrinkThumb + "'>");
drinkImage.attr("style", "width: 200px; height: 200px;");
var liquorType = $("<p> Liquor Type: " + liquor + "</p>");
pictureDiv.append(drinkImage);
infoDiv.append(drinkName, liquorType);
//divs appended to the page
$("#cocktail-card").append(pictureDiv, infoDiv);
});
}
function moviePair(apiUrl, i) {
//ajax call retrieves the information from the cocktailsDB API
$.ajax({
url: apiUrl,
method: "GET",
}).then(function ({results}) {
var movies = [];
console.log(apiUrl)
console.log(results.map(result => result.title))
$("#movie-card").empty();
for (var i = 0; i < 3; i++){
var index = Math.floor(Math.random() * results.length);
console.log(index)
movies.push(results[index]);
results.splice(index, 1);
}
console.log(movies)
for (var i = 0; i < movies.length; i++){
var movieName
var movieImage
var moviePlot
var pictureDiv = $("<div class='basic-card-image text-center'>");
var infoDiv = $("<div class ='basic-card-content content callout secondary' id='movie-paragraph'>");
movieName = $("<h5>" + movies[i].title + "</h5>");
movieImage = $("<img src='http://image.tmdb.org/t/p/w185//" + movies[i].poster_path + "'>");
movieImage.attr("style", "width: 200px; height: 300px;");
moviePlot = $("<p>" + movies[i].overview + "</p>");
pictureDiv.append(movieImage);
infoDiv.append(movieName, moviePlot);
//divs appended to the page
$("#movie-card").append(pictureDiv, infoDiv);
}
$("#movie-card").append(pictureDiv, infoDiv);
});
}
function clear() {
$("#cocktail-card").empty();
$("#movie-card").empty();
}
function buttonClick(event) {
//clears previous choices
clear();
//retrieves value from the list
var mood = $(this).attr("id");
//chooses the liquor type and movie genre to match the mood
var drinkAndGenre = delegateUserInput(mood);
//feeds array holding genre and liquor type
//calls functions to build API URL's using user specified parameters
var drinkAPI = createDrinkAPI(drinkAndGenre);
var movieAPI = createMovieAPI(drinkAndGenre);
//calls functions that find movie and cocktail information and populates the page
//loops through 3 times to do 3 movies and 3 drinks
for(var i = 0; i < 3; i++){
cocktailPair(drinkAPI, drinkAndGenre, i);
}
moviePair(movieAPI, i);
//corrects for the 2 spellings of whiskey in the API so that all 3 are printed
if(drinkAndGenre[1] === "whiskey"){
var j=0;
cocktailPair(drinkAPI, ["fill", "whisky"], j);
}
}
//sets a click event for each possible mood
$("#happy, #happy a, #sad, #sad a, #angry, #angry a, #scared, #scared a, #romantic, #romantic a").on("click", buttonClick)