Enable pagination on home timeline

This commit is contained in:
silverpill 2021-12-02 01:23:01 +00:00
parent 927754bebf
commit ffb8270b3c
2 changed files with 39 additions and 4 deletions

View file

@ -44,9 +44,20 @@ export interface Post {
token_tx_id: string | null;
}
export async function getPosts(authToken: string): Promise<Post[]> {
export async function getHomeTimeline(
authToken: string,
maxId?: string,
): Promise<Post[]> {
const url = `${BACKEND_URL}/api/v1/timelines/home`
const response = await http(url, { authToken })
let queryParams
if (maxId) {
queryParams = { max_id: maxId }
}
const response = await http(url, {
method: "GET",
queryParams,
authToken,
})
const data = await response.json()
return data
}

View file

@ -3,6 +3,13 @@
<div class="content posts">
<post-editor @post-created="insertPost"></post-editor>
<post-or-repost v-for="post in posts" :post="post" :key="post.id"></post-or-repost>
<button
v-if="isPageFull()"
class="btn"
@click="loadNextPage()"
>
Show more posts
</button>
</div>
<sidebar></sidebar>
</div>
@ -11,13 +18,15 @@
<script lang="ts">
import { Options, Vue, setup } from "vue-class-component"
import { Post, getPosts } from "@/api/posts"
import { Post, getHomeTimeline } from "@/api/posts"
import Avatar from "@/components/Avatar.vue"
import PostOrRepost from "@/components/PostOrRepost.vue"
import PostEditor from "@/components/PostEditor.vue"
import Sidebar from "@/components/Sidebar.vue"
import { useCurrentUser } from "@/store/user"
const PAGE_SIZE = 20
@Options({
components: {
Avatar,
@ -37,13 +46,28 @@ export default class PostList extends Vue {
async created() {
const authToken = this.store.ensureAuthToken()
this.posts = await getPosts(authToken)
this.posts = await getHomeTimeline(authToken)
}
insertPost(post: Post) {
this.posts = [post, ...this.posts]
}
isPageFull(): boolean {
return this.posts.length >= PAGE_SIZE
}
async loadNextPage() {
if (this.posts.length > 0) {
const authToken = this.store.ensureAuthToken()
const posts = await getHomeTimeline(
authToken,
this.posts[this.posts.length - 1].id,
)
this.posts.push(...posts)
}
}
}
</script>