diff --git a/Cargo.toml b/Cargo.toml index 1901332..14cdef3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ksiegarnia" -version = "0.1.0" +version = "0.9.0" edition = "2021" [dependencies] diff --git a/migrations/20250601094238_zmień_dostawe.sql b/migrations/20250601094238_zmień_dostawe.sql new file mode 100644 index 0000000..5267c38 --- /dev/null +++ b/migrations/20250601094238_zmień_dostawe.sql @@ -0,0 +1,3 @@ +ALTER TABLE zamowienia ALTER COLUMN typ_dostawy SET DEFAULT 'shipping'; + +UPDATE zamowienia SET typ_dostawy = 'shipping' WHERE typ_dostawy IS NULL; diff --git a/static/cart.html b/static/cart.html index d0f749b..196d8e6 100644 --- a/static/cart.html +++ b/static/cart.html @@ -58,19 +58,16 @@ 0.00 PLN - -
-
+
- +
12.99 PLN -
-
- +
+
Do zapłaty: diff --git a/static/js/book.js b/static/js/book.js index 513c6a5..cb8d6d1 100644 --- a/static/js/book.js +++ b/static/js/book.js @@ -1,3 +1,5 @@ +import { formatCurrency } from "./utils.js" + document.addEventListener('DOMContentLoaded', function() { const urlParams = new URLSearchParams(window.location.search); const bookId = urlParams.get('id'); @@ -38,9 +40,11 @@ async function loadBookDetails(bookId) { } function displayBookDetails(book) { + const price = formatCurrency(book.cena); + document.getElementById('book-title').textContent = book.tytul; document.getElementById('book-author').textContent = book.autor; - document.getElementById('book-price').textContent = `${book.cena} PLN`; + document.getElementById('book-price').textContent = price; document.getElementById('book-description').textContent = book.opis; const bookCover = document.getElementById('book-cover'); @@ -51,6 +55,14 @@ function displayBookDetails(book) { } async function addToCart(bookId) { + const response = await fetch('/api/check-auth'); + const data = await response.json(); + + if (!data.authenticated) { + window.location.href = '/login.html'; + return; + } + try { const response = await fetch('/api/add-to-cart', { method: 'POST', diff --git a/static/js/books.js b/static/js/books.js index 6186b84..faf7292 100644 --- a/static/js/books.js +++ b/static/js/books.js @@ -1,3 +1,5 @@ +import { formatCurrency } from './utils.js'; + document.addEventListener('DOMContentLoaded', function() { const booksContainer = document.getElementById('books-container'); const searchInput = document.getElementById('searchInput'); @@ -72,6 +74,8 @@ function displayBooks(books) { booksContainer.innerHTML = ''; books.forEach(book => { + const price = formatCurrency(book.cena); + const bookCard = `
@@ -80,7 +84,7 @@ function displayBooks(books) {
${book.tytul}

${book.autor}

-

${book.cena} PLN

+

${price}

@@ -101,8 +105,15 @@ function displayBooks(books) { }); } -// Funkcja dodająca do koszyka async function addToCart(bookId) { + const response = await fetch('/api/check-auth'); + const data = await response.json(); + + if (!data.authenticated) { + window.location.href = '/login.html'; + return; + } + try { const response = await fetch('/api/add-to-cart', { method: 'POST', diff --git a/static/js/cart.js b/static/js/cart.js index f3ed04f..072868a 100644 --- a/static/js/cart.js +++ b/static/js/cart.js @@ -8,7 +8,6 @@ document.addEventListener('DOMContentLoaded', function() { // Dodaj event listeners if (localPickupCheckbox) { - // JEDEN listener dla checkboxa localPickupCheckbox.addEventListener('change', function() { // Pobierz aktualny stan koszyka i zaktualizuj podsumowanie const cartItems = Array.from(document.querySelectorAll('.card.mb-3')).map(card => { @@ -60,6 +59,13 @@ async function loadCart() { const cartItems = await response.json(); displayCartItems(cartItems); + + // Ustaw checkbox jako odznaczony domyślnie (dostawa) + const localPickupCheckbox = document.getElementById('localPickup'); + if (localPickupCheckbox) { + localPickupCheckbox.checked = false; + } + updateSummary(cartItems); } catch (error) { console.error('Błąd ładowania koszyka:', error); @@ -179,7 +185,6 @@ async function updateCartItemQuantity(bookId, change) { } } -// Funkcja do aktualizacji podsumowania na podstawie DOM function updateSummaryFromDOM() { const cartItems = Array.from(document.querySelectorAll('.card.mb-3')).map(card => { const bookId = card.querySelector('.decrease-btn').dataset.bookId; @@ -218,8 +223,8 @@ function updateSummary(cartItems) { productsValueEl.textContent = productsValue.toFixed(2) + ' PLN'; - // Oblicz koszt dostawy - const deliveryCost = localPickupCheckbox && localPickupCheckbox.checked ? 0 : 12.99; + // Oblicz koszt dostawy: jeśli checkbox ZAZNACZONY to 0 (odbiór lokalny), w przeciwnym razie 12.99 (dostawa) + const deliveryCost = localPickupCheckbox && localPickupCheckbox.checked ? 12.99 : 0; deliveryValueEl.textContent = deliveryCost.toFixed(2) + ' PLN'; const totalValue = productsValue + deliveryCost; @@ -272,6 +277,7 @@ async function handleCheckout() { quantity: item.quantity })), total: parseFloat(document.getElementById('total-value').textContent), + // Odwrócona logika: checked = odbiór lokalny, unchecked = dostawa delivery_type: localPickupCheckbox.checked ? "local" : "shipping" }; diff --git a/static/js/profile.js b/static/js/profile.js index b162211..733f563 100644 --- a/static/js/profile.js +++ b/static/js/profile.js @@ -50,13 +50,27 @@ function displayOrderHistory(orders) { orders.forEach(order => { const orderDate = new Date(order.data_zamowienia).toLocaleDateString(); + + // Poprawione mapowanie typów dostawy + let deliveryType = order.typ_dostawy || 'shipping'; + let deliveryText = ''; + + if (deliveryType === 'local') { + deliveryText = 'Odbiór lokalny'; + } else if (deliveryType === 'shipping') { + deliveryText = 'Dostawa'; + } else { + // Dla starych zamówień bez typu + deliveryText = 'Dostawa'; + } + const orderElement = `
Zamówienie #${order.id} - ${orderDate}

Status: ${order.status || 'W realizacji'}

Suma: ${order.suma_totalna} PLN

-

Typ dostawy: ${order.typ_dostawy === 'local' ? 'Odbiór lokalny' : 'Dostawa'}

+

Typ: ${deliveryText}

Produkty:
@@ -78,3 +92,4 @@ function displayOrderHistory(orders) { orderHistoryContainer.innerHTML += orderElement; }); } +