Skip to content

Commit

Permalink
Filter books by publisher is done
Browse files Browse the repository at this point in the history
  • Loading branch information
kksrini89 committed Dec 29, 2019
1 parent 4721c99 commit 0bc2018
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 63 deletions.
33 changes: 33 additions & 0 deletions book.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getBookByIsbn, getNewBooks } from "./api.js";
import { populateBook, populatePublisher } from "./lib.js";

const book = {
query: "javascript",
page_no: 1,
book_list: [],
publisher_list: [],
async populate() {
// clear the books area
document.querySelector(".books-area").innerHTML = "";

// const { books } = await searchWithQuery(this.query, this.page_no);
const { books } = await getNewBooks();
console.log(books);
if (books && books.length) {
for (const book_info of books) {
if (book_info) {
// GET Book Meta Data Info
const bookResult = await getBookByIsbn(book_info.isbn13);
this.publisher_list.push(bookResult.publisher);
this.book_list.push(bookResult);
// To populate book area
populateBook(bookResult);
}
}
// To fill publishers dropdown values
populatePublisher([...new Set(this.publisher_list)]);
}
}
};

export default book;
71 changes: 43 additions & 28 deletions lib.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getNewBooks } from "./api.js";
// import { getNewBooks } from "./api.js";
import book from "./book.js";

const containerElement = document.querySelector(".books-area");
const applied_filter_tmpl = `<div>
Expand Down Expand Up @@ -31,22 +32,30 @@ export function populateBook(book_info) {
}
}

export function filterByPublisher(publisher = null) {
if (publisher) {
// clear books area container
containerElement.innerHTML = "";
getNewBooks().then(books => {
if (books && books.length) {
for (const book of books) {
if (book.publisher.toLowerCase() === publisher.toLowerCase()) {
populateBook(book);
}
}
}
});
}
/**
* Publisher dropdown - On click handler
*/
function selectPublisher(event) {
event.preventDefault();
console.log(this.text);

// Reset already selected option
const menuItems = document.querySelectorAll(
".publishers-filter a.dropdown-item"
);
menuItems.forEach(item => item.classList.remove("is-active"));

// add highlight option
this.classList.add("is-active");

filterByPublisher(this.text);
}

/**
* To create publisher dropdown item
* @param {string} name Publisher name
* @returns { HTMLElement }
*/
function createPublisherElement(name) {
if (name) {
const anchorTag = document.createElement("a");
Expand All @@ -64,6 +73,11 @@ function createPublisherElement(name) {
// );
}

/**
* To create book ui element
* @param {object} book
* @returns { HTMLElement }
*/
function createBookInfoElement(book = null) {
if (book) {
const book_info_tmpl = `<figure class="image book-figure">
Expand All @@ -80,18 +94,19 @@ function createBookInfoElement(book = null) {
}
}

function selectPublisher(event) {
event.preventDefault();
console.log(this.text);

// Reset already selected option
const menuItems = document.querySelectorAll(
".publishers-filter a.dropdown-item"
);
menuItems.forEach(item => item.classList.remove("is-active"));

// add highlight option
this.classList.add("is-active");
// Utility methods
function filterByPublisher(publisher = null) {
if (publisher) {
// clear books area container
containerElement.innerHTML = "";
const { book_list } = book;

filterByPublisher(this.text);
if (book_list && book_list.length) {
for (const book of book_list) {
if (book.publisher.toLowerCase() === publisher.toLowerCase()) {
populateBook(book);
}
}
}
}
}
36 changes: 1 addition & 35 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// import { books } from "./books-data.js";
import { populatePublisher, populateBook } from "./lib.js";
import { getNewBooks, searchWithQuery, getBookByIsbn } from "./api.js";
import book from "./book.js";

if (navigator.serviceWorker) {
navigator.serviceWorker
Expand All @@ -9,36 +7,4 @@ if (navigator.serviceWorker) {
.catch(console.error);
}

const book = {
query: "javascript",
page_no: 1,
book_list: [],
publisher_list: [],
async populate() {
// clear the books area
document.querySelector('.books-area').innerHTML = '';

// const { books } = await searchWithQuery(this.query, this.page_no);
const { books } = await getNewBooks();
console.log(books);
if (books && books.length) {
this.book_list = books;
for (const book_info of books) {
if (book_info) {
// GET Book Meta Data Info
const bookResult = await getBookByIsbn(book_info.isbn13);
this.publisher_list.push(bookResult.publisher);

// To populate book area
populateBook(bookResult);
// this.book_list.push(book_info);
}
}
console.log(this.publisher_list);
// To fill publishers dropdown values
populatePublisher([...new Set(this.publisher_list)]);
}
}
};

book.populate();

0 comments on commit 0bc2018

Please sign in to comment.