from django.views.generic.edit import FormView


class HTMXModalFormView(FormView):
    """Render form inside a modal and respond with HTMX triggers on success."""

    modal_template_name = "components/modals/modal_form.html"

    def get_template_names(self):
        return [self.modal_template_name]

    def form_valid(self, form):
        response = super().form_valid(form)
        if self.request.headers.get("HX-Request") == "true":
            response["HX-Trigger"] = "modal:close"
        return response
