Skip to content

Commit

Permalink
Merge pull request #15 from tya0/master
Browse files Browse the repository at this point in the history
added more JS for filtering
  • Loading branch information
tya0 committed Feb 25, 2016
2 parents 949e2df + a34a787 commit 0a35bdb
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 22 deletions.
71 changes: 63 additions & 8 deletions dev/scripts/script.js
Expand Up @@ -65,6 +65,8 @@ app.getBookInfo = function(authorData){
console.log(about);
}

<<<<<<< HEAD
=======
// Handlebars template to display books
app.displayBooks = function(){
$("#books").empty();
Expand All @@ -77,6 +79,7 @@ app.displayBooks = function(){


// Function to choose author/get name by clicking pictures
>>>>>>> 949e2df92c738c914b41a86453ba223212b693fe
app.selectAuthor = function(){
$("label").on("click", function(e){
e.preventDefault();
Expand All @@ -98,10 +101,16 @@ app.selectAuthor = function(){
$(".authorHeading h2").text(app.author);
});
}
<<<<<<< HEAD
//this will be the array we will store all the authors books
app.allBookArray = [];
=======


// Function to access list of author's books
>>>>>>> 949e2df92c738c914b41a86453ba223212b693fe
app.page = 1;
//app.bookArray is the general array that will store the array of books passed on to display from the filter function to display and from initial ajax call to display -
app.bookArray = [];

app.getBookList = function(){
Expand All @@ -121,31 +130,75 @@ app.getBookList = function(){
console.log(data);
var books = data.GoodreadsResponse.author.books.book;

<<<<<<< HEAD


if (data.GoodreadsResponse.author.books.end === data.GoodreadsResponse.author.books.total) {
books.forEach(function(val, i) {
app.allBookArray.push(val);
})

app.bookArray = app.allBookArray;

console.log(app.bookArray);
app.displayBooks();
=======
books.forEach(function(val, i){
app.bookArray.push(val);
})
// If the number at the end of the page is equal to the total number of books, then console.log the list of books
if (data.GoodreadsResponse.author.books.end === data.GoodreadsResponse.author.books.total) {
console.log(data.GoodreadsResponse.author.books);
// Otherwise, add one to app.page and run the function again.
>>>>>>> 949e2df92c738c914b41a86453ba223212b693fe
} else {
app.page++;
app.getBookList();
console.log(data.GoodreadsResponse.author.books.end);
};
console.log(app.bookArray);
});
};

// Filter books by rating
app.filterBooks = function(){
filteredBooksArray = [];
data.forEach(function(val, i){
if ((val.average_rating >= app.rating) && (val.average_rating <= app.rating+1)) {
filteredBooksArray.push(val);
app.filteredBooksByRating = [];
console.log("entered filterBooks")
// console.log(app.rating);
app.bookArray.forEach(function(val, i){
console.log(val);
if ((val.average_rating >= app.rating) && (val.average_rating <= app.rating+(0.99))) {
console.log("enter if")
console.log(val.average_rating);
app.filteredBooksByRating.push(val);
}
})
//app.displayBooks()
console.log(app.filteredBooksByRating);
if (app.filteredBooksByRating.length === 0 ){
$("#books").empty();
$(".books h3").show().text("No books found with this rating")
} else {
$(".books h3").hide();
app.bookArray = app.filteredBooksByRating;
app.displayBooks()
}

$("#showAll").on("click", function(e){
console.log("showall")
e.preventDefault();
$(".books h3").hide();
app.bookArray = app.allBookArray;
app.displayBooks();
})
}

app.displayBooks = function(){
console.log("entered displayBooks")
$("#books").empty();
var bookHtml = $("#authorTemplate").html();
var bookTemplate = Handlebars.compile(bookHtml);
app.bookArray.forEach(function(data, i){
$("#books").append(bookTemplate(data));
})
}

// Reset button for authors
Expand Down Expand Up @@ -177,9 +230,11 @@ app.starHover = function(){
})
.on("click", function(e){
e.preventDefault();
app.rating = $(this).data("rating")
app.rating = $(this).data("rating");
app.rating = parseInt(app.rating);
console.log(app.rating);
//app.filteredBooks();
app.bookArray = app.allBookArray;
app.filterBooks();
if ($(this).hasClass("selected") && ($(this).hasClass("hover")) ) {
$(".star").not(".hover").removeClass("selected");
}
Expand Down
29 changes: 15 additions & 14 deletions public/index.html
Expand Up @@ -8,14 +8,14 @@
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>
<script id="authorTemplate" type="x-handlebars-template">
// <div class="author">
// <h2>{{title}}</h2>
// <img src="{{image_url}}" alt="">
<div class="author">
<h2>{{title}}</h2>
<img src="{{image_url}}" alt="">

// <p>Average rating: {{average_rating}}</p>
// <p>Publication year: {{publication_year}}</p>
// <p>{{Description}}</p>
// </div>
<p>Average rating: {{average_rating}}</p>
<p>Publication year: {{publication_year}}</p>
<p>{{Description}}</p>
</div>
</script>
</head>
<body>
Expand Down Expand Up @@ -80,18 +80,19 @@ <h2></h2>
<section class="filters">
<div class="wrapper">
<div class="rating">
<i class="fa fa-star-o fa-2x star oneStar" data-rating="1"></i>
<i class="fa fa-star-o fa-2x star twoStar" data-rating="2"></i>
<i class="fa fa-star-o fa-2x star threeStar" data-rating="3"></i>
<i class="fa fa-star-o fa-2x star fourStar" data-rating="4"></i>
<i class="fa fa-star-o fa-2x star fiveStar" data-rating="5"></i>
<i class="fa fa-star-o fa-2x star oneStar" data-rating="1.00"></i>
<i class="fa fa-star-o fa-2x star twoStar" data-rating="2.00"></i>
<i class="fa fa-star-o fa-2x star threeStar" data-rating="3.00"></i>
<i class="fa fa-star-o fa-2x star fourStar" data-rating="4.00"></i>
<i class="fa fa-star-o fa-2x star fiveStar" data-rating="5.00"></i>
<a href="showAll" id="showAll">Show All</a>
</div>
</div>
</section>
<section>
<section class="books">
<div class="wrapper">
<h3></h3>
<div class="books" id="books">

</div>
</div>
</section>
Expand Down
74 changes: 74 additions & 0 deletions public/scripts/script.js
Expand Up @@ -65,6 +65,8 @@ app.getBookInfo = function (authorData) {
console.log(about);
};

<<<<<<< HEAD
=======
// Handlebars template to display books
app.displayBooks = function () {
$("#books").empty();
Expand All @@ -76,6 +78,7 @@ app.displayBooks = function () {
};

// Function to choose author/get name by clicking pictures
>>>>>>> 949e2df92c738c914b41a86453ba223212b693fe
app.selectAuthor = function () {
$("label").on("click", function (e) {
e.preventDefault();
Expand All @@ -94,9 +97,15 @@ app.selectAuthor = function () {
$(".authorHeading h2").text(app.author);
});
};
<<<<<<< HEAD
//this will be the array we will store all the authors books
app.allBookArray = [];
=======

// Function to access list of author's books
>>>>>>> 949e2df92c738c914b41a86453ba223212b693fe
app.page = 1;
//app.bookArray is the general array that will store the array of books passed on to display from the filter function to display and from initial ajax call to display -
app.bookArray = [];

app.getBookList = function () {
Expand All @@ -116,6 +125,22 @@ app.getBookList = function () {
console.log(data);
var books = data.GoodreadsResponse.author.books.book;

<<<<<<< HEAD
if (data.GoodreadsResponse.author.books.end === data.GoodreadsResponse.author.books.total) {
books.forEach(function (val, i) {
app.allBookArray.push(val);
});

app.bookArray = app.allBookArray;

console.log(app.bookArray);
app.displayBooks();
} else {
app.page++;
app.getBookList();
console.log(data.GoodreadsResponse.author.books.end);
};
=======
books.forEach(function (val, i) {
app.bookArray.push(val);
});
Expand All @@ -129,18 +154,60 @@ app.getBookList = function () {
console.log(data.GoodreadsResponse.author.books.end);
};
console.log(app.bookArray);
>>>>>>> 949e2df92c738c914b41a86453ba223212b693fe
});
};

// Filter books by rating
app.filterBooks = function () {
<<<<<<< HEAD
app.filteredBooksByRating = [];
console.log("entered filterBooks");
// console.log(app.rating);
app.bookArray.forEach(function (val, i) {
console.log(val);
if (val.average_rating >= app.rating && val.average_rating <= app.rating + 0.99) {
console.log("enter if");
console.log(val.average_rating);
app.filteredBooksByRating.push(val);
}
});
console.log(app.filteredBooksByRating);
if (app.filteredBooksByRating.length === 0) {
$("#books").empty();
$(".books h3").show().text("No books found with this rating");
} else {
$(".books h3").hide();
app.bookArray = app.filteredBooksByRating;
app.displayBooks();
}
$("#showAll").on("click", function (e) {
console.log("showall");
e.preventDefault();
$(".books h3").hide();
app.bookArray = app.allBookArray;
app.displayBooks();
});
};
app.displayBooks = function () {
console.log("entered displayBooks");
$("#books").empty();
var bookHtml = $("#authorTemplate").html();
var bookTemplate = Handlebars.compile(bookHtml);
app.bookArray.forEach(function (data, i) {
$("#books").append(bookTemplate(data));
});
=======
filteredBooksArray = [];
data.forEach(function (val, i) {
if (val.average_rating >= app.rating && val.average_rating <= app.rating + 1) {
filteredBooksArray.push(val);
}
});
//app.displayBooks()
>>>>>>> 949e2df92c738c914b41a86453ba223212b693fe
};
// Reset button for authors
Expand Down Expand Up @@ -170,8 +237,15 @@ app.starHover = function () {
}).on("click", function (e) {
e.preventDefault();
app.rating = $(this).data("rating");
<<<<<<< HEAD
app.rating = parseInt(app.rating);
console.log(app.rating);
app.bookArray = app.allBookArray;
app.filterBooks();
=======
console.log(app.rating);
//app.filteredBooks();
>>>>>>> 949e2df92c738c914b41a86453ba223212b693fe
if ($(this).hasClass("selected") && $(this).hasClass("hover")) {
$(".star").not(".hover").removeClass("selected");
}
Expand Down

0 comments on commit 0a35bdb

Please sign in to comment.