diff --git a/sigi/apps/home/views.py b/sigi/apps/home/views.py index c05f8d4..67ec842 100644 --- a/sigi/apps/home/views.py +++ b/sigi/apps/home/views.py @@ -245,14 +245,20 @@ def card_snippet(request, card_code): @login_required def card_add_tab(request, tab_slug): + total = 0 + categoria = tab_slug for card in Cards.objects.all(): if slugify(card.categoria) == tab_slug: - Dashboard( + categoria = card.categoria + __, created = Dashboard.objects.update_or_create( + defaults={"ordem": card.ordem}, usuario=request.user, card=card, categoria=card.categoria, - ordem=card.ordem, - ).save() + ) + if created: + total += 1 + messages.info(request, _(f"{total} cards adicionados na aba {categoria}")) return HttpResponseRedirect(reverse("admin:index")) @@ -280,17 +286,15 @@ def card_reorder(request): Dashboard.objects.filter( usuario=request.user, card__codigo=codigo, categoria=categoria ).update(ordem=nova_ordem) - return JsonResponse({"result": "Done"}) + return JsonResponse({"result": _("Ordem alterada")}) @login_required def card_remove(request, categoria, codigo): - dash = get_object_or_404( - Dashboard, categoria=categoria, card__codigo=codigo - ) - dash.delete() - messages.success(request, _("Card removido")) - return HttpResponseRedirect(reverse("admin:index")) + count, *__ = Dashboard.objects.filter( + categoria=categoria, card__codigo=codigo + ).delete() + return JsonResponse({"result": _(f"{count} card(s) removido(s)")}) @login_required diff --git a/sigi/static/js/dashboard.js b/sigi/static/js/dashboard.js index 9dc9d2f..0be3175 100644 --- a/sigi/static/js/dashboard.js +++ b/sigi/static/js/dashboard.js @@ -1,6 +1,24 @@ $(document).ready(function () { - M.Tabs.init($('.tabs'), {}); $(".dash-control").hide(); + M.Tabs.init($('.tabs'), {}); + $(".remove-card").off("click").on("click", function (e) { + e.preventDefault(); + var $this = $(this); + var context = $this.parents("li"); + $.ajax({ + url: $this.attr("href"), + method: 'GET', + context: context, + success: function (data) { + $(this).remove(); + M.toast({ html: data.result }) + }, + error: function (jqXHR, textStatus, errorThrown) { + console.log(errorThrown); + M.toast({ html: `${textStatus}: ${errorThrown}` }); + } + }) + }); $(".tab-edit a").off("click").on("click", function (e) { e.preventDefault(); $(".dash-control").toggle(); @@ -13,11 +31,12 @@ $(document).ready(function () { parent.children().each(function (pos) { dados[$(this).attr("data-card-id")] = pos + 1; }) - $.get(url, dados, function () { - M.toast({ html: 'Ordem alterada' }) + $.get(url, dados, function (data) { + M.toast({ html: data.response }) }); } }); + $(".sortable").sortable("enable"); } else { $(".sortable").sortable("disable"); } diff --git a/sigi/templates/material/admin/index.html b/sigi/templates/material/admin/index.html index 1591526..191c120 100644 --- a/sigi/templates/material/admin/index.html +++ b/sigi/templates/material/admin/index.html @@ -43,7 +43,7 @@ {% for dash in sigi_dashes %} diff --git a/sigi/templates/sigi/snippets/base_card.html b/sigi/templates/sigi/snippets/base_card.html index 41ea39a..26cad95 100644 --- a/sigi/templates/sigi/snippets/base_card.html +++ b/sigi/templates/sigi/snippets/base_card.html @@ -21,7 +21,7 @@
drag_handle {% block card-title %}{% translate card_title|default:"" %}{% endblock %} - close + close
{% block card-content %}{% endblock card-content %}