From 3c08b6fca20206b23ec2b550d865d5a679b970e5 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Wed, 15 Nov 2023 14:47:00 +0100
Subject: [PATCH] reload nomination voting details on form submit

---
 .../templates/colleges/_hx_nomination_summary.html    |  5 ++++-
 .../colleges/_hx_nomination_voting_rounds_tab.html    |  6 ++++--
 scipost_django/colleges/urls.py                       |  5 +++++
 scipost_django/colleges/views.py                      | 11 +++++++++++
 4 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_summary.html b/scipost_django/colleges/templates/colleges/_hx_nomination_summary.html
index 6e4e24f49..d6bd119b1 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_summary.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_summary.html
@@ -65,7 +65,10 @@
       <div class="col-12 col-md-5">
 
         {% if nomination.latest_voting_round %}
-          {% include "colleges/_hx_voting_round_summary.html" with round=nomination.latest_voting_round %}
+          <div hx-get="{% url "colleges:_hx_voting_round_summary" round_id=nomination.latest_voting_round.id %}"
+               hx-trigger="submit from:#nomination-{{ nomination.id }}-details target:form delay:1000">
+            {% include "colleges/_hx_voting_round_summary.html" with round=nomination.latest_voting_round %}
+          </div>
  
         {% else %}
           <div class="h-100 d-flex align-items-center justify-content-end">
diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html b/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html
index a7a010ef0..099ecf999 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html
@@ -17,9 +17,11 @@
     {% for voting_round in voting_rounds %}
       <div id="nomination-{{ nomination.id }}-round-{{ voting_round.id }}-tab-btn" type="button" class="me-2 nav-link 
         {% if selected_round and selected_round.id == voting_round.id %}active{% endif %}
-  
         {% if voting_round.id in inaccessible_round_ids %}disabled opacity-50{% endif %}
-         " hx-get="{% url 'colleges:_hx_nomination_voting_rounds_tab' nomination_id=nomination.id round_id=voting_round.id %}" hx-target="#nomination-{{ nomination.id }}-round-tab-holder" hx-swap="outerHTML">
+        " hx-get="{% url 'colleges:_hx_nomination_voting_rounds_tab' nomination_id=nomination.id round_id=voting_round.id %}" 
+          hx-target="#nomination-{{ nomination.id }}-round-tab-holder" 
+          hx-swap="outerHTML"
+          hx-trigger="click{% if selected_round and selected_round.id == voting_round.id %}, submit from:#nomination-{{ nomination.id }}-round-tab-holder target:form delay:1000{% endif %}">
         <span class="d-block text-nowrap">
 
           {% if voting_round.voting_opens and voting_round.voting_deadline %}
diff --git a/scipost_django/colleges/urls.py b/scipost_django/colleges/urls.py
index 21299fad2..3627fbf17 100644
--- a/scipost_django/colleges/urls.py
+++ b/scipost_django/colleges/urls.py
@@ -238,6 +238,11 @@ urlpatterns = [
                     views._hx_nomination_voter_table,
                     name="_hx_nomination_voter_table",
                 ),
+                path(
+                    "_hx_voting_round_summary",
+                    views._hx_voting_round_summary,
+                    name="_hx_voting_round_summary",
+                ),
                 path(
                     "forms/",
                     include(
diff --git a/scipost_django/colleges/views.py b/scipost_django/colleges/views.py
index 85ad66d20..ff1015b45 100644
--- a/scipost_django/colleges/views.py
+++ b/scipost_django/colleges/views.py
@@ -766,6 +766,17 @@ def _hx_nomination_details_contents(request, nomination_id):
     return render(request, "colleges/_hx_nomination_details_contents.html", context)
 
 
+@login_required
+@user_passes_test(is_edadmin_or_advisory_or_active_regular_or_senior_fellow)
+def _hx_voting_round_summary(request, round_id):
+    """For (re)loading the round summary if modified."""
+    round = get_object_or_404(FellowshipNominationVotingRound, pk=round_id)
+    context = {
+        "round": round,
+    }
+    return render(request, "colleges/_hx_voting_round_summary.html", context)
+
+
 def _hx_nominations_search_form(request, filter_set: str):
     form = FellowshipNominationSearchForm(
         user=request.user,
-- 
GitLab