From c8472c94238d19b7d31d5220c39c27e04ca25f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Mon, 7 Jul 2025 16:36:22 +0200 Subject: [PATCH] poprawki w synchronizacji produktow --- app.py | 13 ++--- ...sync_products.js => product_suggestion.js} | 50 +++++++++++++++++-- templates/admin/list_products.html | 5 +- 3 files changed, 55 insertions(+), 13 deletions(-) rename static/js/{sync_products.js => product_suggestion.js} (57%) diff --git a/app.py b/app.py index 1ce623d..a65634f 100644 --- a/app.py +++ b/app.py @@ -220,7 +220,7 @@ def require_system_password(): # specjalny wyjątek dla statycznych, ale sprawdzany ręcznie niżej if request.endpoint == 'static_bp.serve_js': # tu sprawdzamy czy to JS, który ma być chroniony - protected_js = ["live.js", "list_guest.js", "hide_list.js", "socket_reconnect.js","sync_products.js", "expenses.js", "toggle_button.js"] + protected_js = ["live.js", "list_guest.js", "hide_list.js", "socket_reconnect.js","product_suggestion.js", "expenses.js", "toggle_button.js"] requested_file = request.view_args.get("filename", "") if requested_file in protected_js: return redirect(url_for('system_auth', next=request.url)) @@ -875,16 +875,17 @@ def sync_suggestion_ajax(item_id): else: return jsonify({'success': True, 'message': f'Sugestia dla produktu „{item.name}” już istnieje.'}) -@app.route('/admin/delete_suggestion/') +@app.route('/admin/delete_suggestion/', methods=['POST']) @login_required -def delete_suggestion(suggestion_id): +def delete_suggestion_ajax(suggestion_id): if not current_user.is_admin: - return redirect(url_for('index_guest')) + return jsonify({'success': False, 'message': 'Brak uprawnień'}), 403 + suggestion = SuggestedProduct.query.get_or_404(suggestion_id) db.session.delete(suggestion) db.session.commit() - flash('Sugestia została usunięta', 'success') - return redirect(url_for('list_products')) + + return jsonify({'success': True, 'message': 'Sugestia została usunięta.'}) @app.route('/admin/expenses_data') @login_required diff --git a/static/js/sync_products.js b/static/js/product_suggestion.js similarity index 57% rename from static/js/sync_products.js rename to static/js/product_suggestion.js index c5909ca..9c37360 100644 --- a/static/js/sync_products.js +++ b/static/js/product_suggestion.js @@ -3,10 +3,12 @@ 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)); + }); - // Pobierz już "czyste" przyciski + // Synchronizacja sugestii const buttons = document.querySelectorAll('.sync-btn'); - buttons.forEach(btn => { btn.addEventListener('click', function(e) { e.preventDefault(); @@ -16,7 +18,7 @@ document.addEventListener("DOMContentLoaded", function() { button.disabled = true; - fetch(`/admin/sync_suggestion_ajax/${itemId}`, { + fetch(`/admin/sync_suggestion/${itemId}`, { method: 'POST', headers: { 'X-Requested-With': 'XMLHttpRequest' @@ -40,6 +42,44 @@ document.addEventListener("DOMContentLoaded", function() { }); }); }); + + // Usuwanie sugestii + const deleteButtons = document.querySelectorAll('.delete-suggestion-btn'); + deleteButtons.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) { + // Możesz usunąć cały wiersz lub np. odświeżyć kolumnę + const row = button.closest('tr'); + if (row) { + row.remove(); + } + } else { + button.disabled = false; + } + }) + .catch(error => { + showToast('Błąd usuwania sugestii', 'danger'); + button.disabled = false; + }); + }); + }); }); @@ -58,8 +98,8 @@ function showToast(message, type = 'success') { `; toastContainer.appendChild(toast); - // Automatyczne usunięcie po 3 sekundach + // Automatyczne usunięcie po 1.75 sekundy setTimeout(() => { toast.remove(); }, 1750); -} \ No newline at end of file +} diff --git a/templates/admin/list_products.html b/templates/admin/list_products.html index 7b5b84f..2fce4cb 100644 --- a/templates/admin/list_products.html +++ b/templates/admin/list_products.html @@ -39,7 +39,8 @@ {% set suggestion = suggestions_dict.get(item.name.lower()) %} {% if suggestion %} ✅ Istnieje (ID: {{ suggestion.id }}) - 🗑️ Usuń + + {% else %} {% endif %} @@ -61,7 +62,7 @@ {% block scripts %} - + {% endblock %} {% endblock %}