| 1 | <template> |
| 2 | <div class="contents"> |
| 3 | <n-button :loading="loadingInvoke" type="success" secondary :size @click.stop="invoke()"> |
| 4 | <template #icon> |
| 5 | <Icon :name="InvokeIcon" /> |
| 6 | </template> |
| 7 | Invoke |
| 8 | </n-button> |
| 9 | <n-button :loading="loadingDelete" :size type="error" secondary @click.stop="handleDelete()"> |
| 10 | <template #icon> |
| 11 | <Icon :name="DeleteIcon" /> |
| 12 | </template> |
| 13 | Delete |
| 14 | </n-button> |
| 15 | </div> |
| 16 | </template> |
| 17 | |
| 18 | <script setup lang="ts"> |
| 19 | import type { ButtonSize } from "naive-ui" |
| 20 | import type { MonitoringAlert } from "@/types/monitoringAlerts.d" |
| 21 | import { NButton, useDialog, useMessage } from "naive-ui" |
| 22 | import { computed, ref, watch } from "vue" |
| 23 | import Api from "@/api" |
| 24 | import Icon from "@/components/common/Icon.vue" |
| 25 | |
| 26 | const { alert, size } = defineProps<{ |
| 27 | alert: MonitoringAlert |
| 28 | size?: ButtonSize |
| 29 | }>() |
| 30 | |
| 31 | const emit = defineEmits<{ |
| 32 | (e: "startInvoking"): void |
| 33 | (e: "stopInvoking"): void |
| 34 | (e: "startDeleting"): void |
| 35 | (e: "stopDeleting"): void |
| 36 | (e: "startLoading"): void |
| 37 | (e: "stopLoading"): void |
| 38 | (e: "invoked"): void |
| 39 | (e: "deleted"): void |
| 40 | }>() |
| 41 | |
| 42 | const DeleteIcon = "ph:trash" |
| 43 | const InvokeIcon = "carbon:play" |
| 44 | |
| 45 | const dialog = useDialog() |
| 46 | const message = useMessage() |
| 47 | const loadingInvoke = ref(false) |
| 48 | const loadingDelete = ref(false) |
| 49 | const loading = computed(() => loadingInvoke.value || loadingDelete.value) |
| 50 | |
| 51 | watch(loading, val => { |
| 52 | if (val) { |
| 53 | emit("startLoading") |
| 54 | } else { |
| 55 | emit("stopLoading") |
| 56 | } |
| 57 | }) |
| 58 | |
| 59 | watch(loadingInvoke, val => { |
| 60 | if (val) { |
| 61 | emit("startInvoking") |
| 62 | } else { |
| 63 | emit("stopInvoking") |
| 64 | } |
| 65 | }) |
| 66 | |
| 67 | watch(loadingDelete, val => { |
| 68 | if (val) { |
| 69 | emit("startDeleting") |
| 70 | } else { |
| 71 | emit("stopDeleting") |
| 72 | } |
| 73 | }) |
| 74 | |
| 75 | function invoke() { |
| 76 | loadingInvoke.value = true |
| 77 | |
| 78 | Api.monitoringAlerts |
| 79 | .invoke(alert.id) |
| 80 | .then(res => { |
| 81 | if (res.data.success) { |
| 82 | emit("invoked") |
| 83 | message.success(res.data?.message || "Alert invoked successfully") |
| 84 | } else { |
| 85 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 86 | } |
| 87 | }) |
| 88 | .catch(err => { |
| 89 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 90 | }) |
| 91 | .finally(() => { |
| 92 | loadingInvoke.value = false |
| 93 | }) |
| 94 | } |
| 95 | |
| 96 | function handleDelete() { |
| 97 | dialog.warning({ |
| 98 | title: "Confirm", |
| 99 | content: "This will delete the alert, are you sure you want to proceed?", |
| 100 | positiveText: "Yes I'm sure", |
| 101 | negativeText: "Cancel", |
| 102 | onPositiveClick: () => { |
| 103 | deleteAlert() |
| 104 | }, |
| 105 | onNegativeClick: () => { |
| 106 | message.info("Delete canceled") |
| 107 | } |
| 108 | }) |
| 109 | } |
| 110 | |
| 111 | function deleteAlert() { |
| 112 | loadingDelete.value = true |
| 113 | |
| 114 | Api.monitoringAlerts |
| 115 | .deleteAlert(alert.id) |
| 116 | .then(res => { |
| 117 | if (res.data.success) { |
| 118 | emit("deleted") |
| 119 | message.success(res.data?.message || "Alert deleted successfully.") |
| 120 | } else { |
| 121 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 122 | } |
| 123 | }) |
| 124 | .catch(err => { |
| 125 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 126 | }) |
| 127 | .finally(() => { |
| 128 | loadingDelete.value = false |
| 129 | }) |
| 130 | } |
| 131 | </script> |