diff --git a/app.py b/app.py index 8b4d08e..b9e1613 100644 --- a/app.py +++ b/app.py @@ -2971,6 +2971,26 @@ def admin_mass_edit_categories(): "admin/mass_edit_categories.html", lists=lists, categories=categories ) +@app.route("/admin/list_items/") +@login_required +@admin_required +def admin_list_items_json(list_id): + l = db.session.get(ShoppingList, list_id) + if not l: + return jsonify({"error": "Lista nie istnieje"}), 404 + + items = [ + { + "name": item.name, + "quantity": item.quantity, + "purchased": item.purchased, + "not_purchased": item.not_purchased, + } + for item in l.items + ] + + return jsonify({"title": l.title, "items": items}) + @app.route("/healthcheck") def healthcheck(): diff --git a/static/js/admin_mass_categories.js b/static/js/admin_mass_categories.js index 7e1f2fd..009b2fa 100644 --- a/static/js/admin_mass_categories.js +++ b/static/js/admin_mass_categories.js @@ -1,4 +1,5 @@ document.addEventListener("DOMContentLoaded", function () { + // Inicjalizacja Tom Select dla wszystkich select[multiple] document.querySelectorAll('select[multiple]').forEach(function (el) { new TomSelect(el, { plugins: ['remove_button'], @@ -8,4 +9,76 @@ document.addEventListener("DOMContentLoaded", function () { dropdownParent: 'body' }); }); -}); \ No newline at end of file + + // Obsługa przycisków podglądu produktów + document.querySelectorAll(".preview-btn").forEach((btn) => { + btn.addEventListener("click", async () => { + const listId = btn.dataset.listId; + const modalTitle = document.getElementById("previewModalLabel"); + const productList = document.getElementById("product-list"); + + modalTitle.textContent = "Ładowanie..."; + productList.innerHTML = '
  • ⏳ Ładowanie produktów...
  • '; + + const modal = new bootstrap.Modal(document.getElementById("productPreviewModal")); + modal.show(); + + try { + const res = await fetch(`/admin/list_items/${listId}`); + const data = await res.json(); + + modalTitle.textContent = `🛒 ${data.title}`; + productList.innerHTML = ""; + + const purchasedList = document.createElement("ul"); + purchasedList.className = "list-group list-group-flush mb-3"; + + const notPurchasedList = document.createElement("ul"); + notPurchasedList.className = "list-group list-group-flush"; + + let hasPurchased = false; + let hasUnpurchased = false; + + data.items.forEach(item => { + const li = document.createElement("li"); + li.className = "list-group-item bg-dark text-white d-flex justify-content-between"; + li.innerHTML = ` + ${item.name} + + x${item.quantity} + `; + + if (item.purchased) { + purchasedList.appendChild(li); + hasPurchased = true; + } else { + notPurchasedList.appendChild(li); + hasUnpurchased = true; + } + }); + + if (hasPurchased) { + const h5 = document.createElement("h6"); + h5.textContent = "✔️ Kupione"; + productList.appendChild(h5); + productList.appendChild(purchasedList); + } + + if (hasUnpurchased) { + const h5 = document.createElement("h6"); + h5.textContent = "🚫 Niekupione / Nieoznaczone"; + productList.appendChild(h5); + productList.appendChild(notPurchasedList); + } + + if (!hasPurchased && !hasUnpurchased) { + productList.innerHTML = '
  • Brak produktów
  • '; + } + + } catch (err) { + modalTitle.textContent = "Błąd"; + productList.innerHTML = '
  • ❌ Błąd podczas ładowania
  • '; + } + }); + }); +}); diff --git a/static/js/list_items_on_list.js b/static/js/list_items_on_list.js new file mode 100644 index 0000000..fbff26a --- /dev/null +++ b/static/js/list_items_on_list.js @@ -0,0 +1,45 @@ +document.addEventListener("DOMContentLoaded", function () { + // Tom Select jak był + + // Obsługa kliknięcia "Podgląd" + document.querySelectorAll(".preview-btn").forEach((btn) => { + btn.addEventListener("click", async () => { + const listId = btn.dataset.listId; + const modalTitle = document.getElementById("previewModalLabel"); + const productList = document.getElementById("product-list"); + + modalTitle.textContent = "Ładowanie..."; + productList.innerHTML = '
  • ⏳ Ładowanie produktów...
  • '; + + const modal = new bootstrap.Modal(document.getElementById("productPreviewModal")); + modal.show(); + + try { + const res = await fetch(`/admin/list_items/${listId}`); + const data = await res.json(); + + modalTitle.textContent = `🛒 ${data.title}`; + productList.innerHTML = ""; + + if (data.items.length === 0) { + productList.innerHTML = '
  • Brak produktów
  • '; + } else { + data.items.forEach(item => { + const li = document.createElement("li"); + li.className = "list-group-item bg-dark text-white d-flex justify-content-between"; + li.innerHTML = ` + ${item.name} + + x${item.quantity} + `; + productList.appendChild(li); + }); + } + + } catch (err) { + modalTitle.textContent = "Błąd"; + productList.innerHTML = '
  • ❌ Błąd podczas ładowania
  • '; + } + }); + }); +}); diff --git a/templates/admin/mass_edit_categories.html b/templates/admin/mass_edit_categories.html index fa64fc0..b183d84 100644 --- a/templates/admin/mass_edit_categories.html +++ b/templates/admin/mass_edit_categories.html @@ -32,8 +32,10 @@ {{ lst.owner.username if lst.owner else "?" }} {{ lst.created_at.strftime('%Y-%m-%d') }} - 🔗 Otwórz +