-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
87 lines (74 loc) · 2.67 KB
/
Copy pathui.js
File metadata and controls
87 lines (74 loc) · 2.67 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
import { addToFavorites, removeFromFavorites, getFavorites } from './storage.js';
const statusDiv = document.getElementById('status');
const resultsDiv = document.getElementById('results');
const favWrap = document.getElementById('fav-results');
export function setStatus(message, type = 'info') {
statusDiv.textContent = message || '';
statusDiv.style.color = type === 'error' ? 'crimson' : '#0ea5e9';
}
export function clearResults() {
resultsDiv.innerHTML = '';
}
export function renderBooks(docs) {
clearResults();
if (!docs.length) {
setStatus('No results found.', 'error');
return;
}
docs.forEach((b) => resultsDiv.appendChild(createBookCard(b)));
}
function createBookCard(b) {
const coverId = b.cover_i;
const cover = coverId
? `https://covers.openlibrary.org/b/id/${coverId}-M.jpg`
: 'https://via.placeholder.com/128x193?text=No+Cover';
const title = b.title || 'No Title';
const authors = (b.author_name || []).join(', ') || 'Unknown Author';
const year = b.first_publish_year || '';
const id = b.key || `${title}-${authors}`;
const card = document.createElement('article');
card.className = 'book-card';
card.innerHTML = `
<div class="book-card__img">
<img src="${cover}" alt="Cover: ${title}" loading="lazy">
</div>
<div class="book-card__info">
<h3>${title} ${year ? `(${year})` : ''}</h3>
<p>${authors}</p>
<button class="fav-btn"> Add to favorite</button>
</div>
`;
card.querySelector('.fav-btn').addEventListener('click', () => {
const { added } = addToFavorites({ id, title, authors, year, cover });
setStatus(added ? 'Added to favorite' : 'Book already in favorites', added ? 'info' : 'error');
renderFavorites();
});
return card;
}
export function renderFavorites() {
if (!favWrap) return;
const favs = getFavorites();
if (!favs.length) {
favWrap.innerHTML = '<p> No favorites added yet </p>';
return;
}
favWrap.innerHTML = favs.map(f => `
<article class="book-card">
<div class="book-card__img">
<img src="${f.cover}" alt="Cover: ${f.title}" loading="lazy">
</div>
<div class="book-card__info">
<h3>${f.title} ${f.year ? `(${f.year})` : ''}</h3>
<p>${f.authors}</p>
<button class="remove-fav" data-id="${f.id}">Remove</button>
</div>
</article>
`).join('');
favWrap.querySelectorAll('.remove-fav').forEach(btn => {
btn.addEventListener('click', () => {
removeFromFavorites(btn.dataset.id);
renderFavorites();
setStatus('Removed from favorites', 'info');
});
});
}