Skip to content

Commit

Permalink
cleanup and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
James Byess committed Jan 17, 2018
1 parent 1f24217 commit 0c38448
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"title":"Wat Mannen Willen","poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BNTE0MjdjZTQtZWU5Ny00MzhjLWI3Y2YtYzExNjA4OThiNDVhXkEyXkFqcGdeQXVyNjU1NzI0ODQ@._V1_SX300.jpg","year":"2015","imdbID":"tt3983640"},{"title":"Star Wars: Episode IV - A New Hope","poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BNzVlY2MwMjktM2E4OS00Y2Y3LWE3ZjctYzhkZGM3YzA1ZWM2XkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg","year":"1977","imdbID":"tt0076759"},{"title":"Star Wars: Episode VI - Return of the Jedi","poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BOWZlMjFiYzgtMTUzNC00Y2IzLTk1NTMtZmNhMTczNTk0ODk1XkEyXkFqcGdeQXVyNTAyODkwOQ@@._V1_SX300.jpg","year":"1983","imdbID":"tt0086190"}]
[]
23 changes: 13 additions & 10 deletions public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// see very bottom of function where we pass them in
(function(w, d) {

// create a global object to store search results etc in
// create a global object to store search results in
w.omdbGlobals = {};

// event listeners to for search form elements
Expand All @@ -15,11 +15,13 @@
form.addEventListener('submit', performSearchAjax);
}

// event listeners for the view favorites button
function viewFavoritesEventListeners() {
var showFavButton = d.getElementById('show-favorites');
showFavButton.addEventListener('click', viewFavorites);
}

// actually runs the ajax call for searching
function performSearchAjax() {
// build params object for extensibility later
event.preventDefault();
Expand All @@ -34,7 +36,6 @@

// onload is where we handle the results.
request.onload = function() {
console.log(request.responseText);
processResults(request.responseText);
};

Expand Down Expand Up @@ -102,7 +103,6 @@
// logic to see if the checkbox is checked already or not
function handleFavoriteChange(e) {
var checked = e.target.checked;
console.log(e.target.value);
if(checked) {
addToFavorites(e.target.value);
}
Expand All @@ -115,19 +115,20 @@
request.open('POST', '/favorites', true);
request.setRequestHeader('Content-Type', 'application/json');
request.onload = function() {
console.log('completed');
console.log('added to favorites');
}

request.send(JSON.stringify(payload));
}

// fires when a movie title is clicked
function showMetaInfo(id) {
// lookup movie by id in global store
var movieMeta = w.omdbGlobals[id];
console.log(movieMeta);

// build template
var metaTemplate = '<ul class="meta-info">'+
'<li class="meta-title">Title: '+movieMeta.title+'</li>' +
'<li class="meta-year">Year: '+movieMeta.year+'</li>' +
'<li class="meta-id">IMDB ID: '+movieMeta.imdbID+'</li>' +
'</ul>';
Expand All @@ -139,26 +140,26 @@

// fires when show favorites button is clicked
function viewFavorites() {

// fetch favorites data from api
var favReq = new XMLHttpRequest();
favReq.open('GET', '/favorites', true);
favReq.onload = function() {
var favHtml = [];

var favorites = JSON.parse(favReq.responseText);
console.log(favorites);

// loop over each result and make an html string out of it
favorites.forEach(function(fav) {
favHtml.push(createFavoriteItem(fav));
});
console.log(favHtml);

var favContainer = d.getElementsByClassName('favorites-titles')[0];
console.log(favContainer);
var favString = favHtml.join('');
favContainer.innerHTML = favString;
};
favReq.send();
}

// returns a string of html
function createFavoriteItem(values) {
var favTemplate = '<div class="favorite-item">'+
values.title +
Expand All @@ -167,7 +168,9 @@
return favTemplate;
}

// returns a string of html for results values
function createResultItem(values) {
// template for result listings
var template = '<div class="results-item">'+
'<div class="results-item__fav">'+
'<input type="checkbox" value="'+values.imdbID+'">' +
Expand Down
1 change: 0 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ app.post('/favorites', function(req, res) {
res.send("Error");
return;
}
console.log(req.body);

var data = JSON.parse(fs.readFileSync('./data.json'));
data.push(req.body);
Expand Down

0 comments on commit 0c38448

Please sign in to comment.