From b67838fca845eeae6572ede00911dab6bd518e48 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Fri, 29 Oct 2021 13:50:32 +0200
Subject: [PATCH] Use new AJAX based interest indication on AK overview views

Additionally, remove obsolete logging
---
 .../templates/AKSubmission/ak_detail.html     |  1 -
 .../AKSubmission/ak_interest_script.html      | 62 +++++++++++++++++++
 .../templates/AKSubmission/ak_overview.html   |  2 +
 .../templates/AKSubmission/ak_table.html      |  8 ++-
 .../AKSubmission/submission_overview.html     |  2 +
 AKSubmission/views.py                         |  7 ++-
 6 files changed, 77 insertions(+), 5 deletions(-)
 create mode 100644 AKSubmission/templates/AKSubmission/ak_interest_script.html

diff --git a/AKSubmission/templates/AKSubmission/ak_detail.html b/AKSubmission/templates/AKSubmission/ak_detail.html
index 9cee1015..69324b63 100644
--- a/AKSubmission/templates/AKSubmission/ak_detail.html
+++ b/AKSubmission/templates/AKSubmission/ak_detail.html
@@ -68,7 +68,6 @@
                             data: {
                             },
                             success: function (response) {
-                               console.log(response);
                                btn.html('{% fa5_icon 'check' 'fas' %}');
                                btn.off('click');
                                $('#interest-counter').html(response.interest_counter);
diff --git a/AKSubmission/templates/AKSubmission/ak_interest_script.html b/AKSubmission/templates/AKSubmission/ak_interest_script.html
new file mode 100644
index 00000000..952f5720
--- /dev/null
+++ b/AKSubmission/templates/AKSubmission/ak_interest_script.html
@@ -0,0 +1,62 @@
+{% load i18n %}
+{% load fontawesome_5 %}
+
+<script>
+    document.addEventListener('DOMContentLoaded', function () {
+        // CSRF Protection/Authentication
+        function getCookie(name) {
+            let cookieValue = null;
+            if (document.cookie && document.cookie !== '') {
+                const cookies = document.cookie.split(';');
+                for (let i = 0; i < cookies.length; i++) {
+                    const cookie = cookies[i].trim();
+                    // Does this cookie string begin with the name we want?
+                    if (cookie.substring(0, name.length + 1) === (name + '=')) {
+                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
+                        break;
+                    }
+                }
+            }
+            return cookieValue;
+        }
+
+        const csrftoken = getCookie('csrftoken');
+
+        function csrfSafeMethod(method) {
+            // these HTTP methods do not require CSRF protection
+            return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
+        }
+
+        $.ajaxSetup({
+            beforeSend: function (xhr, settings) {
+                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
+                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
+                }
+            }
+        });
+
+        function indicate_interest(ak_id, btn) {
+            $.ajax({
+                   url: "{% url "model:AK-list" event_slug=event.slug %}" + ak_id + "/indicate-interest/",
+                    type: 'POST',
+                    data: {
+                    },
+                    success: function (response) {
+                       btn.html('{% fa5_icon 'check' 'fas' %}');
+                       btn.off('click');
+                    },
+                    error: function (response) {
+                       if(response.status === 403)
+                            alert("{% trans 'Interest indication currently not allowed. Sorry.' %}");
+                       else
+                           alert("{% trans 'Could not save your interest. Sorry.' %}");
+                    }
+               });
+        }
+
+        // Update counter
+        $('.btn-interest').click(function () {
+            indicate_interest($(this).data('ak_id'), $(this));
+        });
+    });
+</script>
diff --git a/AKSubmission/templates/AKSubmission/ak_overview.html b/AKSubmission/templates/AKSubmission/ak_overview.html
index 01b8ab84..b487f168 100644
--- a/AKSubmission/templates/AKSubmission/ak_overview.html
+++ b/AKSubmission/templates/AKSubmission/ak_overview.html
@@ -19,6 +19,8 @@
             white-space: nowrap;
         }
     </style>
+
+    {% include "AKSubmission/ak_interest_script.html" %}
 {% endblock %}
 
 {% block breadcrumbs %}
diff --git a/AKSubmission/templates/AKSubmission/ak_table.html b/AKSubmission/templates/AKSubmission/ak_table.html
index 41047096..ba15c259 100644
--- a/AKSubmission/templates/AKSubmission/ak_table.html
+++ b/AKSubmission/templates/AKSubmission/ak_table.html
@@ -52,9 +52,11 @@
                     <a href="{% url 'submit:ak_edit' event_slug=event.slug pk=ak.pk %}" data-toggle="tooltip"
                        title="{% trans 'Edit' %}"
                        class="btn btn-success">{% fa5_icon 'pencil-alt' 'fas' %}</a>
-                     <a href="{% url 'submit:overview_inc_interest' event_slug=event.slug pk=ak.pk %}" data-toggle="tooltip"
-                       title="{% trans 'Show Interest' %}"
-                       class="btn btn-primary">{% fa5_icon 'thumbs-up' 'fas' %}</a>
+                    {% if interest_indication_active %}
+                        <span data-ak_id="{{ ak.pk }}" data-toggle="tooltip"
+                           title="{% trans 'Show Interest' %}"
+                           class="btn btn-primary btn-interest" style="cursor: pointer">{% fa5_icon 'thumbs-up' 'fas' %}</span>
+                    {% endif %}
                 {% endif %}
             </td>
         </tr>
diff --git a/AKSubmission/templates/AKSubmission/submission_overview.html b/AKSubmission/templates/AKSubmission/submission_overview.html
index e7994b69..49782a63 100644
--- a/AKSubmission/templates/AKSubmission/submission_overview.html
+++ b/AKSubmission/templates/AKSubmission/submission_overview.html
@@ -28,6 +28,8 @@
         }
     </style>
 
+    {% include "AKSubmission/ak_interest_script.html" %}
+
     {% if event.active %}
         <link href="{% static 'common/vendor/select2/select2.min.css' %}" rel="stylesheet" />
         <script src="{% static 'common/vendor/select2/select2.min.js' %}"></script>
diff --git a/AKSubmission/views.py b/AKSubmission/views.py
index a47fe728..e1844787 100644
--- a/AKSubmission/views.py
+++ b/AKSubmission/views.py
@@ -74,6 +74,10 @@ class AKOverviewView(FilterByEventSlugMixin, ListView):
         context["active_category"] = self.get_active_category_name(context)
         context['table_title'] = self.get_table_title(context)
 
+        # Display interest indication button?
+        current_timestamp = datetime.now().astimezone(self.event.timezone)
+        context['interest_indication_active'] = ak_interest_indication_active(self.event, current_timestamp)
+
         return context
 
 
@@ -137,10 +141,11 @@ class AKDetailView(EventSlugMixin, DetailView):
         context = super().get_context_data(object_list=object_list, **kwargs)
         context["availabilities"] = Availability.objects.filter(ak=context["ak"])
 
+        current_timestamp = datetime.now().astimezone(self.event.timezone)
+
         # Is this AK taking place now or soon (used for top page visualization)
         context["featured_slot_type"] = "NONE"
         if apps.is_installed("AKPlan"):
-            current_timestamp = datetime.now().astimezone(self.event.timezone)
             in_two_hours = current_timestamp + timedelta(hours=2)
             slots = context["ak"].akslot_set.filter(start__isnull=False, room__isnull=False)
             for slot in slots:
-- 
GitLab