main
vue 162 lines 4.88 KB
Raw
1 <template>
2 <div class="flex flex-col gap-4">
3 <!-- Filters Bar -->
4 <div class="flex flex-wrap items-end gap-3">
5 <n-form-item label="Customer" :show-feedback="false" class="mb-0! min-w-60 grow">
6 <n-select
7 v-model:value="selectedCustomerCode"
8 :options="customersOptions"
9 placeholder="Select Customer"
10 filterable
11 :loading="loadingCustomers"
12 :consistent-menu-width="false"
13 clearable
14 />
15 </n-form-item>
16
17 <n-button type="primary" :disabled="!selectedCustomerCode" @click="showAddTemplateDrawer = true">
18 <template #icon>
19 <Icon name="carbon:add" />
20 </template>
21 Add template
22 </n-button>
23 </div>
24
25 <n-empty
26 v-if="!selectedCustomerCode"
27 description="Select a customer to browse dashboard templates and enable dashboards"
28 class="h-48 justify-center"
29 />
30
31 <template v-else>
32 <!-- No Event Sources Warning -->
33 <n-alert v-if="showNoSourcesWarning" title="No Event Sources Configured" type="warning">
34 An Event Source needs to be defined for this customer before dashboards can be enabled. Go to the
35 customer's
36 <strong>Event Sources</strong>
37 tab to configure one.
38 </n-alert>
39
40 <EnabledDashboardsSection
41 ref="enabledDashboardsSectionRef"
42 v-model:enabled-dashboards="enabledDashboards"
43 :customer-code="selectedCustomerCode"
44 :visible="!showNoSourcesWarning"
45 :event-sources-list
46 />
47 </template>
48
49 <n-drawer
50 v-model:show="showAddTemplateDrawer"
51 display-directive="show"
52 :width="960"
53 class="max-w-[92vw]"
54 :trap-focus="false"
55 >
56 <n-drawer-content title="Add template" closable :native-scrollbar="false">
57 <DashboardCategoriesSection
58 :selected-customer-code
59 :event-sources-list
60 :loading-event-sources
61 :enabled-dashboards
62 @refresh-enabled-dashboards="refreshEnabledDashboards"
63 />
64 </n-drawer-content>
65 </n-drawer>
66 </div>
67 </template>
68
69 <script setup lang="ts">
70 import type { Customer } from "@/types/customers.d"
71 import type { EnabledDashboard } from "@/types/dashboards.d"
72 import type { EventSource } from "@/types/eventSources.d"
73 import { NAlert, NButton, NDrawer, NDrawerContent, NEmpty, NFormItem, NSelect, useMessage } from "naive-ui"
74 import { computed, onBeforeMount, ref, useTemplateRef, watch } from "vue"
75 import Api from "@/api"
76 import Icon from "@/components/common/Icon.vue"
77 import DashboardCategoriesSection from "./DashboardCategoriesSection.vue"
78 import EnabledDashboardsSection from "./EnabledDashboardsSection.vue"
79
80 const message = useMessage()
81
82 // ── Customer selection ──────────────────────────────────────────
83 const loadingCustomers = ref(false)
84 const customersList = ref<Customer[]>([])
85 const selectedCustomerCode = ref<string | null>(null)
86 const showAddTemplateDrawer = ref(false)
87
88 // ── Enabled dashboards (lista aggiornata da EnabledDashboardsSection via v-model) ──
89 const enabledDashboards = ref<EnabledDashboard[]>([])
90 const enabledDashboardsSectionRef =
91 useTemplateRef<InstanceType<typeof EnabledDashboardsSection>>("enabledDashboardsSectionRef")
92
93 const customersOptions = computed(() =>
94 customersList.value.map(c => ({ label: `#${c.customer_code} - ${c.customer_name}`, value: c.customer_code }))
95 )
96
97 function getCustomers() {
98 loadingCustomers.value = true
99
100 Api.customers
101 .getCustomers()
102 .then(res => {
103 if (res.data.success) {
104 customersList.value = res.data?.customers || []
105 } else {
106 message.warning(res.data?.message || "An error occurred. Please try again later.")
107 }
108 })
109 .catch(err => {
110 message.error(err.response?.data?.message || "An error occurred. Please try again later.")
111 })
112 .finally(() => {
113 loadingCustomers.value = false
114 })
115 }
116
117 // ── Event Source selection ───────────────────────────────────────
118 const loadingEventSources = ref(false)
119 const eventSourcesList = ref<EventSource[]>([])
120
121 const showNoSourcesWarning = computed(
122 () => selectedCustomerCode.value && !loadingEventSources.value && !eventSourcesList.value.length
123 )
124
125 function getEventSources(customerCode: string) {
126 loadingEventSources.value = true
127 eventSourcesList.value = []
128
129 Api.siem
130 .getEventSources(customerCode)
131 .then(res => {
132 if (res.data.success) {
133 eventSourcesList.value = res.data?.event_sources || []
134 } else {
135 message.warning(res.data?.message || "An error occurred. Please try again later.")
136 }
137 })
138 .catch(err => {
139 message.error(err.response?.data?.message || "An error occurred. Please try again later.")
140 })
141 .finally(() => {
142 loadingEventSources.value = false
143 })
144 }
145
146 function refreshEnabledDashboards() {
147 enabledDashboardsSectionRef.value?.refreshEnabledDashboards()
148 }
149
150 watch(selectedCustomerCode, code => {
151 eventSourcesList.value = []
152 showAddTemplateDrawer.value = false
153
154 if (code) {
155 getEventSources(code)
156 }
157 })
158
159 onBeforeMount(() => {
160 getCustomers()
161 })
162 </script>