From 998b6dfe0b195e15d2a833338999d26ed1a1e856 Mon Sep 17 00:00:00 2001
From: zachmann <gabriel.zachmann@kit.edu>
Date: Fri, 24 Nov 2023 12:32:12 +0100
Subject: [PATCH] [web] cache notifications data (email status, calendars) in
 mytoken list notification interaction

---
 internal/server/web/static/js/tokeninfo.js | 40 ++++++++++++++++------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/internal/server/web/static/js/tokeninfo.js b/internal/server/web/static/js/tokeninfo.js
index a4061877..a9cec9ff 100644
--- a/internal/server/web/static/js/tokeninfo.js
+++ b/internal/server/web/static/js/tokeninfo.js
@@ -344,6 +344,8 @@ const $calendarURL = $('#notify-calendar-url');
 
 let calendarURLs = {};
 
+let cachedNotificationsTypeData = {};
+
 $notificationTypeSelector.on('change', function () {
     let t = $(this).val();
     switch (t) {
@@ -357,6 +359,10 @@ $notificationTypeSelector.on('change', function () {
             $notificationTypeEmailContent.hideB();
             $notificationTypeEntryContent.showB();
 
+            let email_ok = cachedNotificationsTypeData["email_ok"];
+            if (email_ok !== undefined && email_ok !== null && email_ok) {
+                break;
+            }
             $('.notify-entry-content-element').hideB();
             $.ajax({
                 type: "GET",
@@ -366,13 +372,16 @@ $notificationTypeSelector.on('change', function () {
                     let email_verified = res["email_verified"];
                     if (email === undefined || email === null || email === "") {
                         $('#email-not-verified-hint').showB();
+                        cachedNotificationsTypeData["email_ok"] = false;
                         return;
                     }
                     if (email_verified === undefined || !email_verified) {
                         $('#email-not-set-hint').showB();
+                        cachedNotificationsTypeData["email_ok"] = false;
                         return;
                     }
                     $('#notify-entry-content-normal').showB();
+                    cachedNotificationsTypeData["email_ok"] = true;
                 },
                 error: function (errRes) {
                     $errorModalMsg.text(getErrorMessage(errRes));
@@ -384,21 +393,32 @@ $notificationTypeSelector.on('change', function () {
             $notificationTypeEntryContent.hideB();
             $notificationTypeEmailContent.hideB();
             $notificationTypeCalendarContent.showB();
+
+        function fillCals(cals) {
+            let options = "";
+            if (cals !== undefined && cals !== null) {
+                cals.forEach(function (cal) {
+                    let name = cal["name"];
+                    calendarURLs[name] = cal["ics_path"];
+                    options += `<option value=${name}>${name}</option>`;
+                });
+                cachedNotificationsTypeData["calendars"] = cals;
+            }
+            $calendarSelector.html(options);
+            $calendarSelector.trigger('change');
+        }
+
+            let cals = cachedNotificationsTypeData["calendars"];
+            if (cals !== undefined && cals !== null && cals.length > 0) {
+                fillCals(cals);
+                break;
+            }
             $.ajax({
                 type: "GET",
                 url: storageGet('notifications_endpoint') + "/calendars",
                 success: function (res) {
-                    let options = "";
                     let cals = res["calendars"];
-                    if (cals !== undefined && cals !== null) {
-                        cals.forEach(function (cal) {
-                            let name = cal["name"];
-                            calendarURLs[name] = cal["ics_path"];
-                            options += `<option value=${name}>${name}</option>`;
-                        })
-                    }
-                    $calendarSelector.html(options);
-                    $calendarSelector.trigger('change');
+                    fillCals(cals);
                 },
                 error: function (errRes) {
                     $settingsErrorModalMsg.text(getErrorMessage(errRes));
-- 
GitLab