2023-01-18 20:59:00 +01:00
|
|
|
<script>
|
2023-02-15 19:27:51 +01:00
|
|
|
import Modal from '$components/modal.svelte';
|
|
|
|
import input from '$lib/actions/input';
|
2023-06-18 21:31:55 +02:00
|
|
|
import hostTree from '$lib/stores/hosttree';
|
2023-02-15 19:27:51 +01:00
|
|
|
import { AddHost, UpdateHost } from '$wails/go/app/App';
|
2023-06-18 21:31:55 +02:00
|
|
|
import { createEventDispatcher, onMount } from 'svelte';
|
2023-01-18 20:59:00 +01:00
|
|
|
|
2023-01-20 19:08:56 +01:00
|
|
|
export let hostKey = '';
|
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-06-18 21:31:55 +02:00
|
|
|
$: host = $hostTree[hostKey];
|
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 {
|
2023-05-31 20:20:39 +02:00
|
|
|
const newHostKey = await AddHost(JSON.stringify(form));
|
|
|
|
if (newHostKey) {
|
|
|
|
hostKey = newHostKey;
|
|
|
|
}
|
2023-01-20 19:08:56 +01:00
|
|
|
}
|
2023-06-18 21:31:55 +02:00
|
|
|
dispatch('updated', form);
|
|
|
|
dispatch('close');
|
2023-01-18 20:59:00 +01:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
}
|
2023-06-18 21:31:55 +02:00
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
form = { ...(host || {}) };
|
|
|
|
});
|
2023-01-18 20:59:00 +01:00
|
|
|
</script>
|
|
|
|
|
2023-06-18 21:31:55 +02:00
|
|
|
<Modal title={host ? `Edit ${host.name}` : 'Create a new host'} on:close>
|
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>
|
2023-05-31 20:20:39 +02:00
|
|
|
</form>
|
2023-01-18 20:59:00 +01:00
|
|
|
|
2023-05-31 20:20:39 +02:00
|
|
|
<div class="result" slot="footer">
|
|
|
|
<div>
|
|
|
|
{#if error}
|
|
|
|
<div class="error">{error}</div>
|
|
|
|
{/if}
|
2023-01-18 20:59:00 +01:00
|
|
|
</div>
|
2023-05-31 20:20:39 +02:00
|
|
|
<button class="btn" disabled={!valid} on:click={submit}>
|
|
|
|
{host ? 'Save' : 'Create'}
|
|
|
|
</button>
|
|
|
|
</div>
|
2023-01-18 20:59:00 +01:00
|
|
|
</Modal>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
form {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
gap: 0.5rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
.result {
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
</style>
|