-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
74 lines (64 loc) · 2.28 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const toggleSpinner = style => {
document.getElementById("loader").style.display = style;
}
const toggleSearchResult = style => {
document.getElementById("showBooks").style.display = style;
}
// load api data
const getBook = () => {
const inputField = document.getElementById("inputField")
const inputValue = inputField.value;
toggleSpinner("block")
toggleSearchResult("none")
fetch(`https://openlibrary.org/search.json?q=${inputValue}`)
.then(res => res.json())
.then(data => {
displayBooks(data)
})
.catch((err) => {
document.getElementById("notFound").style.display = "block"; // api error handling
console.log(err);
})
}
// display api data on html
const displayBooks = (data) => {
const bookContainer = document.getElementById("bookContainer");
const foundedData = document.getElementById("foundedData")
bookContainer.innerHTML = '';
const books = data.docs;
// console.log(books.length);
books.forEach(book => {
const {title, author_name, first_publish_year, cover_i, publisher} = book;
const div = document.createElement("div");
div.classList = "col-sm-6 col-lg-4"
div.innerHTML = `
<div id="bookBox" class="book-box">
<img class="shadow book-img" src="${`https://covers.openlibrary.org/b/id/${cover_i}-M.jpg`}" alt="">
<div class="book-info">
<h5 class="title">${title.slice(0, 10)}</h5>
<p>By: ${author_name}</p>
<p>Publish year: ${first_publish_year}</p>
<p class="mb-0">Publisher: ${publisher}</p>
</div>
</div>
`;
bookContainer.appendChild(div)
});
// founded book quantity shown here
if (books.length === 0) {
foundedData.innerHTML = `No results found!`;
document.getElementById("notFound").style.display = "block";
}
else {
foundedData.innerHTML = `${books.length} results shown from ${data.numFound}`
document.getElementById("notFound").style.display = "none";
}
toggleSpinner("none")
toggleSearchResult("block")
}
// make working search button on enter click
document.getElementById("inputField").addEventListener("keypress", function(event) {
if (event.key === "Enter") {
document.getElementById("searchBtn").click();
}
})