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

Tamira-Mariana-Pipes-VideoStoreConsumer #12

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2471c25
made basic Movie model:
tvojnar Dec 18, 2017
9f01368
made basic collection for MOvieList
tvojnar Dec 18, 2017
2402ed6
make template
lmarianarp19 Dec 18, 2017
368a166
fixed bugs in Movie model and MovieList collection
tvojnar Dec 18, 2017
e1fdb65
Merge pull request #1 from lmarianarp19/html-table
lmarianarp19 Dec 18, 2017
34a8e14
added url to MovieList collection
tvojnar Dec 18, 2017
22fbf92
fixed url ; to ,
tvojnar Dec 18, 2017
93191d5
Merge pull request #2 from lmarianarp19/movie-model-collection
tvojnar Dec 18, 2017
2fd3b13
made MovieView
tvojnar Dec 18, 2017
dcef991
Merge pull request #3 from lmarianarp19/show-all-movies
tvojnar Dec 18, 2017
0d3478d
last push before switching branches
tvojnar Dec 18, 2017
dbf0a8a
Merge branch 'master' of https://github.com/lmarianarp19/VideoStoreCo…
tvojnar Dec 18, 2017
deb8f2e
Show list of movies
lmarianarp19 Dec 18, 2017
8b32d57
Merge pull request #4 from lmarianarp19/movie-list-view
lmarianarp19 Dec 18, 2017
e1988de
added search bar
tvojnar Dec 18, 2017
26d1941
can click on the search button and get into searchForMovie
tvojnar Dec 19, 2017
97d326b
can now pull out the form text in the searh input. End of the day on …
tvojnar Dec 19, 2017
7d4c094
can make a search query to the external api via the local api to get …
tvojnar Dec 19, 2017
7aa29f4
Merge pull request #5 from lmarianarp19/playing-with-api
tvojnar Dec 19, 2017
7de495c
can show search results
lmarianarp19 Dec 19, 2017
2a35445
status message appears if there are no results for the search
tvojnar Dec 19, 2017
3dc69e2
was checking if we already have the movie by making another api call,…
tvojnar Dec 19, 2017
1149936
only adds the 'add to library' button if you don't already have the m…
tvojnar Dec 20, 2017
a9ecf35
Merge pull request #6 from lmarianarp19/add-movie
tvojnar Dec 20, 2017
0cfc2f3
few changes to save in data base
lmarianarp19 Dec 20, 2017
30a6fc0
can make a post request to the api now. Doesn't do what we want on th…
tvojnar Dec 20, 2017
c329b53
can make a post request and get back a new instance of a movie from t…
tvojnar Dec 20, 2017
dcb6442
clean the results table
lmarianarp19 Dec 20, 2017
5f0cce7
failure case to save movie
lmarianarp19 Dec 20, 2017
1557208
added back functionality to display error message if no search result…
tvojnar Dec 20, 2017
90f2c7f
fixed .done for post request to correctly check if the object returne…
tvojnar Dec 20, 2017
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
99 changes: 86 additions & 13 deletions dist/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,88 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Backbone Baseline</title>
</head>
<body>
<main id="main-content">

</main>

<script src="/app.bundle.js"></script>

</body>
</html>
<head>
<meta charset="utf-8">
<title>movies</title>
</head>
<body>
<main id="main-content">
<section id="search-view">
<div id="status-messages"></div>
<form id="search-form">
<label for="title">Title</label>
<input id="search-input" name="title" type="text" placeholder="What movie are you looking for?"></input>
<button type="submit" class="button search-button" id="search-button">Search</button>
</form>
<section id="results-table", class="columns large-8 small-12 hide">
<h2>Results!</h2>
<div id="search-status"></div>
<table>
<thead>
<th>Image</th>
<th>Title</th>
<th>Overview</th>
<th>Release Date</th>
<th><button class="button" id='clear-search'>Clear Results</button>
</thead>
<tbody id="search-list">
</tbody>
</table>
</section>


</section>
<section class="columns large-8 small-12">
<h2>Movies!</h2>
<section id="list-movies">
<table>
<thead>
<th>Title</th>
<th>Release Date</th>
</thead>
<tbody id="movies-list">
</tbody>
</table>
</section>
</section>
</main>

<script src="/app.bundle.js"></script>
<script type="text/template" id="all-movies-template">
<td >
<%- title %>
</td>
<td>
<%- release_date %>
</td>
<td>
<button type="button" class="button">See Details</button>
</td>
</script>

<script type="text/template" id="search-results">
<td >
<img src="<%- image_url%>">
</td>
<td >
<%- title %>
</td>
<td>
<%- overview %>
</td>
<td>
<%- release_date %>
</td>
<td id="have-movie">
<!-- have ? 'have it' : 'have it' -->

<% if (have) { %>
You have this movie
<% } %>
<% if (!have) { %>
<button class="button" id="add-movie">Add to library</button>
<% } %>
</td>
</script>

</body>
</html>
32 changes: 31 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,40 @@ import './css/styles.css';
// Import jQuery & Underscore
import $ from 'jquery';
import _ from 'underscore';
import Backbone from 'backbone';

// Import models and collections
import Movie from 'models/movie'
import MovieList from 'collections/movie_list'
import MovieListView from './views/movie_list_view'
import SearchListView from './views/search_list_view'

// ready to go
$(document).ready(function() {

$('#main-content').append('<p>Hello World!</p>');
let bus = {};
bus = _.extend(bus, Backbone.Events);

const movieTemplate = _.template($('#all-movies-template').html());
const searchTemplate = _.template($('#search-results').html());

const moviesList = new MovieList();


const searchListView = new SearchListView({
el: $('#search-view'),
template: searchTemplate,
bus: bus,
movies: moviesList,
});

const movieListView = new MovieListView({
model: moviesList,
template: movieTemplate,
el: $('#list-movies'),
bus: bus,
});

moviesList.fetch();

});
9 changes: 9 additions & 0 deletions src/collections/movie_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Backbone from 'backbone';
import Movie from '../models/movie';

const MovieList = Backbone.Collection.extend({
model: Movie,
url: 'http://localhost:3000/movies',
}) // MovieList

export default MovieList
4 changes: 4 additions & 0 deletions src/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ aside label {
div {
display: inline;
}

.hide {
display: none;
}
/*
* {
border-style: solid;
Expand Down
9 changes: 9 additions & 0 deletions src/models/movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Backbone from 'backbone';

const Movie = Backbone.Model.extend({
// set the url so we can make a post request on a model
url: 'http://localhost:3000/movies',

});

export default Movie
42 changes: 42 additions & 0 deletions src/views/movie_list_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Backbone from 'backbone';
import MovieView from './movie_view';

const MovieListView = Backbone.View.extend({
initialize(params){
this.template = params.template;
this.bus = params.bus;

this.listenTo(this.model, 'update', this.render);

// listen for event from the searchView to check if the movie already exists in the local collection
this.listenTo(this.bus, 'lookForMovie', this.checkCollectionForMovie);
},
render() {
// console.log("inside render in movie list view");
// console.log( this.model);
this.model.each((movie) => {
const movieView = new MovieView({
model: movie,
template: this.template,
tagName: 'tr',
});
this.$('#movies-list').append(movieView.render().$el);
});
return this
}, // render
checkCollectionForMovie(movieData) {
let haveIt = false;
this.model.each((movie) => {
if (movie.get('title') === movieData.title && movie.get('release_date') === movieData.release_date ) {
haveIt = true;
console.log('we have the movie');
} // if
this.bus.trigger(`${movieData.title}${movieData.release_date}`, haveIt)
}) // .each


}, // checkbox

});

export default MovieListView;
16 changes: 16 additions & 0 deletions src/views/movie_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Backbone from 'backbone';
import Movie from '../models/movie';

const MovieView = Backbone.View.extend({
initialize(params) {
this.template = params.template;
}, // initialize
render() {
const compiledTemplate = this.template(this.model.toJSON());
this.$el.html(compiledTemplate)

return this;
}, // render
}) // MovieView

export default MovieView;
70 changes: 70 additions & 0 deletions src/views/search_list_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Backbone from 'backbone';
import $ from 'jquery';
import SearchView from './search_view';


const SearchListView = Backbone.View.extend({
initialize(params) {
this.template = params.template;
this.movies = params.movies;
this.bus = params.bus;
this.listenTo(this.bus, 'errorSavingMovie', this.renderStatus);
}, // initialize
statusMessage(message) {
this.$('#status-messages').empty();
this.$('#status-messages').append(`<p>${message}</p>`)
}, // statusMessage
events: {
'click #search-button': 'searchForMovie',
'click #clear-search': 'clearResults',
}, // events
searchForMovie(event) {
event.preventDefault();
// this allows us to access the text input in the search form
const title = this.$('#search-input').val();

// create the url to make the api request
const url = 'http://localhost:3000/movies?query=' + title ;
const encoded_url = encodeURI(url);

// make a call to the url using only jQuery
// this will make a call to the local API's movies index action, which will then make a call to the external API to get all the movies that match the search query
// const APIresponse = $.get(encoded_url)

$.get(encoded_url).done( (data) => {
const APIresponse = data;
if (APIresponse.length < 1) {
console.log('no results');
this.statusMessage(`No results found for ${title}`);
}
this.render(APIresponse)

})
}, // searchForMovie
render(results){
// select element in html and toggle class
$('#results-table').toggleClass('hide')
// make a new search view for each result in the search response.
for (var result of results) {
const searchView = new SearchView({
model: result,
template: this.template,
tagName: 'tr',
movies: this.movies,
bus: this.bus,
});
this.$('#search-list').append(searchView.render().$el);
}
return this
},//close render
clearResults(){
$('#results-table').toggleClass('hide');
$('#search-list').empty();
},//clearResults
renderStatus(errorMessage){
this.$('#search-status').empty();
this.$('#search-status').append(errorMessage)
}
}) //close view

export default SearchListView
74 changes: 74 additions & 0 deletions src/views/search_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Backbone from 'backbone';
import $ from 'jquery';
import Movie from '../models/movie';
// import MovieList from '../collections/movie_list'
const SearchView = Backbone.View.extend({
initialize(params){
this.template = params.template;
this.bus = params.bus;
this.movies = params.movies;
this.listenTo(this.model, 'update', this.render);
this.listenTo(this.bus,`${this.model.title}${this.model.release_date}`, this.setHave )
},
events: {
// trigger when user wants to add a movie to the rails DB
'click #add-movie' : 'makePost',
},
render(){
const movieData = {
title: this.model.title,
release_date: this.model.release_date}
this.doWeHaveMovie(movieData)
const compiledTemplate = this.template(this.model); //.toJSON());
this.$el.html(compiledTemplate)
return this
}, // render
doWeHaveMovie(movieData) {
this.bus.trigger('lookForMovie', movieData);
}, // doWeHaveMovie
setHave(haveIt) {
if (haveIt) {
this.model['have'] = true;
} else {
this.model['have'] = false;
} // if
}, // setHave
makePost(){

// from notes: $.post(url, formData, success: callback)

// set the url to make post request
const url = 'http://localhost:3000/movies';

// pull out the external_id from the views model to pass to the api
const ex_id = this.model['external_id'];

// make a post request to the api, passing the external Id to the API
// this request goes to the create at the movies controller
$.post(url, {movie: {external_id: ex_id }}).done((data) => {
console.log('data');
console.log(data);
if (data !== {}){
// when the API was success create a new movie model with this data
console.log('in if for $.post');
const newMovieDB = new Movie(data);
// add the new movie model to the movie list collection
this.movies.add(newMovieDB)
// change the add movie buttom to movie added
this.$('#have-movie').empty();
this.$('#have-movie').append('Movie added')
} else {
const noResults = `<p> Can not add movie to the library </p>`
this.bus.trigger('errorSavingMovie', noResults);
}

}).fail((data)=>{
// if the API calls fails, shows error message
const error = '<p> Error adding movie to the library</p>'
this.bus.trigger('errorSavingMovie', error);
})
}//makePost

});

export default SearchView