-
Notifications
You must be signed in to change notification settings - Fork 2
/
bookLink.js
36 lines (29 loc) · 1.06 KB
/
bookLink.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
function getBookArray() {
const bookArrayJSON = localStorage.getItem('bookArray');
if (!bookArrayJSON) return [];
return JSON.parse(bookArrayJSON);
}
function getBookById(id) {
const bookArray = getBookArray();
return bookArray.find(book => book.id === id);
}
function getBookIdFromUrl() {
const urlParams = new URLSearchParams(window.location.search);
const bookId = parseInt(urlParams.get('id'), 10);
return bookId;
}
function displayBookInfo(book) {
if (!book) return;
const bookImage = document.getElementById('book-image');
const bookTitle = document.getElementById('book-title');
const bookAuthor = document.getElementById('book-author');
const bookSummary = document.getElementById('book-summary');
bookImage.src = book.image;
bookImage.alt = book.title;
bookTitle.textContent = book.title;
bookAuthor.textContent = `by ${book.author}`;
bookSummary.textContent = book.summary;
}
const bookId = getBookIdFromUrl();
const book = getBookById(bookId);
displayBookInfo(book);