| 1 | <template> |
| 2 | <n-popselect |
| 3 | v-model:value="statusSelected" |
| 4 | v-model:show="listVisible" |
| 5 | :options="statusOptions" |
| 6 | :disabled="loading" |
| 7 | size="medium" |
| 8 | scrollable |
| 9 | to="body" |
| 10 | > |
| 11 | <slot :loading /> |
| 12 | </n-popselect> |
| 13 | </template> |
| 14 | |
| 15 | <script setup lang="ts"> |
| 16 | import type { Alert, AlertStatus } from "@/types/incidentManagement/alerts.d" |
| 17 | import { NPopselect, useMessage } from "naive-ui" |
| 18 | import { computed, onBeforeMount, ref, toRefs, watch } from "vue" |
| 19 | import Api from "@/api" |
| 20 | |
| 21 | const props = defineProps<{ |
| 22 | alert: Alert |
| 23 | }>() |
| 24 | const emit = defineEmits<{ |
| 25 | (e: "updated", value: Alert): void |
| 26 | }>() |
| 27 | |
| 28 | const { alert } = toRefs(props) |
| 29 | |
| 30 | const loading = ref(false) |
| 31 | const message = useMessage() |
| 32 | const listVisible = ref(false) |
| 33 | const status = computed(() => alert.value.status) |
| 34 | const statusOptions = ref< |
| 35 | { |
| 36 | label: string |
| 37 | value: AlertStatus |
| 38 | }[] |
| 39 | >([ |
| 40 | { label: "Open", value: "OPEN" }, |
| 41 | { label: "In progress", value: "IN_PROGRESS" }, |
| 42 | { label: "Closed", value: "CLOSED" } |
| 43 | ]) |
| 44 | const statusSelected = ref<AlertStatus | null>(null) |
| 45 | |
| 46 | function updateStatus() { |
| 47 | if (statusSelected.value && statusSelected.value !== status.value) { |
| 48 | loading.value = true |
| 49 | |
| 50 | Api.incidentManagement.alerts |
| 51 | .updateAlertStatus(alert.value.id, statusSelected.value) |
| 52 | .then(res => { |
| 53 | if (res.data.success && statusSelected.value) { |
| 54 | emit("updated", { ...alert.value, status: statusSelected.value }) |
| 55 | } else { |
| 56 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 57 | } |
| 58 | }) |
| 59 | .catch(err => { |
| 60 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 61 | }) |
| 62 | .finally(() => { |
| 63 | loading.value = false |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | watch(statusSelected, () => { |
| 69 | updateStatus() |
| 70 | }) |
| 71 | |
| 72 | onBeforeMount(() => { |
| 73 | if (status.value) { |
| 74 | statusSelected.value = status.value |
| 75 | } |
| 76 | }) |
| 77 | </script> |