diff --git a/src/modules/contents/posts/widgets/RecentPublishedWidget.vue b/src/modules/contents/posts/widgets/RecentPublishedWidget.vue index f9beeb8d..ec98eee2 100644 --- a/src/modules/contents/posts/widgets/RecentPublishedWidget.vue +++ b/src/modules/contents/posts/widgets/RecentPublishedWidget.vue @@ -6,16 +6,15 @@ import { VEntityField, IconExternalLinkLine, } from "@halo-dev/components"; -import { onMounted, ref } from "vue"; import type { ListedPost } from "@halo-dev/api-client"; import { apiClient } from "@/utils/api-client"; import { formatDatetime } from "@/utils/date"; import { postLabels } from "@/constants/labels"; +import { useQuery } from "@tanstack/vue-query"; -const posts = ref([] as ListedPost[]); - -const handleFetchPosts = async () => { - try { +const { data } = useQuery({ + queryKey: ["widget-recent-posts"], + queryFn: async () => { const { data } = await apiClient.post.listPosts({ labelSelector: [ `${postLabels.DELETED}=false`, @@ -26,13 +25,10 @@ const handleFetchPosts = async () => { page: 1, size: 10, }); - posts.value = data.items; - } catch (e) { - console.error("Failed to fetch posts", e); - } -}; - -onMounted(handleFetchPosts); + return data.items; + }, + refetchOnWindowFocus: false, +});