diff --git a/static/js/product_suggestion.js b/static/js/product_suggestion.js index 56efd97..0c1af4c 100644 --- a/static/js/product_suggestion.js +++ b/static/js/product_suggestion.js @@ -1,73 +1,91 @@ +function bindSyncButton(button) { + button.addEventListener('click', function (e) { + e.preventDefault(); + + const itemId = button.getAttribute('data-item-id'); + button.disabled = true; + + fetch(`/admin/sync_suggestion/${itemId}`, { + method: 'POST', + headers: { + 'X-Requested-With': 'XMLHttpRequest' + } + }) + .then(response => response.json()) + .then(data => { + showToast(data.message, data.success ? 'success' : 'danger'); + + if (data.success) { + button.innerText = '✅ Zsynchronizowano'; + button.classList.remove('btn-outline-primary'); + button.classList.add('btn-success'); + } else { + button.disabled = false; + } + }) + .catch(() => { + showToast('Błąd synchronizacji', 'danger'); + button.disabled = false; + }); + }); +} + +function bindDeleteButton(button) { + button.addEventListener('click', function (e) { + e.preventDefault(); + + const suggestionId = button.getAttribute('data-suggestion-id'); + const row = button.closest('tr'); + const itemId = button.getAttribute('data-item-id'); + const nameBadge = row?.querySelector('.badge.bg-primary'); + const itemName = nameBadge?.innerText.trim().toLowerCase(); + + button.disabled = true; + + fetch(`/admin/delete_suggestion/${suggestionId}`, { + method: 'POST', + headers: { + 'X-Requested-With': 'XMLHttpRequest' + } + }) + .then(response => response.json()) + .then(data => { + showToast(data.message, data.success ? 'success' : 'danger'); + + if (!data.success || !row) { + button.disabled = false; + return; + } + + const isProductRow = typeof itemId === 'string' && itemId !== ''; + const cell = row.querySelector('td:last-child'); + if (!cell) return; + + if (isProductRow) { + cell.innerHTML = ``; + const syncBtn = cell.querySelector('.sync-btn'); + if (syncBtn) bindSyncButton(syncBtn); + } else { + cell.innerHTML = 'Usunięto synchronizacje'; + } + }) + .catch(() => { + showToast('Błąd usuwania sugestii', 'danger'); + button.disabled = false; + }); + }); +} + document.addEventListener("DOMContentLoaded", function () { document.querySelectorAll('.sync-btn').forEach(btn => { - btn.replaceWith(btn.cloneNode(true)); - }); - document.querySelectorAll('.delete-suggestion-btn').forEach(btn => { - btn.replaceWith(btn.cloneNode(true)); - }); - - document.querySelectorAll('.sync-btn').forEach(btn => { - btn.addEventListener('click', function (e) { - e.preventDefault(); - - const itemId = this.getAttribute('data-item-id'); - const button = this; - button.disabled = true; - - fetch(`/admin/sync_suggestion/${itemId}`, { - method: 'POST', - headers: { - 'X-Requested-With': 'XMLHttpRequest' - } - }) - .then(response => response.json()) - .then(data => { - showToast(data.message, data.success ? 'success' : 'danger'); - - if (data.success) { - button.innerText = '✅ Zsynchronizowano'; - button.classList.remove('btn-outline-primary'); - button.classList.add('btn-success'); - } else { - button.disabled = false; - } - }) - .catch(() => { - showToast('Błąd synchronizacji', 'danger'); - button.disabled = false; - }); - }); + const clone = btn.cloneNode(true); + btn.replaceWith(clone); + bindSyncButton(clone); }); document.querySelectorAll('.delete-suggestion-btn').forEach(btn => { - btn.addEventListener('click', function (e) { - e.preventDefault(); - - const suggestionId = this.getAttribute('data-suggestion-id'); - const button = this; - button.disabled = true; - - fetch(`/admin/delete_suggestion/${suggestionId}`, { - method: 'POST', - headers: { - 'X-Requested-With': 'XMLHttpRequest' - } - }) - .then(response => response.json()) - .then(data => { - showToast(data.message, data.success ? 'success' : 'danger'); - - if (data.success) { - const row = button.closest('tr'); - if (row) row.remove(); - } else { - button.disabled = false; - } - }) - .catch(() => { - showToast('Błąd usuwania sugestii', 'danger'); - button.disabled = false; - }); - }); + const clone = btn.cloneNode(true); + btn.replaceWith(clone); + bindDeleteButton(clone); }); }); diff --git a/templates/admin/admin_panel.html b/templates/admin/admin_panel.html index 2b41c7e..9b65e2c 100644 --- a/templates/admin/admin_panel.html +++ b/templates/admin/admin_panel.html @@ -224,11 +224,11 @@ {% if l.is_archived %} - Archiwalna + Archiwalna {% elif e.expired %} - Wygasła + Wygasła {% else %} - Aktywna + Aktywna {% endif %} {{ l.created_at.strftime('%Y-%m-%d %H:%M') if l.created_at else '-' }} @@ -250,8 +250,8 @@ - {{ e.comments_count }} - {{ e.receipts_count }} + {{ e.comments_count }} + {{ e.receipts_count }} diff --git a/templates/admin/list_products.html b/templates/admin/list_products.html index 50f16f0..e7186f3 100644 --- a/templates/admin/list_products.html +++ b/templates/admin/list_products.html @@ -11,7 +11,7 @@

📦 Produkty (z synchronizacją sugestii o unikalnych nazwach)

- {{ total_items }} produktów + {{ total_items }} produktów
@@ -28,7 +28,7 @@ {% for item in items %} - + - + - +
{{ item.id }}{{ item.name }}{{ item.name }} {% if item.added_by and users_dict.get(item.added_by) %} 👤 {{ users_dict[item.added_by] }} ({{ item.added_by }}) @@ -36,7 +36,7 @@ - {% endif %} {{ usage_counts.get(item.name.lower(), 0) }}{{ usage_counts.get(item.name.lower(), 0) }} {% set clean_name = item.name | replace('\xa0', ' ') | trim | lower %} {% set suggestion = suggestions_dict.get(clean_name) %} @@ -66,7 +66,7 @@

💡 Wszystkie sugestie (poza powiązanymi)

- {{ orphan_suggestions|length }} sugestii + {{ orphan_suggestions|length }} sugestii
{% set item_names = items | map(attribute='name') | map('lower') | list %} @@ -83,7 +83,7 @@ {% if suggestion.name.lower() not in item_names %}
{{ suggestion.id }}{{ suggestion.name }}{{ suggestion.name }} diff --git a/templates/admin/mass_edit_categories.html b/templates/admin/mass_edit_categories.html index 20cb19b..391f613 100644 --- a/templates/admin/mass_edit_categories.html +++ b/templates/admin/mass_edit_categories.html @@ -46,11 +46,14 @@ {{ l.created_at.strftime('%Y-%m-%d %H:%M') if l.created_at else '-' }} - {% if l.is_archived %}Archiwalna{% endif %} - {% if l.is_temporary %}Tymczasowa{% + {% if l.is_archived %}Archiwalna{% endif %} - {% if l.is_public %}Publiczna{% else %} - Prywatna{% endif %} + {% if l.is_temporary %}Tymczasowa{% + endif %} + {% if l.is_public %}Publiczna{% else + %} + Prywatna{% endif %} {% if user.is_admin %} - Admin + Admin {% else %} - Użytkownik + Użytkownik {% endif %} diff --git a/templates/base.html b/templates/base.html index 1378871..d0c6afc 100644 --- a/templates/base.html +++ b/templates/base.html @@ -42,12 +42,12 @@ {% if current_user.is_authenticated %}
Zalogowany: - {{ current_user.username }} + {{ current_user.username }}
{% else %}
Przeglądasz jako - gość + gość
{% endif %} {% endif %} diff --git a/templates/edit_my_list.html b/templates/edit_my_list.html index 17b0e72..81cc2d2 100644 --- a/templates/edit_my_list.html +++ b/templates/edit_my_list.html @@ -60,7 +60,7 @@

- + {{ list.created_at.strftime('%Y-%m-%d') }}

diff --git a/templates/list.html b/templates/list.html index ab9e3a5..902448f 100644 --- a/templates/list.html +++ b/templates/list.html @@ -6,12 +6,12 @@

Lista: {{ list.title }} {% if list.is_archived %} - (Archiwalna) + (Archiwalna) {% endif %} {% if list.category_badges %} {% for cat in list.category_badges %} - {{ cat.name }} @@ -41,7 +41,7 @@ 🙈 Lista jest ukryta przed gośćmi {% endif %} - {{ request.url_root }}share/{{ list.share_token }} @@ -120,7 +120,7 @@ {{ item.name }} {% if item.quantity and item.quantity > 1 %} - x{{ item.quantity }} + x{{ item.quantity }} {% endif %} diff --git a/templates/list_share.html b/templates/list_share.html index f6f3541..3dc8693 100644 --- a/templates/list_share.html +++ b/templates/list_share.html @@ -6,15 +6,15 @@ 🛍️ {{ list.title }} {% if list.is_archived %} - (Archiwalna) + (Archiwalna) {% endif %} {% if total_expense > 0 %} - + 💸 {{ '%.2f'|format(total_expense) }} PLN {% else %} -