| 1 | <template> |
| 2 | <n-popover v-for="linkedCase of linkedCases" :key="linkedCase.id" placement="top" to="body" trigger="click"> |
| 3 | <template #trigger> |
| 4 | <code class="text-primary cursor-pointer"> |
| 5 | #{{ linkedCase.id }} |
| 6 | <Icon :name="MenuIcon" :size="14" class="relative top-0.5" /> |
| 7 | </code> |
| 8 | </template> |
| 9 | <div class="flex flex-col gap-3 px-1 py-2"> |
| 10 | <CaseItem |
| 11 | :case-data="{ ...linkedCase, alerts: alert ? [alert] : [] }" |
| 12 | compact |
| 13 | embedded |
| 14 | @click="routeIncidentManagementCases(linkedCase.id).navigate()" |
| 15 | /> |
| 16 | <div class="flex items-center justify-end gap-3"> |
| 17 | <n-button |
| 18 | size="small" |
| 19 | secondary |
| 20 | type="warning" |
| 21 | :loading="loadingId === linkedCase.id" |
| 22 | @click="unlink(linkedCase.id)" |
| 23 | > |
| 24 | <template #icon> |
| 25 | <Icon :name="UnlinkIcon" /> |
| 26 | </template> |
| 27 | Unlink Case |
| 28 | </n-button> |
| 29 | </div> |
| 30 | </div> |
| 31 | </n-popover> |
| 32 | </template> |
| 33 | |
| 34 | <script setup lang="ts"> |
| 35 | import type { Alert } from "@/types/incidentManagement/alerts.d" |
| 36 | import { NButton, NPopover, useMessage } from "naive-ui" |
| 37 | import { computed, defineAsyncComponent, ref, watch } from "vue" |
| 38 | import Api from "@/api" |
| 39 | import Icon from "@/components/common/Icon.vue" |
| 40 | import { useNavigation } from "@/composables/useNavigation" |
| 41 | |
| 42 | const props = defineProps<{ alert: Alert }>() |
| 43 | |
| 44 | const emit = defineEmits<{ |
| 45 | (e: "updated", value: Alert): void |
| 46 | (e: "unlinked"): void |
| 47 | }>() |
| 48 | |
| 49 | const CaseItem = defineAsyncComponent(() => import("../cases/CaseItem.vue")) |
| 50 | |
| 51 | const UnlinkIcon = "carbon:unlink" |
| 52 | const MenuIcon = "carbon:overflow-menu-horizontal" |
| 53 | const loadingId = ref<number | false>(false) |
| 54 | const alert = ref<Alert>(props.alert) |
| 55 | const message = useMessage() |
| 56 | const { routeIncidentManagementCases } = useNavigation() |
| 57 | const linkedCases = computed(() => alert.value?.linked_cases || []) |
| 58 | |
| 59 | function updateAlert(updatedAlert: Alert) { |
| 60 | alert.value = updatedAlert |
| 61 | emit("updated", updatedAlert) |
| 62 | } |
| 63 | |
| 64 | function unlink(caseId: number) { |
| 65 | if (!alert.value) return |
| 66 | |
| 67 | loadingId.value = caseId |
| 68 | |
| 69 | Api.incidentManagement.cases |
| 70 | .unlinkCase(alert.value.id, caseId) |
| 71 | .then(res => { |
| 72 | if (res.data.success) { |
| 73 | updateAlert({ |
| 74 | ...alert.value, |
| 75 | linked_cases: alert.value.linked_cases.filter(c => c.id !== caseId) |
| 76 | }) |
| 77 | |
| 78 | emit("unlinked") |
| 79 | message.success(res.data?.message || "Case unlinked successfully") |
| 80 | } else { |
| 81 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 82 | } |
| 83 | }) |
| 84 | .catch(err => { |
| 85 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 86 | }) |
| 87 | .finally(() => { |
| 88 | loadingId.value = false |
| 89 | }) |
| 90 | } |
| 91 | |
| 92 | watch( |
| 93 | () => props.alert, |
| 94 | () => { |
| 95 | alert.value = props.alert |
| 96 | }, |
| 97 | { |
| 98 | immediate: true |
| 99 | } |
| 100 | ) |
| 101 | </script> |