Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thomas' TV-Browser #154

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ select {
.fail {
color: red;
}

.hidden{
display: none;
}
10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
<div class="browse">
<div id="search-form">
<input type="search" placeholder="Search for a title..." id="show-search">
<button>submit</button>
<div id="display"></div>
<button id="submit">submit</button>
</div>

<select id="show-select">
<option value="">Select a show...</option>
<select id="show-select" class="hidden">
<option value="" id="default-select"></option>
</select>

<div id="show-detail"></div>
Expand All @@ -26,4 +26,4 @@

<script src="js/tv_browser.js"></script>
</body>
</html>
</html>
84 changes: 82 additions & 2 deletions js/tv_browser.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,82 @@
// API Docs at:
// http://www.tvmaze.com/api
//Getting initial DOM elements
var select = document.getElementById('show-select');
var showDetail = document.getElementById('show-detail');

//Response Handler for initial search
var responseHandler = function() {
//Parse initial JSON input
var response = JSON.parse(this.responseText);
console.log(response);
displayNames(response);
};

//Function to display names after initial search
var displayNames = function(obj){
for (var i in obj){
var nameDisplay = document.createElement('option');
nameDisplay.innerText = obj[i]['show']['name']
nameDisplay.value = obj[i]['show']['id']
select.appendChild(nameDisplay)
}
}

//Response handler for selecting of movie
var movieHandler = function() {
var movie = JSON.parse(this.responseText);
console.log(movie);
displayMovie(movie);
}

//Function to display show details after movie selected
var displayMovie = function(obj){
var movieName = document.createElement('h2');
var movieImage = document.createElement('img');
var movieSummary = document.createElement('p');

movieName.innerText = obj.name;
movieImage.src = obj.image.medium;
movieSummary.innerHTML = obj.summary;

showDetail.appendChild(movieName);
showDetail.appendChild(movieImage);
showDetail.appendChild(movieSummary);
}

//error handler
var errorHandler = function(){
console.log(this)
}

//AJAX call for initial search
var initialSearch = function(event){
select.classList.toggle('hidden');
var input = document.querySelector('#show-search');
document.getElementById('default-select').innerText = `Shows matching ${input.value}`
var showSearch = " http://api.tvmaze.com/search/shows?q=" + input.value;
// make a new request
var request = new XMLHttpRequest();
// ready the system by calling open, and specifying the url
request.open("GET", showSearch);
//sending request
request.send();
// listen for the request response
request.addEventListener("load", responseHandler)
request.addEventListener("error", errorHandler);
}
document.querySelector('#submit').addEventListener('click', initialSearch);

//AJAX call for specific movie
var getMovie = function(event){
var input = document.querySelector('#show-select');
var showSelect = "http://api.tvmaze.com/shows/" + input.value;
// make a new request
var request = new XMLHttpRequest();
// ready the system by calling open, and specifying the url
request.open("GET", showSelect);
//sending request
request.send();
// listen for the request response
request.addEventListener("load", movieHandler)
request.addEventListener("error", errorHandler);
}
document.querySelector('#show-select').addEventListener('change', getMovie);