main
vue 87 lines 1.94 KB
Raw
1 <template>
2 <n-select
3 to="body"
4 :value="selectedStatus"
5 :options="statusOptions"
6 :loading
7 size="small"
8 class="min-w-36"
9 :consistent-menu-width="false"
10 @update:value="handleStatusChange"
11 />
12 </template>
13
14 <script setup lang="ts">
15 import type { AlertStatus } from "@/types/alerts"
16 import type { ApiError } from "@/types/common"
17 import { NSelect, useMessage } from "naive-ui"
18 import { ref, watch } from "vue"
19 import Api from "@/api"
20 import { getApiErrorMessage } from "@/utils"
21
22 export interface AlertStatusUpdateSuccessPayload {
23 alertId: number
24 status: AlertStatus
25 }
26
27 export interface AlertStatusUpdateErrorPayload {
28 alertId: number
29 status: AlertStatus
30 message: string
31 }
32
33 const props = defineProps<{
34 alertId: number
35 status: AlertStatus
36 }>()
37
38 const emit = defineEmits<{
39 success: [payload: AlertStatusUpdateSuccessPayload]
40 error: [payload: AlertStatusUpdateErrorPayload]
41 }>()
42
43 const message = useMessage()
44
45 const statusOptions = [
46 { label: "Open", value: "OPEN" },
47 { label: "In Progress", value: "IN_PROGRESS" },
48 { label: "Closed", value: "CLOSED" }
49 ]
50
51 const selectedStatus = ref<AlertStatus>(props.status)
52 const loading = ref(false)
53
54 watch(
55 () => props.status,
56 newStatus => {
57 selectedStatus.value = newStatus
58 }
59 )
60
61 async function handleStatusChange(value: string) {
62 if (loading.value || value === selectedStatus.value) return
63
64 const previousStatus = selectedStatus.value
65 selectedStatus.value = value as AlertStatus
66 loading.value = true
67
68 try {
69 await Api.alerts.updateAlertStatus(props.alertId, selectedStatus.value)
70 message.success(`Alert status updated to ${selectedStatus.value}`)
71 emit("success", {
72 alertId: props.alertId,
73 status: selectedStatus.value
74 })
75 } catch (err) {
76 selectedStatus.value = previousStatus
77 message.error(getApiErrorMessage(err as ApiError))
78 emit("error", {
79 alertId: props.alertId,
80 status: previousStatus,
81 message: getApiErrorMessage(err as ApiError)
82 })
83 } finally {
84 loading.value = false
85 }
86 }
87 </script>