Disable "Show more posts" button on home timeline during loading

This commit is contained in:
silverpill 2022-12-01 20:56:01 +00:00
parent 9ca9aa233e
commit 8a911a5724
2 changed files with 8 additions and 3 deletions

View file

@ -8,6 +8,7 @@
<button <button
v-if="isPageFull()" v-if="isPageFull()"
class="btn secondary next-btn" class="btn secondary next-btn"
:disabled="isNextPageLoading"
@click="loadNextPage()" @click="loadNextPage()"
> >
Show more posts Show more posts
@ -16,6 +17,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { watch } from "vue" import { watch } from "vue"
import { $ref } from "vue/macros"
import { PAGE_SIZE } from "@/api/common" import { PAGE_SIZE } from "@/api/common"
import { Post as PostObject } from "@/api/posts" import { Post as PostObject } from "@/api/posts"
@ -31,11 +33,13 @@ const emit = defineEmits<{
}>() }>()
let initialPostCount: number | null = null let initialPostCount: number | null = null
let isNextPageLoading = $ref(false)
watch(() => props.posts.length, (postCount) => { watch(() => props.posts, (posts) => {
if (initialPostCount === null) { if (initialPostCount === null) {
initialPostCount = postCount initialPostCount = posts.length
} }
isNextPageLoading = false
}) })
function onPostDeleted(postId: string) { function onPostDeleted(postId: string) {
@ -51,6 +55,7 @@ function isPageFull(): boolean {
function loadNextPage() { function loadNextPage() {
if (props.posts.length > 0) { if (props.posts.length > 0) {
const maxId = props.posts[props.posts.length - 1].id const maxId = props.posts[props.posts.length - 1].id
isNextPageLoading = true
emit("load-next-page", maxId) emit("load-next-page", maxId)
} }
} }

View file

@ -38,7 +38,7 @@ function insertPost(post: Post) {
async function loadNextPage(maxId: string) { async function loadNextPage(maxId: string) {
const authToken = ensureAuthToken() const authToken = ensureAuthToken()
const nextPage = await getHomeTimeline(authToken, maxId) const nextPage = await getHomeTimeline(authToken, maxId)
posts.push(...nextPage) posts = [...posts, ...nextPage]
} }
</script> </script>