main
ts 81 lines 2.02 KB
Raw
1 import type { DialogApiInjection } from "naive-ui/es/dialog/src/DialogProvider"
2 import type { MessageApiInjection } from "naive-ui/es/message/src/MessageProvider"
3 import type { Alert } from "@/types/incidentManagement/alerts.d"
4 import { h } from "vue"
5 import Api from "@/api"
6
7 export interface DeleteAlertParams {
8 alert: Alert
9 cbBefore?: () => void
10 cbSuccess?: () => void
11 cbAfter?: () => void
12 cbError?: () => void
13 message: MessageApiInjection
14 dialog: DialogApiInjection
15 }
16
17 export function handleDeleteAlert({
18 alert,
19 cbBefore,
20 cbSuccess,
21 cbAfter,
22 cbError,
23 dialog,
24 message
25 }: DeleteAlertParams) {
26 dialog.warning({
27 title: "Confirm",
28 content: () =>
29 h("div", {
30 innerHTML: `Are you sure you want to delete the Alert:<br/><strong>${alert.id} - ${alert.alert_name}</strong> ?`
31 }),
32 positiveText: "Yes I'm sure",
33 negativeText: "Cancel",
34 onPositiveClick: () => {
35 deleteAlert({ alert, cbBefore, cbSuccess, cbAfter, cbError, dialog, message })
36 },
37 onNegativeClick: () => {
38 message.info("Delete canceled")
39 }
40 })
41 }
42
43 export function deleteAlert({ alert, cbBefore, cbSuccess, cbAfter, cbError, message }: DeleteAlertParams) {
44 if (cbBefore && typeof cbBefore === "function") {
45 cbBefore()
46 }
47
48 Api.incidentManagement.alerts
49 .deleteAlert(alert.id)
50 .then(res => {
51 if (res.data.success) {
52 message.success("Alert was successfully deleted.")
53
54 if (cbSuccess && typeof cbSuccess === "function") {
55 cbSuccess()
56 }
57 } else {
58 message.error(res.data?.message || "An error occurred. Please try again later.")
59
60 if (cbError && typeof cbError === "function") {
61 cbError()
62 }
63 }
64 })
65 .catch(err => {
66 if (err.response?.status === 401) {
67 message.error(err.response?.data?.message || "Alert Delete returned Unauthorized.")
68 } else {
69 message.error(err.response?.data?.message || "An error occurred. Please try again later.")
70 }
71
72 if (cbError && typeof cbError === "function") {
73 cbError()
74 }
75 })
76 .finally(() => {
77 if (cbAfter && typeof cbAfter === "function") {
78 cbAfter()
79 }
80 })
81 }