From 1058b324abfa69d95f921f658fb050c4569434cb Mon Sep 17 00:00:00 2001
From: Michael Bauer <michael-bauer@posteo.de>
Date: Fri, 29 Mar 2024 13:28:18 +0100
Subject: [PATCH] Make current day detection more robust.

This version does not rely on internal state anymore.
---
 .../components/coursebook/Coursebook.vue      | 37 ++++++++-----------
 1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/aleksis/apps/alsijil/frontend/components/coursebook/Coursebook.vue b/aleksis/apps/alsijil/frontend/components/coursebook/Coursebook.vue
index f46cfc5fb..e37aff811 100644
--- a/aleksis/apps/alsijil/frontend/components/coursebook/Coursebook.vue
+++ b/aleksis/apps/alsijil/frontend/components/coursebook/Coursebook.vue
@@ -18,7 +18,12 @@
     <template #default="{ items }">
       <v-list-item
         v-for="{ date, docs, first, last } in groupDocsByDay(items)"
-        v-intersect="onIntersect"
+        v-intersect="{
+            handler: onIntersect,
+            options: {
+              threshold: [0, 1]
+            }
+          }"
         :data-date="date.toISODate()"
         :data-first="first"
         :data-last="last"
@@ -174,7 +179,11 @@ export default {
       if (!this.$route.hash) {
         console.log('Set default date');
         this.$router.replace({ hash: DateTime.now().toISODate() })
-      }
+      } 
+
+      // if (date) {
+      //   this.$router.replace({ hash: date })
+      // }
       const date = DateTime.fromISO(this.$route.hash.substring(1));
       // Reset visible
       this.visible = [];
@@ -218,7 +227,7 @@ export default {
           console.log('fetchMoreResult', fetchMoreResult);
           return { items: previousResult.items.concat(fetchMoreResult.items) };
         }
-        });
+      });
     },
     setDate(date) {
       this.$router.replace({ hash: date })
@@ -226,18 +235,11 @@ export default {
     onIntersect(entries, observer) {
       const entry = entries[0];
       if (entry.isIntersecting) {
-        // coming
-        console.log('intersect', this.visible);
 
-        // track visible
-        if (this.visible[0] > entry.target.dataset.date || this.visible.length === 0) {
-          // coming is new first (top) date
-          this.visible.unshift(entry.target.dataset.date);
-          this.setDate(this.visible[0]);
-        } else if (this.visible[this.visible.length -1] < entry.target.dataset.date) {
-          // coming is new last (bottom) date
-          this.visible.push(entry.target.dataset.date);
-        } 
+        if (entry.boundingClientRect.top <= 0) {
+          console.log('@', entry.target.dataset.date);
+          this.setDate(entry.target.dataset.date);
+        }
 
         // load more
         if (entry.target.dataset.first) {
@@ -253,13 +255,6 @@ export default {
           this.fetchMore(date.plus({ days: 1 }).toISODate(),
                          date.plus({ days: 5 }).toISODate());
         }
-      } else if (this.visible[0] === entry.target.dataset.date) {
-        // first (top) visible date is going
-        this.visible.shift()
-        this.setDate(this.visible[0]);
-      } else if (this.visible[this.visible.length - 1] === entry.target.dataset.date) {
-        // last (bottom) visible date is going
-        this.visible.pop()
       }
     },
   },
-- 
GitLab