| 1 | <template> |
| 2 | <n-spin :show="loading" content-class="flex grow flex-col"> |
| 3 | <div class="flex flex-col gap-4"> |
| 4 | <n-form ref="formRef" :model :rules label-width="auto"> |
| 5 | <div class="grid grid-cols-1 gap-x-6 gap-y-2 md:grid-cols-2"> |
| 6 | <!-- Customer Code (readonly) --> |
| 7 | <n-form-item :label="getMetaFieldLabel('customer_code')" path="customer_code"> |
| 8 | <n-input :value="model.customer_code" disabled /> |
| 9 | </n-form-item> |
| 10 | |
| 11 | <!-- Integration Name (readonly) --> |
| 12 | <n-form-item :label="getMetaFieldLabel('integration_name')" path="integration_name"> |
| 13 | <n-input :value="model.integration_name" disabled /> |
| 14 | </n-form-item> |
| 15 | |
| 16 | <template v-for="(_, key) of model" :key> |
| 17 | <n-form-item |
| 18 | v-if="!['customer_code', 'integration_name'].includes(key)" |
| 19 | :label="getMetaFieldLabel(key)" |
| 20 | :path="key" |
| 21 | > |
| 22 | <n-input v-model:value.trim="model[key]" placeholder="Optional" clearable /> |
| 23 | </n-form-item> |
| 24 | </template> |
| 25 | </div> |
| 26 | </n-form> |
| 27 | |
| 28 | <!-- Edit Form Actions --> |
| 29 | <div class="flex items-center justify-between gap-3"> |
| 30 | <div class="flex items-center justify-between gap-3"> |
| 31 | <slot name="extraActions" /> |
| 32 | |
| 33 | <n-button v-if="showResetButton" @click="reset()">Reset</n-button> |
| 34 | </div> |
| 35 | <n-button type="primary" :loading @click="save()"> |
| 36 | <template #icon> |
| 37 | <Icon :name="SaveIcon" /> |
| 38 | </template> |
| 39 | Save Changes |
| 40 | </n-button> |
| 41 | </div> |
| 42 | </div> |
| 43 | </n-spin> |
| 44 | </template> |
| 45 | |
| 46 | <script setup lang="ts"> |
| 47 | import type { FormInst, FormRules } from "naive-ui" |
| 48 | import type { UpdateMetaAutoRequest } from "@/api/endpoints/integrations" |
| 49 | import type { CustomerIntegrationMetaNetwork, CustomerIntegrationMetaThirdParty } from "@/types/integrations.d" |
| 50 | import { NButton, NForm, NFormItem, NInput, NSpin, useMessage } from "naive-ui" |
| 51 | import { ref } from "vue" |
| 52 | import Api from "@/api" |
| 53 | import Icon from "@/components/common/Icon.vue" |
| 54 | import { getMetaFieldLabel } from "./utils" |
| 55 | |
| 56 | const { integrationData, showResetButton } = defineProps<{ |
| 57 | integrationData: CustomerIntegrationMetaThirdParty | CustomerIntegrationMetaNetwork |
| 58 | showResetButton?: boolean |
| 59 | }>() |
| 60 | |
| 61 | const emit = defineEmits<{ |
| 62 | (e: "success"): void |
| 63 | }>() |
| 64 | |
| 65 | const SaveIcon = "carbon:save" |
| 66 | |
| 67 | const message = useMessage() |
| 68 | const loading = ref(false) |
| 69 | const formRef = ref<FormInst | null>(null) |
| 70 | |
| 71 | // Form for editing |
| 72 | const model = ref<UpdateMetaAutoRequest>(getDefaultModel(integrationData)) |
| 73 | |
| 74 | // Form validation rules |
| 75 | const rules: FormRules = { |
| 76 | customer_code: { |
| 77 | required: true |
| 78 | }, |
| 79 | integration_name: { |
| 80 | required: true |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | function getDefaultModel( |
| 85 | entity?: CustomerIntegrationMetaThirdParty | CustomerIntegrationMetaNetwork |
| 86 | ): UpdateMetaAutoRequest { |
| 87 | return { |
| 88 | customer_code: entity?.customer_code || "", |
| 89 | integration_name: |
| 90 | entity && "integration_name" in entity |
| 91 | ? entity.integration_name |
| 92 | : entity && "network_connector_name" in entity |
| 93 | ? entity.network_connector_name |
| 94 | : "", |
| 95 | graylog_input_id: entity?.graylog_input_id || undefined, |
| 96 | graylog_index_id: entity?.graylog_index_id || undefined, |
| 97 | graylog_stream_id: entity?.graylog_stream_id || undefined, |
| 98 | graylog_pipeline_id: entity?.graylog_pipeline_id || undefined, |
| 99 | graylog_content_pack_input_id: entity?.graylog_content_pack_input_id || undefined, |
| 100 | graylog_content_pack_stream_id: entity?.graylog_content_pack_stream_id || undefined, |
| 101 | grafana_org_id: entity?.grafana_org_id || undefined, |
| 102 | grafana_dashboard_folder_id: entity?.grafana_dashboard_folder_id || undefined, |
| 103 | grafana_datasource_uid: entity?.grafana_datasource_uid || undefined |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | function reset() { |
| 108 | if (!loading.value) { |
| 109 | model.value = getDefaultModel(integrationData) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | function save() { |
| 114 | if (!formRef.value) return |
| 115 | |
| 116 | formRef.value.validate(errors => { |
| 117 | if (errors) { |
| 118 | message.error("Please fix the validation errors") |
| 119 | return |
| 120 | } |
| 121 | |
| 122 | loading.value = true |
| 123 | |
| 124 | const payload: UpdateMetaAutoRequest = { ...model.value } |
| 125 | for (const key in payload) { |
| 126 | if (!payload[key as keyof UpdateMetaAutoRequest]) payload[key as keyof UpdateMetaAutoRequest] = "" |
| 127 | } |
| 128 | |
| 129 | Api.integrations |
| 130 | .updateMetaAuto(payload) |
| 131 | .then(res => { |
| 132 | if (res.data.success) { |
| 133 | message.success(res.data?.message || "Metadata updated successfully") |
| 134 | // Reload the data to show updated values |
| 135 | emit("success") |
| 136 | } else { |
| 137 | message.warning(res.data?.message || "Failed to update metadata") |
| 138 | } |
| 139 | }) |
| 140 | .catch(err => { |
| 141 | const errorMsg = err.response?.data?.message || "An error occurred while updating metadata" |
| 142 | message.error(errorMsg) |
| 143 | }) |
| 144 | .finally(() => { |
| 145 | loading.value = false |
| 146 | }) |
| 147 | }) |
| 148 | } |
| 149 | </script> |