| 1 | <template> |
| 2 | <n-select |
| 3 | to="body" |
| 4 | :value="selectedCritical" |
| 5 | :options="criticalOptions" |
| 6 | :loading |
| 7 | size="small" |
| 8 | class="min-w-36" |
| 9 | :status="selectedCritical === 'CRITICAL' ? 'error' : undefined" |
| 10 | :consistent-menu-width="false" |
| 11 | @update:value="handleCriticalChange" |
| 12 | /> |
| 13 | </template> |
| 14 | |
| 15 | <script setup lang="ts"> |
| 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 AgentCriticalUpdateSuccessPayload { |
| 23 | agentId: string | number |
| 24 | critical: boolean |
| 25 | } |
| 26 | |
| 27 | export interface AgentCriticalUpdateErrorPayload { |
| 28 | agentId: string | number |
| 29 | critical: boolean |
| 30 | message: string |
| 31 | } |
| 32 | |
| 33 | const props = defineProps<{ |
| 34 | agentId: string | number |
| 35 | critical: boolean |
| 36 | }>() |
| 37 | |
| 38 | const emit = defineEmits<{ |
| 39 | success: [AgentCriticalUpdateSuccessPayload] |
| 40 | error: [AgentCriticalUpdateErrorPayload] |
| 41 | }>() |
| 42 | |
| 43 | const message = useMessage() |
| 44 | |
| 45 | const criticalOptions = [ |
| 46 | { label: "Critical", value: "CRITICAL" }, |
| 47 | { label: "Not Critical", value: "NOT_CRITICAL" } |
| 48 | ] |
| 49 | |
| 50 | const selectedCritical = ref<string>(props.critical ? "CRITICAL" : "NOT_CRITICAL") |
| 51 | const loading = ref(false) |
| 52 | |
| 53 | watch( |
| 54 | () => props.critical, |
| 55 | newCritical => { |
| 56 | selectedCritical.value = newCritical ? "CRITICAL" : "NOT_CRITICAL" |
| 57 | } |
| 58 | ) |
| 59 | |
| 60 | async function handleCriticalChange(value: string) { |
| 61 | if (loading.value || value === selectedCritical.value) return |
| 62 | |
| 63 | const previousCritical = selectedCritical.value |
| 64 | selectedCritical.value = value |
| 65 | loading.value = true |
| 66 | |
| 67 | try { |
| 68 | if (selectedCritical.value === "CRITICAL") { |
| 69 | await Api.agents.markAgentAsCritical(props.agentId.toString()) |
| 70 | } else { |
| 71 | await Api.agents.markAgentAsNotCritical(props.agentId.toString()) |
| 72 | } |
| 73 | message.success( |
| 74 | `Agent criticality updated to ${selectedCritical.value === "CRITICAL" ? "Critical" : "Not Critical"}` |
| 75 | ) |
| 76 | emit("success", { |
| 77 | agentId: props.agentId, |
| 78 | critical: selectedCritical.value === "CRITICAL" |
| 79 | }) |
| 80 | } catch (err) { |
| 81 | selectedCritical.value = previousCritical |
| 82 | message.error(getApiErrorMessage(err as ApiError)) |
| 83 | emit("error", { |
| 84 | agentId: props.agentId, |
| 85 | critical: previousCritical === "CRITICAL", |
| 86 | message: getApiErrorMessage(err as ApiError) |
| 87 | }) |
| 88 | } finally { |
| 89 | loading.value = false |
| 90 | } |
| 91 | } |
| 92 | </script> |