Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only update affected media list when deleting destination in HTMX frontend #1120

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1128.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Only update the related media list when deleting a destination.
35 changes: 22 additions & 13 deletions src/argus/htmx/destination/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@ def create_htmx(request) -> HttpResponse:
@require_http_methods(["POST"])
def delete_htmx(request, pk: int) -> HttpResponse:
destination = get_object_or_404(request.user.destinations.all(), pk=pk)
template = "htmx/destination/_form_list.html"
media = destination.media
error_msg = None
try:
medium = api_safely_get_medium_object(destination.media.slug)
medium.raise_if_not_deletable(destination)
except NotificationMedium.NotDeletableError:
error_msg = "This destination cannot be deleted."
return _render_destination_list(request, errors=[error_msg], template=template)
error_msg = "That destination cannot be deleted."
else:
destination.delete()
return _render_destination_list(request, template=template)

forms = _get_update_forms(request.user, media=media)

context = {
"error_msg": error_msg,
"forms": forms,
"media": media,
}
return render(request, "htmx/destination/_collapse_with_forms.html", context=context)


@require_http_methods(["POST"])
Expand All @@ -62,7 +70,6 @@ def _render_destination_list(
request,
create_form: Optional[DestinationFormCreate] = None,
update_forms: Optional[Sequence[DestinationFormUpdate]] = None,
errors: Optional[Sequence[str]] = None,
template: str = "htmx/destination/destination_list.html",
) -> HttpResponse:
"""Function to render the destinations page.
Expand All @@ -71,29 +78,31 @@ def _render_destination_list(
with errors while retaining the user input. If you want a blank form, pass None.
:param update_forms: list of update forms to display. Useful for rendering forms
with error messages while retaining the user input.
If this is None, the update forms will be generated from the user's destinations.
:param errors: a list of error messages to display on the page. Will not be tied to
any form fields."""
If this is None, the update forms will be generated from the user's destinations."""

if create_form is None:
create_form = DestinationFormCreate()
if update_forms is None:
update_forms = _get_update_forms(request.user)
if errors is None:
errors = []
grouped_forms = _group_update_forms_by_media(update_forms)
context = {
"create_form": create_form,
"grouped_forms": grouped_forms,
"errors": errors,
"page_title": "Destinations",
}
return render(request, template, context=context)


def _get_update_forms(user) -> list[DestinationFormUpdate]:
def _get_update_forms(user, media: Media = None) -> list[DestinationFormUpdate]:
"""Get a list of update forms for the user's destinations.
:param media: if provided, only return destinations for this media.
"""
if media:
destinations = user.destinations.filter(media=media)
else:
destinations = user.destinations.all()
# Sort by oldest first
destinations = user.destinations.all().order_by("pk")
destinations = destinations.order_by("pk")
return [DestinationFormUpdate(instance=destination) for destination in destinations]


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<details class="collapse bg-base-200 collapse-arrow" open="">
<summary class="collapse-title text-xl font-medium">{{ media.name }} ({{ forms|length }})</summary>
<div class="collapse-content">
{% if error_msg %}<p class="text-error">{{ error_msg }}</p>{% endif %}
{% for form in forms %}
<div class="flex w-full h-fit items-center justify-center">
{% include "htmx/destination/_edit_form.html" %}
{% include "htmx/destination/_delete_form.html" %}
</div>
{% endfor %}
</div>
</details>
1 change: 0 additions & 1 deletion src/argus/htmx/templates/htmx/destination/_content.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<div id="destination-content" class="flex flex-col items-center gap-4">
{% include "htmx/destination/_create_form.html" %}
{% for error in errors %}<p class="text-error">{{ error }}</p>{% endfor %}
{% include "htmx/destination/_form_list.html" %}
</div>
3 changes: 2 additions & 1 deletion src/argus/htmx/templates/htmx/destination/_delete_form.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<form hx-post="{% url 'htmx:htmx-delete' form.instance.id %}"
hx-trigger="submit"
hx-target="#form-list">
hx-target="closest details"
hx-swap="outerHTML">
{% csrf_token %}
<fieldset class="menu menu-horizontal menu-md items-center gap-4">
<input type="submit" value="Delete" class="btn btn-secondary">
Expand Down
12 changes: 1 addition & 11 deletions src/argus/htmx/templates/htmx/destination/_form_list.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
<div id="form-list" class="flex flex-col max-w-4xl w-full gap-4">
{% for media, forms in grouped_forms.items %}
<details class="collapse bg-base-200 collapse-arrow" open="">
<summary class="collapse-title text-xl font-medium">{{ media.name }} ({{ forms|length }})</summary>
<div class="collapse-content">
{% for form in forms %}
<div class="flex w-full h-fit items-center justify-center">
{% include "htmx/destination/_edit_form.html" %}
{% include "htmx/destination/_delete_form.html" %}
</div>
{% endfor %}
</div>
</details>
{% include "htmx/destination/_collapse_with_forms.html" with error_msg=None %}
{% endfor %}
</div>
Loading