| 1 | <template> |
| 2 | <div class="customer-event-sources"> |
| 3 | <transition name="form-fade" mode="out-in"> |
| 4 | <div v-if="showForm" class="flex flex-col gap-4"> |
| 5 | <h4>{{ !!editingSource ? "Edit Event Source" : "Add Event Source" }}</h4> |
| 6 | <CustomerEventSourceForm |
| 7 | :customer-code |
| 8 | :editing-source |
| 9 | @submitted="refreshList()" |
| 10 | @close="closeForm()" |
| 11 | /> |
| 12 | </div> |
| 13 | <div v-else class="flex flex-col gap-4"> |
| 14 | <div class="flex items-center justify-between gap-4"> |
| 15 | <n-button size="small" type="primary" @click="openForm()"> |
| 16 | <template #icon> |
| 17 | <Icon :name="AddIcon" :size="14" /> |
| 18 | </template> |
| 19 | Add Event Source |
| 20 | </n-button> |
| 21 | </div> |
| 22 | |
| 23 | <n-spin :show="loading"> |
| 24 | <div class="min-h-52"> |
| 25 | <template v-if="list.length"> |
| 26 | <CustomerEventSourceItem |
| 27 | v-for="source of list" |
| 28 | :key="source.id" |
| 29 | :source |
| 30 | embedded |
| 31 | class="item-appear item-appear-bottom item-appear-005 mb-2" |
| 32 | @edit="openEdit(source)" |
| 33 | @deleted="refreshList()" |
| 34 | /> |
| 35 | </template> |
| 36 | <template v-else> |
| 37 | <n-empty v-if="!loading" description="No event sources found" class="h-48 justify-center" /> |
| 38 | </template> |
| 39 | </div> |
| 40 | </n-spin> |
| 41 | </div> |
| 42 | </transition> |
| 43 | </div> |
| 44 | </template> |
| 45 | |
| 46 | <script setup lang="ts"> |
| 47 | import type { EventSource } from "@/types/eventSources.d" |
| 48 | import { NButton, NEmpty, NSpin, useMessage } from "naive-ui" |
| 49 | import { onBeforeMount, ref } from "vue" |
| 50 | import Api from "@/api" |
| 51 | import Icon from "@/components/common/Icon.vue" |
| 52 | import CustomerEventSourceForm from "./CustomerEventSourceForm.vue" |
| 53 | import CustomerEventSourceItem from "./CustomerEventSourceItem.vue" |
| 54 | |
| 55 | const { customerCode } = defineProps<{ |
| 56 | customerCode: string |
| 57 | }>() |
| 58 | |
| 59 | const AddIcon = "carbon:add-alt" |
| 60 | |
| 61 | const message = useMessage() |
| 62 | const showForm = ref(false) |
| 63 | const loading = ref(false) |
| 64 | const list = ref<EventSource[]>([]) |
| 65 | const editingSource = ref<EventSource | null>(null) |
| 66 | |
| 67 | function getEventSources() { |
| 68 | loading.value = true |
| 69 | |
| 70 | Api.siem |
| 71 | .getEventSources(customerCode) |
| 72 | .then(res => { |
| 73 | if (res.data.success) { |
| 74 | list.value = res.data?.event_sources || [] |
| 75 | } else { |
| 76 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 77 | } |
| 78 | }) |
| 79 | .catch(err => { |
| 80 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 81 | }) |
| 82 | .finally(() => { |
| 83 | loading.value = false |
| 84 | }) |
| 85 | } |
| 86 | |
| 87 | function openForm() { |
| 88 | editingSource.value = null |
| 89 | showForm.value = true |
| 90 | } |
| 91 | |
| 92 | function openEdit(source: EventSource) { |
| 93 | editingSource.value = source |
| 94 | showForm.value = true |
| 95 | } |
| 96 | |
| 97 | function closeForm() { |
| 98 | editingSource.value = null |
| 99 | showForm.value = false |
| 100 | } |
| 101 | |
| 102 | function refreshList() { |
| 103 | closeForm() |
| 104 | getEventSources() |
| 105 | } |
| 106 | |
| 107 | onBeforeMount(() => { |
| 108 | getEventSources() |
| 109 | }) |
| 110 | </script> |
| 111 | |
| 112 | <style lang="scss" scoped> |
| 113 | .customer-event-sources { |
| 114 | .form-fade-enter-active, |
| 115 | .form-fade-leave-active { |
| 116 | transition: |
| 117 | opacity 0.2s ease-in-out, |
| 118 | transform 0.3s ease-in-out; |
| 119 | } |
| 120 | .form-fade-enter-from { |
| 121 | opacity: 0; |
| 122 | transform: translateY(10px); |
| 123 | } |
| 124 | .form-fade-leave-to { |
| 125 | opacity: 0; |
| 126 | transform: translateY(-10px); |
| 127 | } |
| 128 | } |
| 129 | </style> |