| 1 | <template> |
| 2 | <div class="contents"> |
| 3 | <n-button |
| 4 | v-if="isCaseClosed" |
| 5 | :loading="loadingCaseReopen" |
| 6 | :size |
| 7 | type="warning" |
| 8 | secondary |
| 9 | @click.stop="reopenCase()" |
| 10 | > |
| 11 | <template #icon> |
| 12 | <Icon :name="OpenIcon" /> |
| 13 | </template> |
| 14 | Reopen |
| 15 | </n-button> |
| 16 | <n-button v-else :loading="loadingCaseClose" type="success" secondary :size @click.stop="closeCase()"> |
| 17 | <template #icon> |
| 18 | <Icon :name="CloseIcon" /> |
| 19 | </template> |
| 20 | Close |
| 21 | </n-button> |
| 22 | <n-button :loading="loadingCaseDelete" :size type="error" secondary @click.stop="handleDelete()"> |
| 23 | <template #icon> |
| 24 | <Icon :name="DeleteIcon" /> |
| 25 | </template> |
| 26 | Delete |
| 27 | </n-button> |
| 28 | </div> |
| 29 | </template> |
| 30 | |
| 31 | <script setup lang="ts"> |
| 32 | import type { ButtonSize } from "naive-ui" |
| 33 | import type { SocCase, SocCaseExt } from "@/types/soc/case.d" |
| 34 | import { NButton, useDialog, useMessage } from "naive-ui" |
| 35 | import { computed, ref, watch } from "vue" |
| 36 | import Api from "@/api" |
| 37 | import Icon from "@/components/common/Icon.vue" |
| 38 | import { StateName } from "@/types/soc/case.d" |
| 39 | |
| 40 | const { caseData, size } = defineProps<{ |
| 41 | caseData: SocCase | SocCaseExt | null |
| 42 | size?: ButtonSize |
| 43 | }>() |
| 44 | |
| 45 | const emit = defineEmits<{ |
| 46 | (e: "closed"): void |
| 47 | (e: "reopened"): void |
| 48 | (e: "deleted"): void |
| 49 | (e: "startDeleting"): void |
| 50 | (e: "startLoading"): void |
| 51 | (e: "stopLoading"): void |
| 52 | }>() |
| 53 | |
| 54 | const DeleteIcon = "ph:trash" |
| 55 | const CloseIcon = "ph:circle-wavy-check" |
| 56 | const OpenIcon = "ph:circle-wavy-warning" |
| 57 | |
| 58 | const dialog = useDialog() |
| 59 | const message = useMessage() |
| 60 | const loadingCaseClose = ref(false) |
| 61 | const loadingCaseReopen = ref(false) |
| 62 | const loadingCaseDelete = ref(false) |
| 63 | const loading = computed(() => loadingCaseClose.value || loadingCaseReopen.value || loadingCaseDelete.value) |
| 64 | |
| 65 | const isCaseClosed = computed(() => caseData?.state_name === StateName.Closed) |
| 66 | |
| 67 | watch(loading, val => { |
| 68 | if (val) { |
| 69 | emit("startLoading") |
| 70 | } else { |
| 71 | emit("stopLoading") |
| 72 | } |
| 73 | }) |
| 74 | |
| 75 | function closeCase() { |
| 76 | if (caseData?.case_id) { |
| 77 | loadingCaseClose.value = true |
| 78 | |
| 79 | Api.soc |
| 80 | .closeCase(caseData.case_id.toString()) |
| 81 | .then(res => { |
| 82 | if (res.data.success) { |
| 83 | emit("closed") |
| 84 | message.success(res.data?.message || "SOC Case closed.") |
| 85 | } else { |
| 86 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 87 | } |
| 88 | }) |
| 89 | .catch(err => { |
| 90 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 91 | }) |
| 92 | .finally(() => { |
| 93 | loadingCaseClose.value = false |
| 94 | }) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | function reopenCase() { |
| 99 | if (caseData?.case_id) { |
| 100 | loadingCaseReopen.value = true |
| 101 | |
| 102 | Api.soc |
| 103 | .reopenCase(caseData.case_id.toString()) |
| 104 | .then(res => { |
| 105 | if (res.data.success) { |
| 106 | emit("reopened") |
| 107 | message.success(res.data?.message || "SOC Case reopened.") |
| 108 | } else { |
| 109 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 110 | } |
| 111 | }) |
| 112 | .catch(err => { |
| 113 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 114 | }) |
| 115 | .finally(() => { |
| 116 | loadingCaseReopen.value = false |
| 117 | }) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | function handleDelete() { |
| 122 | dialog.warning({ |
| 123 | title: "Confirm", |
| 124 | content: "This will delete the case, are you sure you want to proceed?", |
| 125 | positiveText: "Yes I'm sure", |
| 126 | negativeText: "Cancel", |
| 127 | onPositiveClick: () => { |
| 128 | deleteCase() |
| 129 | }, |
| 130 | onNegativeClick: () => { |
| 131 | message.info("Delete canceled") |
| 132 | } |
| 133 | }) |
| 134 | } |
| 135 | |
| 136 | function deleteCase() { |
| 137 | if (caseData?.case_id) { |
| 138 | loadingCaseDelete.value = true |
| 139 | emit("startDeleting") |
| 140 | |
| 141 | Api.soc |
| 142 | .deleteCase(caseData.case_id.toString()) |
| 143 | .then(res => { |
| 144 | if (res.data.success) { |
| 145 | message.success(res.data?.message || "SOC Case deleted.") |
| 146 | } else { |
| 147 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 148 | } |
| 149 | }) |
| 150 | .catch(err => { |
| 151 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 152 | }) |
| 153 | .finally(() => { |
| 154 | emit("deleted") |
| 155 | loadingCaseDelete.value = false |
| 156 | }) |
| 157 | } |
| 158 | } |
| 159 | </script> |