1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-04-19 08:51:03 +00:00
rolens/frontend/src/organisms/connection/hostdetail.svelte

88 lines
1.9 KiB
Svelte
Raw Normal View History

2023-01-18 20:59:00 +01:00
<script>
2023-01-20 19:08:56 +01:00
import { input } from '../../actions';
2023-01-18 20:59:00 +01:00
import { createEventDispatcher } from 'svelte';
2023-01-20 19:08:56 +01:00
import { AddHost, UpdateHost } from '../../../wailsjs/go/app/App';
2023-01-18 20:59:00 +01:00
import Modal from '../../components/modal.svelte';
export let show = false;
2023-01-20 19:08:56 +01:00
export let hostKey = '';
2023-01-22 21:12:56 +01:00
export let hosts = {};
2023-01-18 20:59:00 +01:00
2023-01-20 19:08:56 +01:00
const dispatch = createEventDispatcher();
2023-01-22 21:12:56 +01:00
let form = {};
2023-01-18 20:59:00 +01:00
let error = '';
$: valid = validate(form);
2023-01-22 21:12:56 +01:00
$: host = hosts[hostKey];
2023-01-18 20:59:00 +01:00
$: if (show || !show) {
2023-01-20 19:08:56 +01:00
init();
}
function init() {
form = { ...(host || {}) };
2023-01-18 20:59:00 +01:00
}
function validate(form) {
return form.name && form.uri && true;
}
async function submit() {
if (!valid) {
return;
}
try {
2023-01-20 19:08:56 +01:00
if (host && hostKey) {
await UpdateHost(hostKey, JSON.stringify(form));
}
else {
await AddHost(JSON.stringify(form));
}
2023-01-18 20:59:00 +01:00
show = false;
dispatch('reload');
}
catch (e) {
error = e;
}
}
</script>
2023-01-20 19:08:56 +01:00
<Modal bind:show title={host ? `Edit ${host.name}` : 'Create a new host'}>
2023-01-18 20:59:00 +01:00
<form on:submit|preventDefault={submit}>
<label class="field">
<span class="label">Label</span>
2023-01-20 19:08:56 +01:00
<input type="text" placeholder="mywebsite.com MongoDB" bind:value={form.name} use:input={{ autofocus: true }} />
2023-01-18 20:59:00 +01:00
</label>
<label class="field">
<span class="label">Connection string</span>
2023-01-20 19:08:56 +01:00
<input type="text" placeholder="mongodb://..." bind:value={form.uri} spellcheck="false" use:input />
2023-01-18 20:59:00 +01:00
</label>
<div class="result">
<div>
{#if error}
<div class="error">{error}</div>
{/if}
</div>
2023-01-20 19:08:56 +01:00
<button class="btn" disabled={!valid} type="submit">
{host ? 'Save' : 'Create'}
</button>
2023-01-18 20:59:00 +01:00
</div>
</form>
</Modal>
<style>
form {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.result {
display: flex;
justify-content: space-between;
align-items: center;
}
</style>