main
vue 312 lines 8.6 KB
Raw
1 <template>
2 <n-spin :show="loading" class="customer-meta-form">
3 <n-form ref="formRef" :label-width="80" :model="form" :rules>
4 <div class="flex flex-col gap-4">
5 <div class="flex flex-wrap gap-4">
6 <div v-for="(_, key) of form" :key class="grow">
7 <n-form-item :label="fieldsMeta[key]?.label ?? ''" :path="key" class="grow">
8 <n-input
9 v-model:value.trim="form[key]"
10 :placeholder="fieldsMeta[key]?.placeholder ?? ''"
11 :readonly="fieldsMeta[key]?.readonly ?? false"
12 clearable
13 />
14 </n-form-item>
15 </div>
16 </div>
17 <div class="flex justify-between gap-4">
18 <div class="flex gap-4">
19 <slot name="additionalActions"></slot>
20 </div>
21 <div class="flex gap-4">
22 <n-button :disabled="loading" @click="reset()">Reset</n-button>
23 <n-button type="primary" :disabled="!isValid" :loading @click="validate()">Submit</n-button>
24 </div>
25 </div>
26 </div>
27 </n-form>
28 </n-spin>
29 </template>
30
31 <script setup lang="ts">
32 // TODO-FE: refactor
33 import type { FormInst, FormRules, FormValidationError } from "naive-ui"
34 import type { CustomerMeta } from "@/types/customers.d"
35 import _get from "lodash/get"
36 import _toSafeInteger from "lodash/toSafeInteger"
37 import _trim from "lodash/trim"
38 import { NButton, NForm, NFormItem, NInput, NSpin, useMessage } from "naive-ui"
39 import { computed, onBeforeMount, onMounted, ref, toRefs, watch } from "vue"
40 import Api from "@/api"
41
42 type CustomerMetaModel = { [K in keyof CustomerMeta]: string }
43
44 const props = defineProps<{
45 customerMeta?: CustomerMeta
46 customerCode: string
47 customerName: string
48 metaId: number
49 resetOnSubmit?: boolean
50 }>()
51
52 const emit = defineEmits<{
53 (e: "update:loading", value: boolean): void
54 (e: "submitted", value: CustomerMeta): void
55 (
56 e: "mounted",
57 value: {
58 reset: () => void
59 }
60 ): void
61 }>()
62
63 const { customerMeta, customerCode, customerName, metaId, resetOnSubmit } = toRefs(props)
64
65 const loading = ref(false)
66 const message = useMessage()
67 const form = ref<CustomerMetaModel>(getClearForm())
68 const formRef = ref<FormInst | null>(null)
69
70 const rules: FormRules = {
71 customer_meta_graylog_index: {
72 required: true,
73 message: "Please input Graylog Index",
74 trigger: ["input", "blur"]
75 },
76 customer_meta_graylog_stream: {
77 required: true,
78 message: "Please input Graylog Stream",
79 trigger: ["input", "blur"]
80 },
81 customer_meta_grafana_org_id: {
82 required: true,
83 message: "Please input Grafana Org Id",
84 trigger: ["input", "blur"]
85 },
86 customer_meta_index_retention: {
87 required: true,
88 message: "Please input Index retention",
89 trigger: ["input", "blur"]
90 },
91 customer_meta_wazuh_group: {
92 required: true,
93 message: "Please input Wazuh Group",
94 trigger: ["input", "blur"]
95 },
96 customer_meta_wazuh_registration_port: {
97 required: true,
98 message: "Please input Wazuh registration port",
99 trigger: ["input", "blur"]
100 },
101 customer_meta_wazuh_log_ingestion_port: {
102 required: true,
103 message: "Please input Wazuh log ingestion port",
104 trigger: ["input", "blur"]
105 },
106 customer_meta_wazuh_auth_password: {
107 required: true,
108 message: "Please input Wazuh auth password",
109 trigger: ["input", "blur"]
110 },
111 customer_meta_iris_customer_id: {
112 required: true,
113 message: "Please input Wazuh auth password",
114 trigger: ["input", "blur"]
115 },
116 customer_meta_office365_organization_id: {
117 required: true,
118 message: "Please input Wazuh auth password",
119 trigger: ["input", "blur"]
120 }
121 }
122
123 const fieldsMeta: { [key: string]: { label: string; placeholder: string; readonly?: boolean } } = {
124 id: {
125 label: "Meta ID",
126 placeholder: "Meta ID...",
127 readonly: true
128 },
129 customer_code: {
130 label: "Customer Code",
131 placeholder: "Customer Code...",
132 readonly: true
133 },
134 customer_name: {
135 label: "Customer Name",
136 placeholder: "Customer Name...",
137 readonly: true
138 },
139 customer_meta_graylog_index: {
140 label: "Graylog Index",
141 placeholder: "Graylog Index..."
142 },
143 customer_meta_graylog_stream: {
144 label: "Graylog Stream",
145 placeholder: "Graylog Stream..."
146 },
147 customer_meta_grafana_org_id: {
148 label: "Grafana Org Id",
149 placeholder: "Grafana Org Id..."
150 },
151 customer_meta_index_retention: {
152 label: "Index retention",
153 placeholder: "Index retention..."
154 },
155 customer_meta_wazuh_group: {
156 label: "Wazuh Group",
157 placeholder: "Wazuh Group..."
158 },
159 customer_meta_wazuh_registration_port: {
160 label: "Wazuh registration port",
161 placeholder: "Wazuh registration port..."
162 },
163 customer_meta_wazuh_log_ingestion_port: {
164 label: "Wazuh log ingestion port",
165 placeholder: "Wazuh log ingestion port..."
166 },
167 customer_meta_wazuh_auth_password: {
168 label: "Wazuh auth password",
169 placeholder: "Wazuh auth password..."
170 },
171 customer_meta_iris_customer_id: {
172 label: "Iris Customer ID",
173 placeholder: "Iris Customer ID..."
174 },
175 customer_meta_office365_organization_id: {
176 label: "Office365 Organization ID",
177 placeholder: "Office365 Organization ID..."
178 },
179 customer_meta_wazuh_api_port: {
180 label: "Wazuh API port",
181 placeholder: "Wazuh API port..."
182 }
183 }
184
185 const isValid = computed(() => {
186 let valid = true
187
188 for (const key in rules) {
189 const rule = rules[key] as FormRules
190
191 if (rule.required && !_trim(_get(form.value, key))) {
192 valid = false
193 }
194 }
195
196 return valid
197 })
198
199 function validate() {
200 if (!formRef.value) return
201
202 formRef.value.validate((errors?: Array<FormValidationError>) => {
203 if (!errors) {
204 submit()
205 } else {
206 message.warning("You must fill in the required fields correctly.")
207 return false
208 }
209 })
210 }
211
212 function getClearForm(customerMeta?: Partial<CustomerMeta>): CustomerMetaModel {
213 return {
214 id: `${metaId.value}` || "",
215 customer_code: customerCode.value || "",
216 customer_name: customerName.value || "",
217 customer_meta_graylog_index: customerMeta?.customer_meta_graylog_index || "",
218 customer_meta_graylog_stream: customerMeta?.customer_meta_graylog_stream || "",
219 customer_meta_grafana_org_id: customerMeta?.customer_meta_grafana_org_id || "",
220 customer_meta_index_retention: customerMeta?.customer_meta_index_retention || "",
221 customer_meta_wazuh_group: customerMeta?.customer_meta_wazuh_group || "",
222 customer_meta_wazuh_registration_port: customerMeta?.customer_meta_wazuh_registration_port || "",
223 customer_meta_wazuh_log_ingestion_port: customerMeta?.customer_meta_wazuh_log_ingestion_port || "",
224 customer_meta_wazuh_auth_password: customerMeta?.customer_meta_wazuh_auth_password || "",
225 customer_meta_iris_customer_id: `${customerMeta?.customer_meta_iris_customer_id}` || "",
226 customer_meta_office365_organization_id: customerMeta?.customer_meta_office365_organization_id || "",
227 customer_meta_wazuh_api_port: customerMeta?.customer_meta_wazuh_api_port || ""
228 }
229 }
230
231 function getPayload(meta: CustomerMetaModel): CustomerMeta {
232 return {
233 id: _toSafeInteger(meta.id),
234 customer_code: meta.customer_code,
235 customer_name: meta.customer_name,
236 customer_meta_graylog_index: meta.customer_meta_graylog_index,
237 customer_meta_graylog_stream: meta.customer_meta_graylog_stream,
238 customer_meta_grafana_org_id: meta.customer_meta_grafana_org_id,
239 customer_meta_wazuh_group: meta.customer_meta_wazuh_group,
240 customer_meta_index_retention: meta.customer_meta_index_retention,
241 customer_meta_wazuh_registration_port: meta.customer_meta_wazuh_registration_port,
242 customer_meta_wazuh_log_ingestion_port: meta.customer_meta_wazuh_log_ingestion_port,
243 customer_meta_wazuh_auth_password: meta.customer_meta_wazuh_auth_password,
244 customer_meta_iris_customer_id: _toSafeInteger(meta.customer_meta_iris_customer_id),
245 customer_meta_office365_organization_id: meta.customer_meta_office365_organization_id,
246 customer_meta_wazuh_api_port: meta.customer_meta_wazuh_api_port
247 }
248 }
249
250 function reset() {
251 if (!loading.value) {
252 resetForm()
253 formRef.value?.restoreValidation()
254 }
255 }
256
257 function resetForm() {
258 form.value = getClearForm()
259 }
260
261 function submit() {
262 loading.value = true
263
264 const method = customerMeta.value?.customer_meta_graylog_index
265 ? Api.customers.updateCustomerMeta
266 : Api.customers.createCustomerMeta
267
268 method(getPayload(form.value), customerCode.value)
269 .then(res => {
270 if (res.data.success) {
271 emit("submitted", res.data.customer_meta)
272 if (resetOnSubmit.value) {
273 resetForm()
274 }
275 } else {
276 message.warning(res.data?.message || "An error occurred. Please try again later.")
277 }
278 })
279 .catch(err => {
280 message.error(err.response?.data?.message || "An error occurred. Please try again later.")
281 })
282 .finally(() => {
283 loading.value = false
284 })
285 }
286
287 function setForm() {
288 form.value = getClearForm(customerMeta.value)
289 }
290
291 watch(loading, val => {
292 emit("update:loading", val)
293 })
294
295 watch(customerMeta, val => {
296 if (val) {
297 setForm()
298 }
299 })
300
301 onBeforeMount(() => {
302 if (customerMeta.value) {
303 setForm()
304 }
305 })
306
307 onMounted(() => {
308 emit("mounted", {
309 reset
310 })
311 })
312 </script>