forked from Technigo/project-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
242 lines (222 loc) · 7.32 KB
/
script.js
File metadata and controls
242 lines (222 loc) · 7.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*Here we have created two different arrays that you can work with if you want.
If you choose to create your own arrays with elements, just make sure that some
of the properties make sense to filter on, and some to sort on.*/
// Add filter for genre + sort for author, year and rating
const books = [
{
title: 'Babetta',
author: 'Nina Wähä',
year: 2022,
genre: 'fiction',
rating: 3.9,
description:
'A cinematic novel about loneliness, eeriness, our inability to connect, and our desire to simultaneously merge with each other.',
image: './books-images/babetta.jpeg'
},
{
title: 'The Copenhagen Trilogy',
author: 'Tove Ditlevsen',
year: 1971,
genre: 'fiction',
rating: 4.5,
description:
"A portrayal of love, friendship, art, ambition and the terrible lure of addiction, from one of Denmark's most celebrated twentieth-century writers",
image: './books-images/thecopenhagentrilogy.jpeg'
},
{
title: 'Den högsta kasten',
author: 'Carina Rydberg',
year: 1997,
genre: 'fiction',
rating: 4.4,
description:
'A book about obsession, betrayal, and revenge, and also a depiction of the morals of nighttime Stockholm.',
image: './books-images/den högsta kasten.jpeg'
},
{
title: 'My Brilliant Friend',
author: 'Elena Ferrante',
year: 2011,
genre: 'fiction',
rating: 4.5,
description:
'A story of violence: domestic and cultural, physical and emotional. A novel about two young girls exploring friendship and adolescence.',
image: './books-images/my brilliant friend.jpeg'
},
{
title: 'Frankenstein',
author: 'J.D. Salinger',
year: 1818,
genre: 'horror',
rating: 4.5,
description:
'The classic gothic horror novel which has thrilled and engrossed readers for two centuries.',
image: './books-images/frankenstein.jpeg'
},
{
title: 'Allegro Pastell',
author: 'Leif Randt',
year: 2023,
genre: 'fiction',
rating: 3.5,
description:
'A novel about an almost ordinary love and its transformations, reality and badminton, ideal relationships, happiness, politics, and what it means to be true to oneself."',
image: './books-images/allegro_pastell.jpeg'
},
{
title: 'A little life',
author: 'Hanya Yanagihara',
year: 2015,
genre: 'fiction',
rating: 4.8,
description:
"A powerful, disturbing novel. It's full of pain, desperation, and a sense of isolating sadness.",
image: './books-images/a-little-life.jpeg'
},
{
title: 'Norwegian Wood',
author: 'Haruki Murakami',
year: 2001,
genre: 'fiction',
rating: 4.0,
description:
'An intensely understated love story, simultaneously erotic and innocent, as straightforward and complex as love itself.',
image: './books-images/norwegian-wood.jpeg'
},
{
title: 'Just Kids',
author: 'Patti Smith',
year: 2010,
genre: 'memoir',
rating: 5.0,
description:
"Explores Patti Smith's life from youth through adulthood, hinting at her imaginative childhood and productive artistic life up to the present moment.",
image: './books-images/just-kids.jpeg'
},
{
title: 'Annie John',
author: 'Jamaica Kincaid',
year: 2016,
genre: 'fiction',
rating: 4.8,
description:
"A beautiful, poignant, and unsparing tale of a young girl's coming of age, the struggle to find and understand oneself, and how love and hatred can be just a small step apart.",
image: './books-images/anniejohn.jpeg'
},
{
title: 'Conversations with friends',
author: 'Sally Rooney',
year: 2017,
genre: 'fiction',
rating: 3.0,
description:
"A sharply intelligent novel about two college students and the strange, unexpected connection they forge with a married couple.",
image: './books-images/conversations-with-friends.jpeg'
},
{
title: 'Circe',
author: 'Madeline Miller',
year: 2018,
genre: 'fantasy',
rating: 4.9,
description:
"A novel that explores Circe's origin story and narrates Circe's encounters with mythological figures.",
image: './books-images/circe.jpeg'
}
]
const container = document.getElementById("container");
const filterDropDown = document.getElementById("filterDropDown");
const sortAuthorsButton = document.getElementById("sortAuthorsButton");
const sortYearButton = document.getElementById("sortYearButton");
const sortRatingButton = document.getElementById("sortRatingButton");
let sortDirection = "asc";
let sortCriteria = "author";
const loadBooks = (bookArray) => {
container.innerHTML = "";
bookArray.forEach((book) => {
container.innerHTML += `
<div class = "card">
<h2>${book.title}</h2>
<img src="${book.image}" alt="${book.title}" />
<p>${book.author}</p>
<p>${book.year}</p>
<p>${book.genre}</p>
<p>${book.rating}</p>
<p>${book.description}</p>
</div>
`;
});
};
const filterBooks = () => {
const value = filterDropDown.value;
if (value === "all") {
loadBooks(books);
} else {
const filteredList = books.filter((book) => book.genre === value);
loadBooks(filteredList);
}
}
const sortBooks = () => {
if (sortDirection === "asc") {
if (sortCriteria === "author") {
books.sort((a, b) => a.author.localeCompare(b.author));
} else if (sortCriteria === "year") {
books.sort((a, b) => a.year - b.year);
} else if (sortCriteria === "rating") {
books.sort((a, b) => b.rating - a.rating);
}
sortDirection = "desc";
} else {
if (sortCriteria === "author") {
books.sort((a, b) => b.author.localeCompare(a.author));
} else if (sortCriteria === "year") {
books.sort((a, b) => b.year - a.year);
} else if (sortCriteria === "rating") {
books.sort((a, b) => a.rating - b.rating);
}
sortDirection = "asc";
}
loadBooks (books);
};
filterDropDown.addEventListener("change", filterBooks);
sortAuthorsButton.addEventListener("click", () => {
sortCriteria = "author";
sortBooks();
sortAuthorsButton.textContent = `Author ${sortDirection === "asc" ? "A-Z" : "Z-A"}`;
});
sortYearButton.addEventListener("click", () => {
sortCriteria = "year";
sortBooks();
sortYearButton.textContent = `Year ${sortDirection === "asc" ? "(Newest to Oldest)" : "(Oldest to Newest)"}`;
});
sortRatingButton.addEventListener("click", () => {
sortCriteria = "rating";
sortBooks();
sortRatingButton.textContent = `Rating ${sortDirection === "asc" ? "(Highest to Lowest)" : "(Lowest to Highest)"}`;
});
const searchInput = document.getElementById("searchInput");
const searchButton = document.getElementById("searchButton");
const notFoundMessage = document.getElementById("notFoundMessage");
const searchBooks = () => {
const searchText = searchInput.value.toLowerCase();
const filteredList = books.filter((book) => {
return (
book.title.toLowerCase().includes(searchText) ||
book.author.toLowerCase().includes(searchText) ||
book.genre.toLowerCase().includes(searchText)
);
});
if (filteredList.length === 0) {
notFoundMessage.style.display = "block";
} else {
notFoundMessage.style.display = "none";
}
loadBooks(filteredList);
};
searchButton.addEventListener("click", searchBooks);
searchInput.addEventListener("keyup", (event) => {
if (event.key === "Enter") {
searchBooks();
}
});
loadBooks (books);