2025-06-01 17:41:00 +02:00
|
|
|
import { formatCurrency } from './utils.js';
|
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
|
|
const booksContainer = document.getElementById('books-container');
|
|
|
|
const searchInput = document.getElementById('searchInput');
|
|
|
|
const sortSelect = document.getElementById('sortSelect');
|
|
|
|
|
|
|
|
loadBooks();
|
|
|
|
|
|
|
|
if (searchInput) {
|
|
|
|
searchInput.addEventListener('input', debounce(loadBooks, 300));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sortSelect) {
|
|
|
|
sortSelect.addEventListener('change', loadBooks);
|
|
|
|
}
|
|
|
|
});
|
2025-05-30 18:00:52 +02:00
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
async function loadBooks() {
|
|
|
|
const booksContainer = document.getElementById('books-container');
|
|
|
|
const searchInput = document.getElementById('searchInput');
|
|
|
|
const sortSelect = document.getElementById('sortSelect');
|
|
|
|
|
|
|
|
if (!booksContainer) return;
|
|
|
|
|
|
|
|
booksContainer.innerHTML = `
|
|
|
|
<div class="col-12 text-center py-5">
|
|
|
|
<div class="spinner-border text-primary" role="status">
|
|
|
|
<span class="visually-hidden">Ładowanie...</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
2025-05-30 18:00:52 +02:00
|
|
|
try {
|
2025-06-01 11:09:37 +02:00
|
|
|
const searchTerm = searchInput ? searchInput.value : '';
|
|
|
|
const sortBy = sortSelect ? sortSelect.value : 'default';
|
|
|
|
|
|
|
|
const response = await fetch(`/api/ksiazki?search=${encodeURIComponent(searchTerm)}&sort=${sortBy}`, {
|
|
|
|
credentials: 'include'
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Błąd ładowania książek');
|
|
|
|
}
|
|
|
|
|
|
|
|
const books = await response.json();
|
|
|
|
displayBooks(books);
|
2025-05-30 18:00:52 +02:00
|
|
|
} catch (error) {
|
2025-06-01 11:09:37 +02:00
|
|
|
console.error('Błąd:', error);
|
|
|
|
booksContainer.innerHTML = `
|
|
|
|
<div class="col-12 text-center py-5">
|
|
|
|
<div class="alert alert-danger">Wystąpił błąd podczas ładowania książek</div>
|
|
|
|
</div>
|
|
|
|
`;
|
2025-05-30 18:00:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
function displayBooks(books) {
|
|
|
|
const booksContainer = document.getElementById('books-container');
|
|
|
|
|
|
|
|
if (!booksContainer) return;
|
|
|
|
|
2025-05-30 18:00:52 +02:00
|
|
|
if (books.length === 0) {
|
2025-06-01 11:09:37 +02:00
|
|
|
booksContainer.innerHTML = `
|
|
|
|
<div class="col-12 text-center py-5">
|
|
|
|
<p>Brak książek spełniających kryteria wyszukiwania</p>
|
|
|
|
</div>
|
|
|
|
`;
|
2025-05-30 18:00:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2025-06-01 11:09:37 +02:00
|
|
|
|
|
|
|
booksContainer.innerHTML = '';
|
|
|
|
|
|
|
|
books.forEach(book => {
|
2025-06-01 17:41:00 +02:00
|
|
|
const price = formatCurrency(book.cena);
|
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
const bookCard = `
|
|
|
|
<div class="book-card">
|
|
|
|
<a href="/book.html?id=${book.id}" class="book-cover-link">
|
|
|
|
<div class="book-cover-container">
|
2025-05-30 18:00:52 +02:00
|
|
|
<img src="${book.obraz_url}" class="book-cover" alt="${book.tytul}">
|
2025-06-01 11:09:37 +02:00
|
|
|
<div class="book-overlay">
|
|
|
|
<h5>${book.tytul}</h5>
|
|
|
|
<p>${book.autor}</p>
|
2025-06-01 17:41:00 +02:00
|
|
|
<p class="price">${price}</p>
|
2025-06-01 11:09:37 +02:00
|
|
|
</div>
|
2025-05-30 18:00:52 +02:00
|
|
|
</div>
|
|
|
|
</a>
|
2025-06-01 11:09:37 +02:00
|
|
|
<button class="btn btn-sm btn-outline-primary w-100 mt-2 add-to-cart-btn" data-book-id="${book.id}">
|
|
|
|
<i class="bi bi-cart-plus me-1"></i> Dodaj do koszyka
|
|
|
|
</button>
|
2025-05-30 18:00:52 +02:00
|
|
|
</div>
|
2025-06-01 11:09:37 +02:00
|
|
|
`;
|
|
|
|
booksContainer.innerHTML += bookCard;
|
|
|
|
});
|
|
|
|
|
|
|
|
document.querySelectorAll('.add-to-cart-btn').forEach(button => {
|
|
|
|
button.addEventListener('click', function() {
|
|
|
|
const bookId = this.dataset.bookId;
|
|
|
|
addToCart(bookId);
|
|
|
|
});
|
|
|
|
});
|
2025-05-30 18:00:52 +02:00
|
|
|
}
|
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
async function addToCart(bookId) {
|
2025-06-01 17:41:00 +02:00
|
|
|
const response = await fetch('/api/check-auth');
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
if (!data.authenticated) {
|
|
|
|
window.location.href = '/login.html';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
try {
|
|
|
|
const response = await fetch('/api/add-to-cart', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
book_id: parseInt(bookId),
|
|
|
|
quantity: 1
|
|
|
|
}),
|
|
|
|
credentials: 'include'
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
alert('Książka została dodana do koszyka!');
|
|
|
|
} else {
|
|
|
|
const errorData = await response.json();
|
|
|
|
alert(`Błąd: ${errorData.message || 'Nie udało się dodać do koszyka'}`);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Błąd:', error);
|
|
|
|
alert('Wystąpił błąd podczas dodawania do koszyka');
|
|
|
|
}
|
|
|
|
}
|
2025-05-30 18:00:52 +02:00
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
function debounce(func, wait) {
|
|
|
|
let timeout;
|
|
|
|
return function(...args) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(() => func.apply(this, args), wait);
|
2025-05-30 18:00:52 +02:00
|
|
|
};
|
2025-06-01 11:09:37 +02:00
|
|
|
}
|