Skip to content

Commit 46892b9

Browse files
committed
Make the profiles delete button a modal
1 parent 2b8a879 commit 46892b9

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

src/argus/htmx/forms.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class ModalForm:
6+
form: int = None
7+
instance_id: int = None
8+
template_name: str = "htmx/_base_form_modal2.html"
9+
10+
# needed by template
11+
button_class: str = "btn-primary"
12+
cancel_text: str = "Cancel"
13+
submit_text: str = "Ok"
14+
button_title: str
15+
dialog_id: str
16+
endpoint: str
17+
explanation: str
18+
header: str
19+
20+
def get_endpoint(self):
21+
if self.instance_id:
22+
return self.endpoint.format(self.instance_id)
23+
return self.endpoint
24+
25+
26+
class DeleteModalForm(ModalForm):
27+
button_title = "Delete title"
28+
header = "Delete header"
29+
submit_text = "Delete nao"
30+
explanation = "Delete explanation"

src/argus/htmx/notificationprofile/views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.urls import reverse
1010
from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView
1111

12+
from argus.htmx.forms import DeleteModalForm
1213
from argus.notificationprofile.models import NotificationProfile, Timeslot, Filter, DestinationConfig
1314

1415

@@ -95,6 +96,10 @@ def get_context_data(self, **kwargs):
9596
forms = []
9697
for obj in self.get_queryset():
9798
form = NotificationProfileForm(None, user=self.request.user, instance=obj)
99+
form.modal = DeleteModalForm(
100+
dialog_id=f"delete-modal-{obj.pk}",
101+
endpoint=reverse("htmx:notificationprofile-delete", pk=obj.pk),
102+
)
98103
forms.append(form)
99104
context["form_list"] = forms
100105
return context
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<button class="btn {{ form.button_class }}"
2+
onclick="htmx.find('#{{ form.dialog_id }}').showModal()">{{ button_title }}</button>
3+
<dialog id="{{ form.dialog_id }}" class="modal">
4+
<div class="modal-box card card-compact shadow-xl loading-box">
5+
<div class="w-full">
6+
<div class="divider divider-start">
7+
<h3 class="card-title">{{ form.header }}</h3>
8+
</div>
9+
<form id="{{ form.dialog_id }}-form"
10+
class="card-body"
11+
{% block form_control %}
12+
method="post"
13+
action="{{ endpoint }}"
14+
{% endblock form_control %}>
15+
{% csrf_token %}
16+
<fieldset class="menu menu-vertical gap-4">
17+
<legend class="antialiased text-base font-bold py-2">{{ form.explanation }}</legend>
18+
{% block dialogform %}
19+
{% endblock dialogform %}
20+
</fieldset>
21+
</form>
22+
<div class="modal-action card-actions">
23+
<form method="dialog" class="w-full">
24+
<div class="divider divider-end">
25+
<button type="submit"
26+
form="{{ form.dialog_id }}-form"
27+
class="btn {{ form.button_class }}">
28+
<span>{{ form.submit_text }}</span>
29+
</button>
30+
<button class="btn">{{ form.cancel_text }}</button>
31+
</div>
32+
</form>
33+
</div>
34+
</div>
35+
<div class="htmx-indicator loading loading-spinner text-primary"></div>
36+
</div>
37+
<form method="dialog" class="modal-backdrop">
38+
<button>close</button>
39+
</form>
40+
</dialog>

src/argus/htmx/templates/htmx/notificationprofile/_notificationprofile_buttons.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div class="card-actions justify-end">
22
<input class="btn btn-primary" type="submit" value="Save">
3+
{% include form.modal.template_name %}
34
<button class="contents">
45
<a class="btn btn-primary"
56
href="{% url "htmx:notificationprofile-delete" pk=object.pk %}">Delete</a>

0 commit comments

Comments
 (0)