refactor: RecentPublishedWidget

pull/879/head
Ryan Wang 2023-02-23 10:57:36 +08:00
parent 830b38cecc
commit c0e76bb187
1 changed files with 9 additions and 13 deletions

View File

@ -6,16 +6,15 @@ import {
VEntityField, VEntityField,
IconExternalLinkLine, IconExternalLinkLine,
} from "@halo-dev/components"; } from "@halo-dev/components";
import { onMounted, ref } from "vue";
import type { ListedPost } from "@halo-dev/api-client"; import type { ListedPost } from "@halo-dev/api-client";
import { apiClient } from "@/utils/api-client"; import { apiClient } from "@/utils/api-client";
import { formatDatetime } from "@/utils/date"; import { formatDatetime } from "@/utils/date";
import { postLabels } from "@/constants/labels"; import { postLabels } from "@/constants/labels";
import { useQuery } from "@tanstack/vue-query";
const posts = ref<ListedPost[]>([] as ListedPost[]); const { data } = useQuery<ListedPost[]>({
queryKey: ["widget-recent-posts"],
const handleFetchPosts = async () => { queryFn: async () => {
try {
const { data } = await apiClient.post.listPosts({ const { data } = await apiClient.post.listPosts({
labelSelector: [ labelSelector: [
`${postLabels.DELETED}=false`, `${postLabels.DELETED}=false`,
@ -26,13 +25,10 @@ const handleFetchPosts = async () => {
page: 1, page: 1,
size: 10, size: 10,
}); });
posts.value = data.items; return data.items;
} catch (e) { },
console.error("Failed to fetch posts", e); refetchOnWindowFocus: false,
} });
};
onMounted(handleFetchPosts);
</script> </script>
<template> <template>
<VCard <VCard
@ -41,7 +37,7 @@ onMounted(handleFetchPosts);
title="最近文章" title="最近文章"
> >
<ul class="box-border h-full w-full divide-y divide-gray-100" role="list"> <ul class="box-border h-full w-full divide-y divide-gray-100" role="list">
<li v-for="(post, index) in posts" :key="index"> <li v-for="(post, index) in data" :key="index">
<VEntity> <VEntity>
<template #start> <template #start>
<VEntityField <VEntityField