diff --git a/app.py b/app.py index 86e193d..b9a158c 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"] + protected_js = ["live.js", "list_guest.js", "hide_list.js", "socket_reconnect.js","sync_products.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)) @@ -858,21 +858,23 @@ def list_products(): suggestions_dict=suggestions_dict ) -@app.route('/admin/sync_suggestion/') +@app.route('/admin/sync_suggestion_ajax/', methods=['POST']) @login_required -def sync_suggestion(item_name): +def sync_suggestion_ajax(item_id): if not current_user.is_admin: - return redirect(url_for('index_guest')) + return jsonify({'success': False, 'message': 'Brak uprawnień'}), 403 - existing = SuggestedProduct.query.filter(func.lower(SuggestedProduct.name) == item_name.lower()).first() + item = Item.query.get_or_404(item_id) + + existing = SuggestedProduct.query.filter(func.lower(SuggestedProduct.name) == item.name.lower()).first() if not existing: - new_suggestion = SuggestedProduct(name=item_name) + new_suggestion = SuggestedProduct(name=item.name) db.session.add(new_suggestion) db.session.commit() - flash(f'Utworzono sugestię dla produktu: {item_name}', 'success') + return jsonify({'success': True, 'message': f'Utworzono sugestię dla produktu: {item.name}'}) else: - flash(f'Sugestia dla produktu "{item_name}" już istnieje.', 'info') - return redirect(url_for('list_products')) + return jsonify({'success': True, 'message': f'Sugestia dla produktu „{item.name}” już istnieje.'}) + @app.route('/admin/delete_suggestion/') @login_required diff --git a/static/js/live.js b/static/js/live.js index 3db10c8..3f04ad9 100644 --- a/static/js/live.js +++ b/static/js/live.js @@ -120,7 +120,7 @@ function setupList(listId, username) { li.innerHTML = `
- + ${data.name} ${quantityBadge}
diff --git a/static/js/sync_products.js b/static/js/sync_products.js new file mode 100644 index 0000000..c5909ca --- /dev/null +++ b/static/js/sync_products.js @@ -0,0 +1,65 @@ +document.addEventListener("DOMContentLoaded", function() { + // Usuń stare eventy + document.querySelectorAll('.sync-btn').forEach(btn => { + btn.replaceWith(btn.cloneNode(true)); + }); + + // Pobierz już "czyste" przyciski + const buttons = document.querySelectorAll('.sync-btn'); + + buttons.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_ajax/${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(error => { + showToast('Błąd synchronizacji', 'danger'); + button.disabled = false; + }); + }); + }); +}); + + +// Funkcja do wyświetlania toast +function showToast(message, type = 'success') { + const toastContainer = document.getElementById('toast-container'); + const toast = document.createElement('div'); + toast.className = `toast align-items-center text-white bg-${type} border-0 show`; + toast.role = 'alert'; + toast.style.minWidth = '250px'; + toast.innerHTML = ` +
+
${message}
+ +
+ `; + toastContainer.appendChild(toast); + + // Automatyczne usunięcie po 3 sekundach + 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 96c0c0f..7b5b84f 100644 --- a/templates/admin/list_products.html +++ b/templates/admin/list_products.html @@ -41,7 +41,7 @@ ✅ Istnieje (ID: {{ suggestion.id }}) 🗑️ Usuń {% else %} - 🔄 Synchronizuj + {% endif %} @@ -59,4 +59,9 @@
+{% block scripts %} + + +{% endblock %} + {% endblock %}