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

Rachel's Tv Browser #155

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ select {
.fail {
color: red;
}

#movie-name {
font-weight: bold;
}

li {
list-style: none;
padding-left: 20px;
font-weight: normal;
}
31 changes: 16 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<main>
<main>

<img src="./images/tvm_api.png">
<img src="./images/tvm_api.png">

<div class="browse">
<div id="search-form">
<input type="search" placeholder="Search for a title..." id="show-search">
<button>submit</button>
</div>
<div class="browse">

<select id="show-select">
<option value="">Select a show...</option>
</select>
<div id="search-form">
<input type="search" placeholder="Search for a title..." id="show-search">
<button id="submitBtn">submit</button>
</div>

<div id="show-detail"></div>
<select id="show-select">
<option value="">Select a show...</option>
</select>

</div>
</main>
<div id="show-detail">

<script src="js/tv_browser.js"></script>
</div>
</div>
</main>
<script src="js/tv_browser.js"></script>
</body>
</html>
</html>
113 changes: 113 additions & 0 deletions js/tv_browser.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,115 @@
// API Docs at:
// http://www.tvmaze.com/api

// make a new request
var request = new XMLHttpRequest();
var showDetail = document.querySelector('#show-detail');
var showSelect = document.querySelector('#show-select');
var optionSelect = document.querySelectorAll('option');
var option = document.querySelector('option');
var optionList = document.querySelector('li');

var resetOption = function(){
while (showSelect.childNodes.length > 1) {
showSelect.removeChild(showSelect.lastChild);
}
}

var createList = function(movieDataArray){
var ulTag = document.createElement('ul');
ulTag.setAttribute('id', 'movie-name');
ulTag.innerText = movieDataArray.show.name;

var liTag = document.createElement('li');
liTag.innerText = "Type: " +movieDataArray.show.type;
ulTag.appendChild(liTag);

liTag = document.createElement('li');
liTag.innerText = "Language: " +movieDataArray.show.language;
ulTag.appendChild(liTag);

liTag = document.createElement('li');
liTag.innerText = "Rating: " +movieDataArray.show.rating.average;
ulTag.appendChild(liTag);

liTag = document.createElement('li');
liTag.innerHTML = "Summary: " +movieDataArray.show.summary;
ulTag.appendChild(liTag);

showDetail.appendChild(ulTag);
};

var createOption = function(movieDataArray){
var optionMovieName = document.createElement('option');
optionMovieName.setAttribute('value', movieDataArray.show.name);
optionMovieName.innerText = movieDataArray.show.name;
showSelect.appendChild(optionMovieName);
}

//function to loop and show results in the DOM.
var loadMovieInfo = function(movieDataObj) {
showDetail.innerText= "";
// resetOption();
for(var i=0; i<movieDataObj.length; i++) {
var movieDataArray = movieDataObj[i];
createList(movieDataArray);
createOption(movieDataArray);
};
};

var responseHandler = function() {
// console.log("response text", this.responseText);
var response = JSON.parse( this.responseText );
console.log( response );
console.log("Movie name: "+response[0].show.name+" type: "+response[0].show.type);
//reset option list when new name searched.
resetOption();
loadMovieInfo(response);

var matchMovieName = false;
var selectHandler = function() {
this.setAttribute('selected', 'selected');
for(var j=0; j<response.length; j++)
if(matchMovieName === false) {
if(this.value === response[j].show.name){
matchMovieName = true;
showDetail.innerText = "";
createList(response[j]);
} else {
matchMovieName = false;
};
};
};


// for (var i = 0; i < showSelect.length; i++){
showSelect.addEventListener('change', selectHandler);
// };


console.log("status text", this.statusText);
console.log("status code", this.status);
};

var requestFailed = function(){
console.log("response text", this.responseText);
console.log("status text", this.statusText);
console.log("status code", this.status);
};

var doSubmit = function(event){
var input = document.querySelector('#show-search');
var endPoint = input.value;
var url = "http://api.tvmaze.com/search/shows?q="+endPoint;
input.value = "";
request.open("GET", url);
request.send();
};

document.querySelector('button').addEventListener('click', doSubmit);


// listen for the request response
request.addEventListener("load", responseHandler);

request.addEventListener("error", requestFailed);