| 1 | <template> |
| 2 | <n-button secondary type="primary" :loading="creating" @click="createCase()"> |
| 3 | <template #icon> |
| 4 | <Icon :name="DangerIcon" /> |
| 5 | </template> |
| 6 | Create case |
| 7 | </n-button> |
| 8 | </template> |
| 9 | |
| 10 | <script setup lang="ts"> |
| 11 | import type { Alert } from "@/types/incidentManagement/alerts.d" |
| 12 | import { NButton, useMessage } from "naive-ui" |
| 13 | import { ref, toRefs } from "vue" |
| 14 | import Api from "@/api" |
| 15 | import Icon from "@/components/common/Icon.vue" |
| 16 | |
| 17 | const props = defineProps<{ alert: Alert }>() |
| 18 | const emit = defineEmits<{ |
| 19 | (e: "updated", value: Alert): void |
| 20 | }>() |
| 21 | |
| 22 | const { alert } = toRefs(props) |
| 23 | |
| 24 | const DangerIcon = "majesticons:exclamation-line" |
| 25 | |
| 26 | const message = useMessage() |
| 27 | const creating = ref(false) |
| 28 | |
| 29 | function updateAlert(updatedAlert: Alert) { |
| 30 | emit("updated", updatedAlert) |
| 31 | } |
| 32 | |
| 33 | function createCase() { |
| 34 | creating.value = true |
| 35 | |
| 36 | Api.incidentManagement.cases |
| 37 | .createCaseFromAlert(alert.value.id) |
| 38 | .then(res => { |
| 39 | if (res.data.success) { |
| 40 | updateAlert({ |
| 41 | ...alert.value, |
| 42 | linked_cases: [ |
| 43 | { |
| 44 | id: res.data.case_alert_link.case_id, |
| 45 | case_name: "", |
| 46 | case_description: "", |
| 47 | case_creation_time: new Date(), |
| 48 | assigned_to: null, |
| 49 | case_status: null, |
| 50 | customer_code: null, |
| 51 | comments: [] |
| 52 | } |
| 53 | ] |
| 54 | }) |
| 55 | message.success(res.data?.message || "Case created successfully") |
| 56 | } else { |
| 57 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 58 | } |
| 59 | }) |
| 60 | .catch(err => { |
| 61 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 62 | }) |
| 63 | .finally(() => { |
| 64 | creating.value = false |
| 65 | }) |
| 66 | } |
| 67 | </script> |