-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtvmaze.js
More file actions
190 lines (159 loc) · 5.48 KB
/
tvmaze.js
File metadata and controls
190 lines (159 loc) · 5.48 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<<<<<<< HEAD
/** Handle search form submission:
* - hide episodes area
* - get list of matching shows and show in shows list
*/
// practice how to run function from the console
async function doAndShowSearch() {
let query = $("#search-query").val();
if (!query) return;
$("#episodes-area").hide();
let shows = await searchShows(query);
populateShows(shows);
}
$("#search-form").on("submit", function (evt) {
evt.preventDefault();
doAndShowSearch();
});
=======
>>>>>>> 977f38ecbfccf6ee08cde3df4c55ca68a0f99e63
/** Search Shows
* - given a search term, search for tv shows that
* match that query. The function is async show it
* will be returning a promise.
*
* - Returns an array of objects. Each object should include
* following show information:
* {
id: <show id>,
name: <show name>,
summary: <show summary>,
image: <an image from the show data, or a default image if no image exists,
(image isn't needed until later)>
}
*/
async function searchShows(query) {
let showResponse = await axios.get(
"http://api.tvmaze.com/singlesearch/shows",
{params:
{q: query}
});
// console.log(showResponse);
let {id, name, summary, image} = showResponse.data;
return [
{
id,
name,
summary,
image
}
]
}
/** Populate shows list:
* - given list of shows, add shows to DOM
*/
function populateShows(shows) {
const $showsList = $("#shows-list");
// Why commented out & kept?
// keep incase... debugging etc?
// $showsList.empty();
// check within showRespone.image for a url
// if there isn't make sure we dont break other cards
// use given URL OR default /missing-image.png
for (let show of shows) {
if (show.image.original === undefined) show.image.original = `/missing-image.png`
let $item = $(
`<div class="col-md-6 col-lg-3 Show" data-show-id="${show.id}">
<div class="card" data-show-id="${show.id}">
<img class="card-img-top" src="${show.image.original}">
<div class="card-body">
<h5 class="card-title">${show.name}</h5>
<p class="card-text">${show.summary}</p>
<button class="toggle-episodes">Episodes</button>
</div>
</div>
</div>
`);
$showsList.prepend($item);
}
}
$("#shows-list").on("click", ".toggle-episodes", handleShowEpisodes);
async function handleShowEpisodes(event) {
let showId = $(event.target).closest(".card").attr("data-show-id");
let episodeInfo = await getEpisodes(showId);
// Break
populateEpisodes(episodeInfo);
$("#episodes-area").show();
}
/** Handle search form submission:
* - hide episodes area
* - get list of matching shows and show in shows list
*/
// practice how to run function from the console
async function doAndShowSearch() {
let query = $("#search-query").val();
if (!query) return;
$('#shows-list').on("click", ".toggle-episodes" , handleShowEpisodes);
async function handleShowEpisodes(event) {
console.log(event.target)
let $showId = $(event.target).closest("div.card").attr("data-show-id")
console.log($showId)
await getEpisodes($showId)
let newEpisodeListing = createEpisodesListing($showId)
let episodeElements = populateEpisodes(newEpisodeListing)
// episodeElement contains array of elements
// for each episode, append
for (let episode of episodeElements){
$('#episodes-list').append(episode);
}
console.log(episodeElements)
// $('#episodes-list').append(episodeElements)// added through populate episodes
// toggles display on the episodes-area div
// $('.episodes-area').css("display", "inline-block" )
$('.episodes-area').show()
console.log(newEpisodeListing)
// append to episodes-list
// toggles display of #episodes-area
}
/** Given a show ID, return list of episodes:
* { id, name, season, number }
*/
async function getEpisodes(id) {
// TODO: get episodes from tvmaze
// you can get this by making GET request to
// http://api.tvmaze.com/shows/SHOW-ID-HERE/episodes
const episodeResponse = await axios.get(`http://api.tvmaze.com/shows/${id}/episodes`);
let episodesInfo = [];
for (let episode of episodeResponse.data) {
const {episodeID, name, season, number} = episode;
episodesInfo.push({episodeID, name, season, number});
}
return episodesInfo;
// take an id and plug in that id into the api call.
// take the response and assign it to a variable.
// create an array with each episode's id, name, season, episodeNumber as an object and return that array.
// TODO: return array-of-episode-info, as described in docstring above
}
async function createEpisodesListing(id) {
let episodeInfo = await getEpisodes(id);
populateEpisodes(episodeInfo);
// console.log(populateEpisodes(episodeInfo))
}
function populateEpisodes(episodesInfo) {
console.log("This is episodes info", episodesInfo);
$("#episodes-list").empty();
for (let episode of episodesInfo) {
console.log("loop");
let $newLi = $("<li>");
const {name, season, number} = episode;
$newLi.text(`${name} (season ${season}, episode ${number})`);
console.log($newLi);
let $epsList = $("#episodes-list")
console.log("this is episode list", $epsList);
$epsList.append($newLi);
}
return episodesToPopulate;
}
// iterate over the episodesInfo. For each episode,
// create a <li>${name} (season ${season}, episode ${number})</li>
// Take each new list tag and append it to the #episode-list.