mirror of https://github.com/halo-dev/halo
refactor: data fetching of dashboard widgets (#5280)
#### What type of PR is this? /area console /kind improvement /milestone 2.12.x #### What this PR does / why we need it: 重构 Console 仪表盘中小部件的数据获取方式。 #### Does this PR introduce a user-facing change? ```release-note None ```pull/5282/head
parent
a4e862cfc4
commit
831a19b86c
|
@ -0,0 +1,13 @@
|
||||||
|
import { apiClient } from "@/utils/api-client";
|
||||||
|
import { useQuery } from "@tanstack/vue-query";
|
||||||
|
|
||||||
|
export function useDashboardStats() {
|
||||||
|
const { data } = useQuery({
|
||||||
|
queryKey: ["dashboard-stats"],
|
||||||
|
queryFn: async () => {
|
||||||
|
const { data } = await apiClient.stats.getStats();
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return { data };
|
||||||
|
}
|
|
@ -1,9 +1,8 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import type { DashboardStats } from "@halo-dev/api-client";
|
import { useDashboardStats } from "@console/composables/use-dashboard-stats";
|
||||||
import { VCard, IconMessage } from "@halo-dev/components";
|
import { VCard, IconMessage } from "@halo-dev/components";
|
||||||
import { inject, type Ref } from "vue";
|
|
||||||
|
|
||||||
const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
|
const { data: stats } = useDashboardStats();
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<VCard class="h-full" :body-class="['h-full']">
|
<VCard class="h-full" :body-class="['h-full']">
|
||||||
|
@ -20,7 +19,7 @@ const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
|
||||||
{{ $t("core.dashboard.widgets.presets.comment_stats.title") }}
|
{{ $t("core.dashboard.widgets.presets.comment_stats.title") }}
|
||||||
</span>
|
</span>
|
||||||
<p class="text-2xl font-medium text-gray-900">
|
<p class="text-2xl font-medium text-gray-900">
|
||||||
{{ dashboardStats?.approvedComments }}
|
{{ stats?.approvedComments }}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,24 +1,23 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { VCard, IconPages } from "@halo-dev/components";
|
import { VCard, IconPages } from "@halo-dev/components";
|
||||||
import { onMounted, ref } from "vue";
|
|
||||||
import { apiClient } from "@/utils/api-client";
|
import { apiClient } from "@/utils/api-client";
|
||||||
import { singlePageLabels } from "@/constants/labels";
|
import { singlePageLabels } from "@/constants/labels";
|
||||||
|
import { useQuery } from "@tanstack/vue-query";
|
||||||
|
|
||||||
const singlePageTotal = ref<number>(0);
|
const { data: total } = useQuery({
|
||||||
|
queryKey: ["widget-singlePage-count"],
|
||||||
const handleFetchSinglePages = async () => {
|
queryFn: async () => {
|
||||||
const { data } = await apiClient.singlePage.listSinglePages({
|
const { data } = await apiClient.singlePage.listSinglePages({
|
||||||
labelSelector: [
|
labelSelector: [
|
||||||
`${singlePageLabels.DELETED}=false`,
|
`${singlePageLabels.DELETED}=false`,
|
||||||
`${singlePageLabels.PUBLISHED}=true`,
|
`${singlePageLabels.PUBLISHED}=true`,
|
||||||
],
|
],
|
||||||
page: 0,
|
page: 0,
|
||||||
size: 0,
|
size: 0,
|
||||||
});
|
});
|
||||||
singlePageTotal.value = data.total;
|
return data.total;
|
||||||
};
|
},
|
||||||
|
});
|
||||||
onMounted(handleFetchSinglePages);
|
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<VCard class="h-full" :body-class="['h-full']">
|
<VCard class="h-full" :body-class="['h-full']">
|
||||||
|
@ -35,7 +34,7 @@ onMounted(handleFetchSinglePages);
|
||||||
{{ $t("core.dashboard.widgets.presets.page_stats.title") }}
|
{{ $t("core.dashboard.widgets.presets.page_stats.title") }}
|
||||||
</span>
|
</span>
|
||||||
<p class="text-2xl font-medium text-gray-900">
|
<p class="text-2xl font-medium text-gray-900">
|
||||||
{{ singlePageTotal || 0 }}
|
{{ total || 0 }}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { VCard, IconBookRead } from "@halo-dev/components";
|
import { VCard, IconBookRead } from "@halo-dev/components";
|
||||||
import { inject, type Ref } from "vue";
|
import { useDashboardStats } from "@console/composables/use-dashboard-stats";
|
||||||
import type { DashboardStats } from "@halo-dev/api-client";
|
|
||||||
|
|
||||||
const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
|
const { data: stats } = useDashboardStats();
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<VCard class="h-full" :body-class="['h-full']">
|
<VCard class="h-full" :body-class="['h-full']">
|
||||||
|
@ -20,7 +19,7 @@ const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
|
||||||
{{ $t("core.dashboard.widgets.presets.post_stats.title") }}
|
{{ $t("core.dashboard.widgets.presets.post_stats.title") }}
|
||||||
</span>
|
</span>
|
||||||
<p class="text-2xl font-medium text-gray-900">
|
<p class="text-2xl font-medium text-gray-900">
|
||||||
{{ dashboardStats?.posts || 0 }}
|
{{ stats?.posts || 0 }}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -60,11 +60,12 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<VModal
|
<VModal
|
||||||
v-model:visible="widgetsModal"
|
v-if="widgetsModal"
|
||||||
height="calc(100vh - 20px)"
|
height="calc(100vh - 20px)"
|
||||||
:width="1280"
|
:width="1280"
|
||||||
:layer-closable="true"
|
:layer-closable="true"
|
||||||
:title="$t('core.dashboard.widgets.modal_title')"
|
:title="$t('core.dashboard.widgets.modal_title')"
|
||||||
|
@close="widgetsModal = false"
|
||||||
>
|
>
|
||||||
<VTabbar
|
<VTabbar
|
||||||
v-model:active-id="activeId"
|
v-model:active-id="activeId"
|
||||||
|
@ -121,11 +122,9 @@ import {
|
||||||
VSpace,
|
VSpace,
|
||||||
VTabbar,
|
VTabbar,
|
||||||
} from "@halo-dev/components";
|
} from "@halo-dev/components";
|
||||||
import { onMounted, provide, ref, type Ref } from "vue";
|
import { ref } from "vue";
|
||||||
import { useStorage } from "@vueuse/core";
|
import { useStorage } from "@vueuse/core";
|
||||||
import { cloneDeep } from "lodash-es";
|
import { cloneDeep } from "lodash-es";
|
||||||
import { apiClient } from "@/utils/api-client";
|
|
||||||
import type { DashboardStats } from "@halo-dev/api-client";
|
|
||||||
import { useI18n } from "vue-i18n";
|
import { useI18n } from "vue-i18n";
|
||||||
import { usePermission } from "@/utils/permission";
|
import { usePermission } from "@/utils/permission";
|
||||||
|
|
||||||
|
@ -271,25 +270,6 @@ function handleRemove(item: any) {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dashboard basic stats
|
|
||||||
|
|
||||||
const dashboardStats = ref<DashboardStats>({
|
|
||||||
posts: 0,
|
|
||||||
comments: 0,
|
|
||||||
approvedComments: 0,
|
|
||||||
users: 0,
|
|
||||||
visits: 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
provide<Ref<DashboardStats>>("dashboardStats", dashboardStats);
|
|
||||||
|
|
||||||
const handleFetchStats = async () => {
|
|
||||||
const { data } = await apiClient.stats.getStats();
|
|
||||||
dashboardStats.value = data;
|
|
||||||
};
|
|
||||||
|
|
||||||
onMounted(handleFetchStats);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import type { DashboardStats } from "@halo-dev/api-client";
|
import { useDashboardStats } from "@console/composables/use-dashboard-stats";
|
||||||
import { VCard, IconEye } from "@halo-dev/components";
|
import { VCard, IconEye } from "@halo-dev/components";
|
||||||
import { inject, type Ref } from "vue";
|
|
||||||
|
|
||||||
const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
|
const { data: stats } = useDashboardStats();
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<VCard class="h-full" :body-class="['h-full']">
|
<VCard class="h-full" :body-class="['h-full']">
|
||||||
|
@ -20,7 +19,7 @@ const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
|
||||||
{{ $t("core.dashboard.widgets.presets.views_stats.title") }}
|
{{ $t("core.dashboard.widgets.presets.views_stats.title") }}
|
||||||
</span>
|
</span>
|
||||||
<p class="text-2xl font-medium text-gray-900">
|
<p class="text-2xl font-medium text-gray-900">
|
||||||
{{ dashboardStats?.visits || 0 }}
|
{{ stats?.visits || 0 }}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { VCard, IconUserSettings } from "@halo-dev/components";
|
import { VCard, IconUserSettings } from "@halo-dev/components";
|
||||||
import { inject, type Ref } from "vue";
|
import { useDashboardStats } from "@console/composables/use-dashboard-stats";
|
||||||
import type { DashboardStats } from "@halo-dev/api-client";
|
|
||||||
|
|
||||||
const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
|
const { data: stats } = useDashboardStats();
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<VCard class="h-full" :body-class="['h-full']">
|
<VCard class="h-full" :body-class="['h-full']">
|
||||||
|
@ -20,7 +19,7 @@ const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
|
||||||
{{ $t("core.dashboard.widgets.presets.user_stats.title") }}
|
{{ $t("core.dashboard.widgets.presets.user_stats.title") }}
|
||||||
</span>
|
</span>
|
||||||
<p class="text-2xl font-medium text-gray-900">
|
<p class="text-2xl font-medium text-gray-900">
|
||||||
{{ dashboardStats?.users }}
|
{{ stats?.users }}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue