Reload home timeline when clicking on "Home" button

This commit is contained in:
silverpill 2023-02-03 23:07:31 +00:00
parent b0f7042008
commit f71b04e3d8
3 changed files with 41 additions and 12 deletions

View file

@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased] ## [Unreleased]
### Added
- Reload home timeline when clicking on "Home" button.
### Changed ### Changed
- Improved username validation. - Improved username validation.

View file

@ -2,7 +2,11 @@
<header v-if="currentUser !== null"> <header v-if="currentUser !== null">
<div id="header"> <div id="header">
<div id="nav"> <div id="nav">
<router-link to="/" class="home-btn"> <router-link
class="home-btn"
:to="{ name: 'home' }"
@click.prevent="showHomeTimeline()"
>
<img :src="require('@/assets/feather/home.svg')"> <img :src="require('@/assets/feather/home.svg')">
<span>Home</span> <span>Home</span>
</router-link> </router-link>
@ -32,14 +36,31 @@
<script setup lang="ts"> <script setup lang="ts">
import { $ } from "vue/macros" import { $ } from "vue/macros"
import { useRoute, useRouter } from "vue-router"
import { useCurrentUser } from "@/store/user"
import Avatar from "@/components/Avatar.vue" import Avatar from "@/components/Avatar.vue"
import InstanceInfo from "@/components/InstanceInfo.vue" import InstanceInfo from "@/components/InstanceInfo.vue"
import Search from "@/components/Search.vue" import Search from "@/components/Search.vue"
import Sidebar from "@/components/Sidebar.vue" import Sidebar from "@/components/Sidebar.vue"
import { useCurrentUser } from "@/store/user"
import { useNotifications } from "@/store/notifications"
const { currentUser } = $(useCurrentUser()) const route = useRoute()
const router = useRouter()
const { currentUser, ensureAuthToken } = $(useCurrentUser())
const { loadNotifications } = $(useNotifications())
/* eslint-disable-next-line no-undef */
const emit = defineEmits<{(event: "reload-home"): void}>()
function showHomeTimeline() {
if (route.name === "home") {
loadNotifications(ensureAuthToken())
emit("reload-home")
} else {
router.push({ name: "home" })
}
}
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">

View file

@ -1,13 +1,13 @@
<template> <template>
<sidebar-layout> <sidebar-layout @reload-home="loadTimeline">
<template #content> <template #content>
<post-editor <post-editor
v-if="canCreatePost()" v-if="canCreatePost()"
:in-reply-to="null" :in-reply-to="null"
@post-created="insertPost" @post-created="insertPost"
></post-editor> ></post-editor>
<post-list :posts="posts" @load-next-page="loadNextPage"></post-list>
<loader v-if="isLoading"></loader> <loader v-if="isLoading"></loader>
<post-list :posts="posts" @load-next-page="loadNextPage"></post-list>
</template> </template>
</sidebar-layout> </sidebar-layout>
</template> </template>
@ -29,13 +29,6 @@ const { ensureAuthToken, ensureCurrentUser } = useCurrentUser()
let posts = $ref<Post[]>([]) let posts = $ref<Post[]>([])
let isLoading = $ref(false) let isLoading = $ref(false)
onMounted(async () => {
isLoading = true
const authToken = ensureAuthToken()
posts = await getHomeTimeline(authToken)
isLoading = false
})
function canCreatePost(): boolean { function canCreatePost(): boolean {
const user = ensureCurrentUser() const user = ensureCurrentUser()
return user.role.permissions.includes(Permissions.CreatePost) return user.role.permissions.includes(Permissions.CreatePost)
@ -45,11 +38,22 @@ function insertPost(post: Post) {
posts = [post, ...posts] posts = [post, ...posts]
} }
async function loadTimeline() {
isLoading = true
const authToken = ensureAuthToken()
posts = await getHomeTimeline(authToken)
isLoading = false
}
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 = [...posts, ...nextPage] posts = [...posts, ...nextPage]
} }
onMounted(async () => {
await loadTimeline()
})
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">