From acdc172b515f446fff3c2f811dc5cb4ab7d751c7 Mon Sep 17 00:00:00 2001
From: Michael Bauer <michael-bauer@posteo.de>
Date: Fri, 22 Mar 2024 11:56:09 +0100
Subject: [PATCH] Introduce knownDates and dateRanges

---
 .../components/coursebook/Coursebook.vue      | 40 ++++++++++---------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/aleksis/apps/alsijil/frontend/components/coursebook/Coursebook.vue b/aleksis/apps/alsijil/frontend/components/coursebook/Coursebook.vue
index adc525f8d..1c47f5902 100644
--- a/aleksis/apps/alsijil/frontend/components/coursebook/Coursebook.vue
+++ b/aleksis/apps/alsijil/frontend/components/coursebook/Coursebook.vue
@@ -102,29 +102,27 @@ export default {
   data() {
     return {
       gqlQuery: documentationsForCoursebook,
+      knownDates: {},
       docsByDay: {},
       lastQuery: null,
       // Placeholder values while query isn't completed yet
       groups: [],
       courses: [],
-      dateStart: null,
-      dateEnd: null,
       incomplete: false,
     };
   },
   computed: {
-    // TODO: Query should wait until dateStart is set!
-    //       = wait for mounted
+    // Assertion: Should only fire on page load or selection change.
+    //            Resets date range.
     gqlQueryArgs() {
       console.log('computing gqlQueryArgs');
+      const dateRange = this.resetDate();
       return {
         own: this.filterType === "all" ? false : true,
         objId: this.objId ? Number(this.objId) : undefined,
         objType: this.objType?.toUpperCase(),
-        dateStart: this.dateStart ?? this.date,
-        dateEnd:
-          this.dateEnd ??
-          DateTime.fromISO(this.date).plus({ weeks: 1 }).toISODate(),
+        dateStart: dateRange[0].toISODate(),
+        dateEnd: dateRange[1].toISODate(),
         incomplete: !!this.incomplete,
       };
     },
@@ -161,6 +159,21 @@ export default {
     },
   },
   methods: {
+    resetDate() {
+      // Assure current date
+      console.log('Resetting date range', this.$route.hash);
+      if (!this.$route.hash) {
+        console.log('Set default date');
+        this.$router.replace({ hash: DateTime.now().toISODate() })
+      }
+      // Resetting known dates to dateRange around current date
+      this.knownDates = {};
+      const dateRange = this.dateRange(DateTime.fromISO(this.$route.hash.substring(1)))
+      dateRange.forEach((ts) => this.knownDates[ts] = true);
+      const lastIdx = dateRange.length - 1;
+      // Returning a dateRange each around first & last date for the initial query
+      return [this.dateRange(dateRange[0])[0], this.dateRange(dateRange[lastIdx])[lastIdx]];
+    },
     // => {dt: [dt doc ...] ...}
     groupDocsByDay(docs) {
       return docs.reduce((byDay, doc) => {
@@ -276,10 +289,9 @@ export default {
         .splitBy({ days: 1 })
         .map((ts) => ts.start);
     },
-    // TODO: Improve Add empty but already queried days to docsByDay -> do not query them again
     // docsByDay: {dt: [dt doc ...] ...}
     assureDate(date) {
-      if (!this.knownDate[date]) {
+      if (!this.knownDates[date]) {
         // find missing & fetch missing range
         // date +- 5 days ?
         const dateRange = Interval
@@ -308,14 +320,6 @@ export default {
     },
   },
   mounted() {
-    // assure date hash
-    console.log('mounted with hash', this.$route.hash);
-    if (!this.$route.hash) {
-      console.log('initialized hash');
-      this.$router.replace({ hash: DateTime.now().toISODate() })
-    }
-    this.dateStart = this.$route.hash.substring(1);
-    this.dateEnd = DateTime.fromISO(this.dateStart).plus({ weeks: 1 }).toISODate()
     window.addEventListener('scroll', this.debounce(this.setCurrentDay, 300));
   },
 };
-- 
GitLab