main
vue 76 lines 1.97 KB
Raw
1 <template>
2 <n-spin :show="loading">
3 <div class="grid grid-cols-1 gap-6 @xl:grid-cols-2 @4xl:grid-cols-4">
4 <CardStats title="Total" :value="stats.total">
5 <template #icon>
6 <Icon :name="ICONS.alerts" :size="24" class="text-info" />
7 </template>
8 </CardStats>
9
10 <CardStats title="Open" :value="stats.open">
11 <template #icon>
12 <Icon name="carbon:warning" :size="24" class="text-error" />
13 </template>
14 </CardStats>
15
16 <CardStats title="In Progress" :value="stats.in_progress">
17 <template #icon>
18 <Icon name="carbon:hourglass" :size="24" class="text-warning" />
19 </template>
20 </CardStats>
21
22 <CardStats title="Closed" :value="stats.closed">
23 <template #icon>
24 <Icon name="carbon:checkmark-outline" :size="24" class="text-success" />
25 </template>
26 </CardStats>
27 </div>
28 </n-spin>
29 </template>
30
31 <script setup lang="ts">
32 import type { ApiError } from "@/types/common"
33 import type { AlertsStats } from "@/types/portal"
34 import { NSpin, useMessage } from "naive-ui"
35 import { onBeforeMount, ref, watch } from "vue"
36 import Api from "@/api"
37 import CardStats from "@/components/common/cards/CardStats.vue"
38 import Icon from "@/components/common/Icon.vue"
39 import { ICONS } from "@/const"
40 import { useCustomerFilterStore } from "@/stores/customerFilter"
41 import { getApiErrorMessage } from "@/utils"
42
43 const stats = ref<AlertsStats>({
44 total: 0,
45 open: 0,
46 in_progress: 0,
47 closed: 0
48 })
49
50 const loading = ref(false)
51 const message = useMessage()
52 const customerFilterStore = useCustomerFilterStore()
53
54 function fetchStats() {
55 loading.value = true
56
57 Api.portal
58 .alertsStats(customerFilterStore.queryCustomerCodes)
59 .then(res => {
60 stats.value = res.data
61 })
62 .catch(err => {
63 message.error(getApiErrorMessage(err as ApiError))
64 })
65 .finally(() => {
66 loading.value = false
67 })
68 }
69
70 onBeforeMount(() => {
71 fetchStats()
72 })
73
74 // Refetch whenever the global customer filter changes.
75 watch(() => customerFilterStore.selectedCustomerCodes, fetchStats, { deep: true })
76 </script>