| 1 | <template> |
| 2 | <n-spin :show="loading || submitting" class="min-h-20"> |
| 3 | <SourceConfigurationViewer v-if="sourceConfiguration" v-show="!editing" :source-configuration /> |
| 4 | |
| 5 | <div v-if="!editing && !loading" class="mt-4 flex items-center justify-end gap-2"> |
| 6 | <n-button @click="setEditMode()"> |
| 7 | <template #icon> |
| 8 | <Icon :name="EditIcon" :size="16" /> |
| 9 | </template> |
| 10 | Edit |
| 11 | </n-button> |
| 12 | </div> |
| 13 | |
| 14 | <SourceConfigurationForm |
| 15 | v-if="sourceConfiguration" |
| 16 | v-show="editing" |
| 17 | :source-configuration-model="sourceConfiguration" |
| 18 | show-index-name-field |
| 19 | @mounted="formCTX = $event" |
| 20 | @submitted="updateSourceConfiguration($event)" |
| 21 | > |
| 22 | <template #additionalActions> |
| 23 | <n-button @click="setViewMode()"> |
| 24 | <template #icon> |
| 25 | <Icon :name="ArrowIcon" :size="16" /> |
| 26 | </template> |
| 27 | Cancel |
| 28 | </n-button> |
| 29 | </template> |
| 30 | </SourceConfigurationForm> |
| 31 | </n-spin> |
| 32 | </template> |
| 33 | |
| 34 | <script setup lang="ts"> |
| 35 | import type { ApiError } from "@/types/common.d" |
| 36 | import type { SourceConfiguration, SourceName } from "@/types/incidentManagement/sources.d" |
| 37 | import { NButton, NSpin, useMessage } from "naive-ui" |
| 38 | import { onBeforeMount, ref } from "vue" |
| 39 | import Api from "@/api" |
| 40 | import Icon from "@/components/common/Icon.vue" |
| 41 | import SourceConfigurationForm from "./SourceConfigurationForm.vue" |
| 42 | import SourceConfigurationViewer from "./SourceConfigurationViewer.vue" |
| 43 | |
| 44 | const { source } = defineProps<{ source: SourceName }>() |
| 45 | |
| 46 | const message = useMessage() |
| 47 | const loading = ref(false) |
| 48 | const submitting = ref(false) |
| 49 | const ArrowIcon = "carbon:arrow-left" |
| 50 | const EditIcon = "uil:edit-alt" |
| 51 | const sourceConfiguration = ref<SourceConfiguration | null>(null) |
| 52 | const editing = ref(false) |
| 53 | const formCTX = ref<{ reset: () => void; toggleSubmittingFlag: () => boolean } | null>(null) |
| 54 | |
| 55 | function getSourceConfiguration() { |
| 56 | loading.value = true |
| 57 | |
| 58 | Api.incidentManagement.sources |
| 59 | .getSourceConfiguration(source) |
| 60 | .then(res => { |
| 61 | if (res.data.success) { |
| 62 | sourceConfiguration.value = { |
| 63 | field_names: res.data.field_names || [], |
| 64 | ioc_field_names: res.data.ioc_field_names || [], |
| 65 | asset_name: res.data.asset_name || "", |
| 66 | timefield_name: res.data.timefield_name || "", |
| 67 | alert_title_name: res.data.alert_title_name || "", |
| 68 | source: res.data.source || source |
| 69 | } |
| 70 | } else { |
| 71 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 72 | } |
| 73 | }) |
| 74 | .catch(err => { |
| 75 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 76 | }) |
| 77 | .finally(() => { |
| 78 | loading.value = false |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | function setEditMode() { |
| 83 | formCTX.value?.reset() |
| 84 | editing.value = true |
| 85 | } |
| 86 | function setViewMode() { |
| 87 | editing.value = false |
| 88 | } |
| 89 | |
| 90 | function updateSourceConfiguration(payload: SourceConfiguration) { |
| 91 | submitting.value = formCTX.value?.toggleSubmittingFlag() || true |
| 92 | |
| 93 | Api.incidentManagement.sources |
| 94 | .updateSourceConfiguration(payload) |
| 95 | .then(res => { |
| 96 | if (res.data.success) { |
| 97 | message.success(res.data?.message || `Source Configuration sent successfully`) |
| 98 | getSourceConfiguration() |
| 99 | setViewMode() |
| 100 | } else { |
| 101 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 102 | } |
| 103 | }) |
| 104 | .catch((err: ApiError) => { |
| 105 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 106 | }) |
| 107 | .finally(() => { |
| 108 | submitting.value = formCTX.value?.toggleSubmittingFlag() || false |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | onBeforeMount(() => { |
| 113 | getSourceConfiguration() |
| 114 | }) |
| 115 | </script> |