Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: data fetching of dashboard widgets #5280

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions console/console-src/composables/use-dashboard-stats.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<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 { inject, type Ref } from "vue";

const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
const { data: stats } = useDashboardStats();
</script>
<template>
<VCard class="h-full" :body-class="['h-full']">
Expand All @@ -20,7 +19,7 @@ const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
{{ $t("core.dashboard.widgets.presets.comment_stats.title") }}
</span>
<p class="text-2xl font-medium text-gray-900">
{{ dashboardStats?.approvedComments }}
{{ stats?.approvedComments }}
</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
<script lang="ts" setup>
import { VCard, IconPages } from "@halo-dev/components";
import { onMounted, ref } from "vue";
import { apiClient } from "@/utils/api-client";
import { singlePageLabels } from "@/constants/labels";
import { useQuery } from "@tanstack/vue-query";

const singlePageTotal = ref<number>(0);

const handleFetchSinglePages = async () => {
const { data } = await apiClient.singlePage.listSinglePages({
labelSelector: [
`${singlePageLabels.DELETED}=false`,
`${singlePageLabels.PUBLISHED}=true`,
],
page: 0,
size: 0,
});
singlePageTotal.value = data.total;
};

onMounted(handleFetchSinglePages);
const { data: total } = useQuery({
queryKey: ["widget-singlePage-count"],
queryFn: async () => {
const { data } = await apiClient.singlePage.listSinglePages({
labelSelector: [
`${singlePageLabels.DELETED}=false`,
`${singlePageLabels.PUBLISHED}=true`,
],
page: 0,
size: 0,
});
return data.total;
},
});
</script>
<template>
<VCard class="h-full" :body-class="['h-full']">
Expand All @@ -35,7 +34,7 @@ onMounted(handleFetchSinglePages);
{{ $t("core.dashboard.widgets.presets.page_stats.title") }}
</span>
<p class="text-2xl font-medium text-gray-900">
{{ singlePageTotal || 0 }}
{{ total || 0 }}
</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script lang="ts" setup>
import { VCard, IconBookRead } from "@halo-dev/components";
import { inject, type Ref } from "vue";
import type { DashboardStats } from "@halo-dev/api-client";
import { useDashboardStats } from "@console/composables/use-dashboard-stats";

const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
const { data: stats } = useDashboardStats();
</script>
<template>
<VCard class="h-full" :body-class="['h-full']">
Expand All @@ -20,7 +19,7 @@ const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
{{ $t("core.dashboard.widgets.presets.post_stats.title") }}
</span>
<p class="text-2xl font-medium text-gray-900">
{{ dashboardStats?.posts || 0 }}
{{ stats?.posts || 0 }}
</p>
</div>
</div>
Expand Down
26 changes: 3 additions & 23 deletions console/console-src/modules/dashboard/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@
</div>

<VModal
v-model:visible="widgetsModal"
v-if="widgetsModal"
height="calc(100vh - 20px)"
:width="1280"
:layer-closable="true"
:title="$t('core.dashboard.widgets.modal_title')"
@close="widgetsModal = false"
>
<VTabbar
v-model:active-id="activeId"
Expand Down Expand Up @@ -121,11 +122,9 @@ import {
VSpace,
VTabbar,
} from "@halo-dev/components";
import { onMounted, provide, ref, type Ref } from "vue";
import { ref } from "vue";
import { useStorage } from "@vueuse/core";
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 { usePermission } from "@/utils/permission";

Expand Down Expand Up @@ -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>

<style>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<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 { inject, type Ref } from "vue";

const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
const { data: stats } = useDashboardStats();
</script>
<template>
<VCard class="h-full" :body-class="['h-full']">
Expand All @@ -20,7 +19,7 @@ const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
{{ $t("core.dashboard.widgets.presets.views_stats.title") }}
</span>
<p class="text-2xl font-medium text-gray-900">
{{ dashboardStats?.visits || 0 }}
{{ stats?.visits || 0 }}
</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script lang="ts" setup>
import { VCard, IconUserSettings } from "@halo-dev/components";
import { inject, type Ref } from "vue";
import type { DashboardStats } from "@halo-dev/api-client";
import { useDashboardStats } from "@console/composables/use-dashboard-stats";

const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
const { data: stats } = useDashboardStats();
</script>
<template>
<VCard class="h-full" :body-class="['h-full']">
Expand All @@ -20,7 +19,7 @@ const dashboardStats = inject<Ref<DashboardStats>>("dashboardStats");
{{ $t("core.dashboard.widgets.presets.user_stats.title") }}
</span>
<p class="text-2xl font-medium text-gray-900">
{{ dashboardStats?.users }}
{{ stats?.users }}
</p>
</div>
</div>
Expand Down
Loading