antares/src/renderer/components/ModalNewConnection.vue

278 lines
9.6 KiB
Vue
Raw Normal View History

2020-05-12 18:27:31 +02:00
<template>
<div class="modal active">
<a class="modal-overlay c-hand" @click="closeModal" />
<div class="modal-container">
2020-08-12 18:11:48 +02:00
<div class="modal-header pl-2">
2020-05-12 18:27:31 +02:00
<div class="modal-title h6">
2020-08-12 18:11:48 +02:00
<div class="d-flex">
<i class="mdi mdi-24px mdi-server-plus mr-1" /> {{ $t('message.createNewConnection') }}
</div>
2020-05-12 18:27:31 +02:00
</div>
<a class="btn btn-clear c-hand" @click="closeModal" />
</div>
<div class="modal-body pb-0">
2020-05-12 18:27:31 +02:00
<div class="content">
<form class="form-horizontal">
<fieldset class="m-0" :disabled="isTesting">
<div class="form-group">
2020-05-31 17:56:33 +02:00
<div class="col-4 col-sm-12">
<label class="form-label">{{ $t('word.connectionName') }}:</label>
</div>
<div class="col-8 col-sm-12">
<input
ref="firstInput"
2020-05-31 17:56:33 +02:00
v-model="connection.name"
class="form-input"
type="text"
>
</div>
</div>
<div class="form-group">
<div class="col-4 col-sm-12">
2020-05-25 15:37:59 +02:00
<label class="form-label">{{ $t('word.client') }}:</label>
2020-05-12 18:27:31 +02:00
</div>
2020-05-31 17:56:33 +02:00
<div class="col-8 col-sm-12">
2020-05-15 17:52:59 +02:00
<select
v-model="connection.client"
class="form-select"
@change="setDefaults"
>
2020-05-12 18:27:31 +02:00
<option value="mysql">
2020-06-10 19:29:10 +02:00
MySQL
</option>
<option value="maria">
MariaDB
2020-05-12 18:27:31 +02:00
</option>
2020-07-31 15:45:32 +02:00
<!-- <option value="mssql">
2020-05-12 18:27:31 +02:00
Microsoft SQL
</option>
<option value="pg">
PostgreSQL
</option>
<option value="oracledb">
Oracle DB
2020-07-31 15:45:32 +02:00
</option> -->
2020-05-12 18:27:31 +02:00
</select>
</div>
</div>
<div class="form-group">
2020-05-31 17:56:33 +02:00
<div class="col-4 col-sm-12">
2020-05-25 15:37:59 +02:00
<label class="form-label">{{ $t('word.hostName') }}/IP:</label>
2020-05-12 18:27:31 +02:00
</div>
2020-05-31 17:56:33 +02:00
<div class="col-8 col-sm-12">
2020-05-12 18:27:31 +02:00
<input
v-model="connection.host"
class="form-input"
type="text"
>
</div>
</div>
<div class="form-group">
2020-05-31 17:56:33 +02:00
<div class="col-4 col-sm-12">
2020-05-25 15:37:59 +02:00
<label class="form-label">{{ $t('word.port') }}:</label>
2020-05-12 18:27:31 +02:00
</div>
2020-05-31 17:56:33 +02:00
<div class="col-8 col-sm-12">
2020-05-12 18:27:31 +02:00
<input
v-model="connection.port"
class="form-input"
type="number"
min="1"
max="65535"
>
</div>
</div>
<div class="form-group">
2020-05-31 17:56:33 +02:00
<div class="col-4 col-sm-12">
2020-05-25 15:37:59 +02:00
<label class="form-label">{{ $t('word.user') }}:</label>
2020-05-12 18:27:31 +02:00
</div>
2020-05-31 17:56:33 +02:00
<div class="col-8 col-sm-12">
2020-05-12 18:27:31 +02:00
<input
v-model="connection.user"
class="form-input"
type="text"
:disabled="connection.ask"
>
</div>
</div>
<div class="form-group">
2020-05-31 17:56:33 +02:00
<div class="col-4 col-sm-12">
2020-05-25 15:37:59 +02:00
<label class="form-label">{{ $t('word.password') }}:</label>
2020-05-12 18:27:31 +02:00
</div>
2020-05-31 17:56:33 +02:00
<div class="col-8 col-sm-12">
2020-05-12 18:27:31 +02:00
<input
v-model="connection.password"
class="form-input"
type="password"
:disabled="connection.ask"
>
</div>
</div>
<div class="form-group">
2020-05-31 17:56:33 +02:00
<div class="col-4 col-sm-12" />
<div class="col-8 col-sm-12">
2020-05-12 18:27:31 +02:00
<label class="form-checkbox form-inline">
2020-05-25 15:37:59 +02:00
<input v-model="connection.ask" type="checkbox"><i class="form-icon" /> {{ $t('message.askCredentials') }}
2020-05-12 18:27:31 +02:00
</label>
</div>
</div>
</fieldset>
</form>
</div>
<BaseToast
class="mb-2"
:message="toast.message"
:status="toast.status"
/>
</div>
<div class="modal-footer text-light">
2020-05-12 18:27:31 +02:00
<button
class="btn btn-gray mr-2"
:class="{'loading': isTesting}"
@click="startTest"
>
2020-05-25 15:37:59 +02:00
{{ $t('message.testConnection') }}
2020-05-12 18:27:31 +02:00
</button>
<button class="btn btn-primary mr-2" @click="saveNewConnection">
2020-05-25 15:37:59 +02:00
{{ $t('word.save') }}
2020-05-12 18:27:31 +02:00
</button>
<button class="btn btn-link" @click="closeModal">
2020-05-25 15:37:59 +02:00
{{ $t('word.close') }}
2020-05-12 18:27:31 +02:00
</button>
</div>
</div>
<ModalAskCredentials
v-if="isAsking"
@close-asking="closeAsking"
2020-05-12 18:27:31 +02:00
@credentials="continueTest"
/>
</div>
</template>
<script>
import { mapActions } from 'vuex';
2020-05-14 15:21:57 +02:00
import Connection from '@/ipc-api/Connection';
import { uidGen } from 'common/libs/uidGen';
2020-05-12 18:27:31 +02:00
import ModalAskCredentials from '@/components/ModalAskCredentials';
import BaseToast from '@/components/BaseToast';
export default {
name: 'ModalNewConnection',
components: {
ModalAskCredentials,
BaseToast
},
data () {
return {
connection: {
2020-05-31 17:56:33 +02:00
name: '',
2020-05-12 18:27:31 +02:00
client: 'mysql',
host: '127.0.0.1',
port: '3306',
user: 'root',
password: '',
2020-05-13 18:40:49 +02:00
ask: false,
uid: uidGen('C')
2020-05-12 18:27:31 +02:00
},
toast: {
status: '',
message: ''
},
isTesting: false,
isAsking: false
};
},
2020-10-04 17:21:21 +02:00
created () {
window.addEventListener('keydown', this.onKey);
setTimeout(() => {
this.$refs.firstInput.focus();
}, 20);
2020-10-04 17:21:21 +02:00
},
beforeDestroy () {
window.removeEventListener('keydown', this.onKey);
},
2020-05-12 18:27:31 +02:00
methods: {
...mapActions({
2020-05-30 12:54:05 +02:00
closeModal: 'application/hideNewConnModal',
2020-05-12 18:27:31 +02:00
addConnection: 'connections/addConnection'
}),
2020-05-15 17:52:59 +02:00
setDefaults () {
switch (this.connection.client) {
case 'mysql':
this.connection.port = '3306';
break;
case 'mssql':
this.connection.port = '1433';
break;
case 'pg':
this.connection.port = '5432';
break;
case 'oracledb':
this.connection.port = '1521';
break;
}
},
2020-05-12 18:27:31 +02:00
async startTest () {
this.isTesting = true;
this.toast = {
status: '',
message: ''
};
if (this.connection.ask)
this.isAsking = true;
2020-05-14 15:21:57 +02:00
else {
try {
const res = await Connection.makeTest(this.connection);
if (res.status === 'error')
this.toast = { status: 'error', message: res.response.message };
else
2020-05-26 18:10:11 +02:00
this.toast = { status: 'success', message: this.$t('message.connectionSuccessfullyMade') };
2020-05-14 15:21:57 +02:00
}
catch (err) {
this.toast = { status: 'error', message: err.stack };
}
this.isTesting = false;
}
2020-05-12 18:27:31 +02:00
},
async continueTest (credentials) { // if "Ask for credentials" is true
this.isAsking = false;
const params = Object.assign({}, this.connection, credentials);
2020-05-24 11:03:20 +02:00
2020-05-23 13:32:14 +02:00
try {
const res = await Connection.makeTest(params);
if (res.status === 'error')
this.toast = { status: 'error', message: res.response.message };
else
2020-05-26 18:10:11 +02:00
this.toast = { status: 'success', message: this.$t('message.connectionSuccessfullyMade') };
2020-05-23 13:32:14 +02:00
}
catch (err) {
this.toast = { status: 'error', message: err.stack };
}
this.isTesting = false;
2020-05-12 18:27:31 +02:00
},
saveNewConnection () {
this.addConnection(this.connection);
this.closeModal();
},
closeAsking () {
this.isAsking = false;
this.isTesting = false;
2020-10-04 17:21:21 +02:00
},
onKey (e) {
e.stopPropagation();
if (e.key === 'Escape')
this.closeModal();
2020-05-12 18:27:31 +02:00
}
}
};
</script>
<style scoped>
2020-07-31 18:16:28 +02:00
.modal-container {
max-width: 450px;
}
2020-05-12 18:27:31 +02:00
</style>