From 0c1eff85d202e69f8e52cbb2fc1a99602aae7a99 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 2 Jan 2019 21:56:44 -0700 Subject: [PATCH] Fix Timeline.vue, add proper infinite scroll --- resources/assets/js/components/Timeline.vue | 46 ++++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/resources/assets/js/components/Timeline.vue b/resources/assets/js/components/Timeline.vue index b2feb3298..acb04f004 100644 --- a/resources/assets/js/components/Timeline.vue +++ b/resources/assets/js/components/Timeline.vue @@ -2,11 +2,6 @@
-
-
- Loading... -
-
@@ -97,6 +92,10 @@
+ +
No more posts to load
+
No posts found
+
@@ -241,7 +240,6 @@ }, updated() { - this.scroll(); }, methods: { @@ -279,6 +277,32 @@ }); }, + infiniteTimeline($state) { + let homeTimeline = '/api/v1/timelines/home'; + let localTimeline = '/api/v1/timelines/public'; + let apiUrl = this.scope == '/' ? homeTimeline : localTimeline; + axios.get(apiUrl, { + params: { + page: this.page, + }, + }).then(res => { + if (res.data.length) { + $('.timeline .loader').addClass('d-none'); + let data = res.data; + this.feed.push(...data); + let ids = data.map(status => status.id); + this.min_id = Math.min(...ids); + if(this.page == 1) { + this.max_id = Math.max(...ids); + } + this.page += 1; + $state.loaded(); + } else { + $state.complete(); + } + }); + }, + fetchNotifications() { axios.get('/api/v1/notifications') .then(res => { @@ -288,16 +312,6 @@ }); }, - scroll() { - window.onscroll = () => { - let bottomOfWindow = document.documentElement.scrollTop + window.innerHeight == document.documentElement.offsetHeight; - - if (bottomOfWindow) { - this.fetchTimelineApi(); - } - }; - }, - reportUrl(status) { let type = status.in_reply_to ? 'comment' : 'post'; let id = status.id;