Add HTMX trigger to close review modal after saving
This commit is contained in:
+6
-2
@@ -2998,8 +2998,12 @@ class GenericViewBase:
|
||||
status=form.cleaned_data["status"],
|
||||
comment=form.cleaned_data.get("comment", ""),
|
||||
)
|
||||
|
||||
return render(request, "generic/partials/question_review_block.html", {"review": review, "question": question, "app_name": self.app_name})
|
||||
# Render the updated review block and include an HX-Trigger so the client
|
||||
# can close the modal (HTMX will dispatch the event named below).
|
||||
resp = render(request, "generic/partials/question_review_block.html", {"review": review, "question": question, "app_name": self.app_name})
|
||||
# Trigger a client-side event; base template will listen for this and hide the modal
|
||||
resp["HX-Trigger"] = "reviewSaved"
|
||||
return resp
|
||||
|
||||
# Default: render the block (latest if exists, otherwise form)
|
||||
if latest:
|
||||
|
||||
@@ -194,6 +194,25 @@
|
||||
<li class="ms-3"><a class="text-muted" href="{% url 'about' %}">About</a></li>
|
||||
</ul>
|
||||
</footer>
|
||||
<!-- Close review modal after HTMX reviewSaved trigger -->
|
||||
<script>
|
||||
document.body.addEventListener('reviewSaved', function () {
|
||||
try {
|
||||
var modalEl = document.getElementById('reviewModal');
|
||||
if (!modalEl) return;
|
||||
var modal = bootstrap.Modal.getInstance(modalEl);
|
||||
if (modal) {
|
||||
modal.hide();
|
||||
} else {
|
||||
// If no instance exists, create one and hide it
|
||||
var m = new bootstrap.Modal(modalEl);
|
||||
m.hide();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error hiding review modal', err);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user