-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
152 lines (118 loc) · 3.32 KB
/
Copy pathscript.js
File metadata and controls
152 lines (118 loc) · 3.32 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
let c = document.getElementById("c"),
i = document.getElementById("i"),
b = document.getElementById("b");
let f1 = document.getElementById("f1"),
r1 = document.getElementById("r1"),
s1 = document.getElementById("s1"),
cat = document.getElementById("cat");
let favToggle = document.getElementById("favToggle"),
homeBtn = document.getElementById("homeBtn");
let msg = document.getElementById("msg");
let state = {
query: "bestseller books",
category: "all",
filter: "all",
rating: "all",
sort: "none",
};
let store = [];
let fav = JSON.parse(localStorage.getItem("fav")) || [];
let showFav = false;
function fetchBooks() {
let q = state.category !== "all" ? state.category + " books" : state.query;
fetch(`https://www.googleapis.com/books/v1/volumes?q=${q}&maxResults=30`)
.then((r) => r.json())
.then((x) => {
store = x.items || [];
apply();
});
}
function apply() {
let arr = showFav ? store.filter((x) => fav.includes(x.id)) : [...store];
if (state.filter === "free")
arr = arr.filter((x) => x.saleInfo?.saleability === "FREE");
if (state.filter === "paid")
arr = arr.filter((x) => x.saleInfo?.saleability === "FOR_SALE");
if (state.rating !== "all")
arr = arr.filter((x) => (x.volumeInfo?.averageRating || 0) >= state.rating);
if (state.sort === "az")
arr.sort((a, b) => a.volumeInfo.title.localeCompare(b.volumeInfo.title));
if (state.sort === "za")
arr.sort((a, b) => b.volumeInfo.title.localeCompare(a.volumeInfo.title));
render(arr);
}
function render(arr) {
c.innerHTML = "";
if (arr.length === 0) {
msg.innerHTML = showFav ? "No favourites" : "No results";
return;
}
msg.innerHTML = showFav ? "Showing Favorites" : "";
arr.forEach((bk) => {
let info = bk.volumeInfo || {};
let img =
info.imageLinks?.thumbnail || "https://via.placeholder.com/150x200";
let title = info.title || "No Title";
let author = info.authors ? info.authors.join(", ") : "Unknown";
let price = bk.saleInfo?.listPrice?.amount;
let priceText = price ? "₹" + price : "Free";
let d = document.createElement("div");
d.className = "b";
let heart = document.createElement("div");
heart.className = "heart";
heart.innerHTML = fav.includes(bk.id) ? "❤" : "♡";
if (fav.includes(bk.id)) heart.classList.add("pink");
heart.onclick = () => {
if (fav.includes(bk.id)) {
fav = fav.filter((x) => x !== bk.id);
} else {
fav.push(bk.id);
}
localStorage.setItem("fav", JSON.stringify(fav));
apply();
};
d.innerHTML = `
<img src="${img}">
<h4>${title.slice(0, 40)}</h4>
<p>${author.slice(0, 30)}</p>
<p class="price">${priceText}</p>
`;
d.appendChild(heart);
c.appendChild(d);
});
}
b.onclick = () => {
state.query = i.value || "bestseller books";
showFav = false;
fetchBooks();
};
cat.onchange = () => {
state.category = cat.value;
fetchBooks();
};
f1.onchange = () => {
state.filter = f1.value;
apply();
};
r1.onchange = () => {
state.rating = r1.value;
apply();
};
s1.onchange = () => {
state.sort = s1.value;
apply();
};
favToggle.onclick = () => {
showFav = true;
apply();
};
homeBtn.onclick = () => {
showFav = false;
fetchBooks();
};
function goHome() {
showFav = false;
state.query = "bestseller books";
fetchBooks();
}
fetchBooks();