-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (100 loc) · 4.03 KB
/
index.js
File metadata and controls
108 lines (100 loc) · 4.03 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
$(document).ready(function() {
var item, tile, author, publisher, bookLink, bookImg;
var outputList = document.getElementById("list-output");
var bookUrl = "https://www.googleapis.com/books/v1/volumes?q=";
var apiKey = "key=AIzaSyDtXC7kb6a7xKJdm_Le6_BYoY5biz6s8Lw";
var placeHldr = '<img src="https://via.placeholder.com/150">';
var searchData;
//listener for search button
$("#search").click(function() {
outputList.innerHTML = ""; //empty html output
document.body.style.backgroundImage = "url('')";
searchData = $("#search-box").val();
//handling empty search input field
if(searchData === "" || searchData === null) {
displayError();
}
else {
// console.log(searchData);
// $.get("https://www.googleapis.com/books/v1/volumes?q="+searchData, getBookData()});
$.ajax({
url: bookUrl + searchData,
dataType: "json",
success: function(response) {
console.log(response)
if (response.totalItems === 0) {
alert("no result!.. try again")
}
else {
$("#title").animate({'margin-top': '5px'}, 1000); //search box animation
$(".book-list").css("visibility", "visible");
displayResults(response);
}
},
error: function () {
alert("Something went wrong.. <br>"+"Try again!");
}
});
}
$("#search-box").val(""); //clearn search box
});
/*
* function to display result in index.html
* @param response
*/
function displayResults(response) {
for (var i = 0; i < response.items.length; i+=2) {
item = response.items[i];
title1 = item.volumeInfo.title;
author1 = item.volumeInfo.authors;
publisher1 = item.volumeInfo.publisher;
bookLink1 = item.volumeInfo.previewLink;
bookIsbn = item.volumeInfo.industryIdentifiers[1].identifier
bookImg1 = (item.volumeInfo.imageLinks) ? item.volumeInfo.imageLinks.thumbnail : placeHldr ;
item2 = response.items[i+1];
title2 = item2.volumeInfo.title;
author2 = item2.volumeInfo.authors;
publisher2 = item2.volumeInfo.publisher;
bookLink2 = item2.volumeInfo.previewLink;
bookIsbn2 = item2.volumeInfo.industryIdentifiers[1].identifier
bookImg2 = (item2.volumeInfo.imageLinks) ? item2.volumeInfo.imageLinks.thumbnail : placeHldr ;
// in production code, item.text should have the HTML entities escaped.
outputList.innerHTML += '<div class="row mt-4">' +
formatOutput(bookImg1, title1, author1, publisher1, bookLink1, bookIsbn) +
formatOutput(bookImg2, title2, author2, publisher2, bookLink2, bookIsbn2) +
'</div>';
console.log(outputList);
}
}
/*
* card element formatter using es6 backticks and templates (indivial card)
* @param bookImg title author publisher bookLink
* @return htmlCard
*/
function formatOutput(bookImg, title, author, publisher, bookLink, bookIsbn) {
// console.log(title + ""+ author +" "+ publisher +" "+ bookLink+" "+ bookImg)
var viewUrl = 'book.html?isbn='+bookIsbn; //constructing link for bookviewer
var htmlCard = `<div class="col-lg-6">
<div class="card" style="">
<div class="row no-gutters">
<div class="col-md-4">
<img src="${bookImg}" class="card-img" alt="...">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">${title}</h5>
<p class="card-text">Author: ${author}</p>
<p class="card-text">Publisher: ${publisher}</p>
<a target="_blank" href="${viewUrl}" class="btn btn-secondary">Read Book</a>
</div>
</div>
</div>
</div>
</div>`
return htmlCard;
}
//handling error for empty search box
function displayError() {
alert("search term can not be empty!")
}
});