Files
mayfly-go/frontend/src/views/ops/mongo/MongoEdit.vue

170 lines
5.5 KiB
Vue
Raw Normal View History

2022-05-17 20:23:08 +08:00
<template>
<div>
<el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" width="38%" :destroy-on-close="true">
2024-11-20 22:43:53 +08:00
<el-form :model="form" ref="mongoForm" :rules="rules" label-width="auto">
<el-tabs v-model="tabActiveName">
2024-11-20 22:43:53 +08:00
<el-tab-pane :label="$t('common.basic')" name="basic">
<el-form-item ref="tagSelectRef" prop="tagCodePaths" :label="$t('tag.relateTag')" required>
<tag-tree-select
@change-tag="
(tagCodePaths) => {
form.tagCodePaths = tagCodePaths;
tagSelectRef.validate();
}
"
multiple
:select-tags="form.tagCodePaths"
/>
</el-form-item>
2024-11-20 22:43:53 +08:00
<el-form-item prop="name" :label="$t('common.name')" required>
<el-input v-model.trim="form.name" auto-complete="off"></el-input>
</el-form-item>
<el-form-item prop="uri" label="uri" required>
<el-input
type="textarea"
:rows="2"
v-model.trim="form.uri"
2024-11-20 22:43:53 +08:00
placeholder="mongodb://username:password@host1:port1"
auto-complete="off"
></el-input>
</el-form-item>
</el-tab-pane>
2024-11-20 22:43:53 +08:00
<el-tab-pane :label="$t('common.other')" name="other">
<el-form-item prop="sshTunnelMachineId" :label="$t('machine.sshTunnel')">
<ssh-tunnel-select v-model="form.sshTunnelMachineId" />
</el-form-item>
</el-tab-pane>
</el-tabs>
2022-05-17 20:23:08 +08:00
</el-form>
<template #footer>
<div class="dialog-footer">
2024-11-20 22:43:53 +08:00
<el-button @click="testConn" :loading="testConnBtnLoading" type="success">{{ $t('ac.testConn') }}</el-button>
<el-button @click="cancel()">{{ $t('common.cancel') }}</el-button>
<el-button type="primary" :loading="saveBtnLoading" @click="btnOk">{{ $t('common.confirm') }}</el-button>
2022-05-17 20:23:08 +08:00
</div>
</template>
</el-dialog>
</div>
</template>
<script lang="ts" setup>
import { toRefs, reactive, ref, watchEffect } from 'vue';
2022-05-17 20:23:08 +08:00
import { mongoApi } from './api';
import { ElMessage } from 'element-plus';
import TagTreeSelect from '../component/TagTreeSelect.vue';
import SshTunnelSelect from '../component/SshTunnelSelect.vue';
2024-11-20 22:43:53 +08:00
import { useI18nFormValidate, useI18nPleaseInput, useI18nPleaseSelect, useI18nSaveSuccessMsg } from '@/hooks/useI18n';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
2022-05-17 20:23:08 +08:00
const props = defineProps({
visible: {
type: Boolean,
2022-10-26 20:49:29 +08:00
},
mongo: {
type: [Boolean, Object],
},
title: {
type: String,
},
});
//定义事件
const emit = defineEmits(['update:visible', 'cancel', 'val-change']);
const rules = {
tagCodePaths: [
{
required: true,
2024-11-20 22:43:53 +08:00
message: useI18nPleaseSelect('tag.relateTag'),
trigger: ['change', 'blur'],
2022-05-17 20:23:08 +08:00
},
],
name: [
{
required: true,
2024-11-20 22:43:53 +08:00
message: useI18nPleaseInput('common.name'),
trigger: ['change', 'blur'],
2022-05-17 20:23:08 +08:00
},
],
uri: [
{
required: true,
2024-11-20 22:43:53 +08:00
message: useI18nPleaseInput('mongo.connUrl'),
trigger: ['change', 'blur'],
2022-05-17 20:23:08 +08:00
},
],
};
const mongoForm: any = ref(null);
const tagSelectRef: any = ref(null);
const state = reactive({
dialogVisible: false,
tabActiveName: 'basic',
form: {
id: null,
code: '',
name: null,
uri: null,
sshTunnelMachineId: null as any,
tagCodePaths: [],
2022-05-17 20:23:08 +08:00
},
submitForm: {},
});
2022-05-17 20:23:08 +08:00
const { dialogVisible, tabActiveName, form, submitForm } = toRefs(state);
const { isFetching: testConnBtnLoading, execute: testConnExec } = mongoApi.testConn.useApi(submitForm);
const { isFetching: saveBtnLoading, execute: saveMongoExec } = mongoApi.saveMongo.useApi(submitForm);
2022-05-17 20:23:08 +08:00
watchEffect(() => {
state.dialogVisible = props.visible;
if (!state.dialogVisible) {
return;
}
state.tabActiveName = 'basic';
const mongo: any = props.mongo;
if (mongo) {
state.form = { ...mongo };
state.form.tagCodePaths = mongo.tags.map((t: any) => t.codePath);
} else {
state.form = { db: 0 } as any;
}
});
const getReqForm = () => {
const reqForm = { ...state.form };
if (!state.form.sshTunnelMachineId || state.form.sshTunnelMachineId <= 0) {
reqForm.sshTunnelMachineId = -1;
}
return reqForm;
};
const testConn = async () => {
2024-11-20 22:43:53 +08:00
await useI18nFormValidate(mongoForm);
state.submitForm = getReqForm();
await testConnExec();
2024-11-20 22:43:53 +08:00
ElMessage.success(t('ac.connSuccess'));
};
const btnOk = async () => {
2024-11-20 22:43:53 +08:00
await useI18nFormValidate(mongoForm);
state.submitForm = getReqForm();
await saveMongoExec();
2024-11-20 22:43:53 +08:00
useI18nSaveSuccessMsg();
emit('val-change', state.form);
cancel();
};
2022-05-17 20:23:08 +08:00
const cancel = () => {
emit('update:visible', false);
emit('cancel');
};
2022-05-17 20:23:08 +08:00
</script>
<style lang="scss"></style>