From a3335cdc4e76e9d0baf6a66f31e73569d35633cf Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Wed, 15 Feb 2023 19:53:49 +0100 Subject: [PATCH] Insert: form improvements --- frontend/src/components/details.svelte | 2 +- frontend/src/lib/strings.js | 4 ++ .../collection/components/form.svelte | 6 +++ .../connection/collection/insert.svelte | 40 ++++++++++++++----- 4 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 frontend/src/lib/strings.js diff --git a/frontend/src/components/details.svelte b/frontend/src/components/details.svelte index 566ed75..ab6091c 100644 --- a/frontend/src/components/details.svelte +++ b/frontend/src/components/details.svelte @@ -22,7 +22,7 @@ {title} {#if deletable} - {/if} diff --git a/frontend/src/lib/strings.js b/frontend/src/lib/strings.js new file mode 100644 index 0000000..8e47075 --- /dev/null +++ b/frontend/src/lib/strings.js @@ -0,0 +1,4 @@ +export function capitalise(string = '') { + const capitalised = string.charAt(0).toUpperCase() + string.slice(1); + return capitalised; +} diff --git a/frontend/src/organisms/connection/collection/components/form.svelte b/frontend/src/organisms/connection/collection/components/form.svelte index eb777ce..698950d 100644 --- a/frontend/src/organisms/connection/collection/components/form.svelte +++ b/frontend/src/organisms/connection/collection/components/form.svelte @@ -3,10 +3,12 @@ import Icon from '$components/icon.svelte'; import { inputTypes } from '$lib/mongo'; import { resolveKeypath, setValue } from '$lib/keypaths'; + import Hint from '$components/hint.svelte'; export let item = {}; export let view = {}; export let valid = false; + export let emptyHint = ''; const validity = {}; $: valid = Object.values(validity).every(v => v !== false); @@ -51,6 +53,10 @@ + {:else} + {#if emptyHint} + {emptyHint} + {/if} {/each} {/if} diff --git a/frontend/src/organisms/connection/collection/insert.svelte b/frontend/src/organisms/connection/collection/insert.svelte index 9c0dd99..a172b50 100644 --- a/frontend/src/organisms/connection/collection/insert.svelte +++ b/frontend/src/organisms/connection/collection/insert.svelte @@ -7,6 +7,7 @@ import { randomString } from '$lib/math'; import { inputTypes } from '$lib/mongo'; import views from '$lib/stores/views'; + import { capitalise } from '$lib/strings'; import { InsertItems } from '$wails/go/app/App'; import { EJSON } from 'bson'; import { createEventDispatcher } from 'svelte'; @@ -16,12 +17,14 @@ const dispatch = createEventDispatcher(); const formValidity = {}; + let json = ''; let newItems = []; let insertedIds; let objectViewerData = ''; let viewType = 'form'; let allValid = false; + $: viewsForCollection = views.forCollection(collection.hostKey, collection.dbKey, collection.key); $: oppositeViewType = viewType === 'table' ? 'form' : 'table'; $: allValid = Object.values(formValidity).every(v => v !== false); @@ -38,6 +41,10 @@ } } + $: if ((viewType === 'form') && !newItems?.length) { + newItems = [ {} ]; + } + async function insert() { insertedIds = await InsertItems(collection.hostKey, collection.dbKey, collection.key, json); if ((collection.viewKey === 'list') && insertedIds) { @@ -66,8 +73,17 @@ } } - function addRow() { - newItems = [ ...newItems, {} ]; + function addRow(beforeIndex = -1) { + if ((beforeIndex === -1) || (typeof beforeIndex !== 'number')) { + newItems = [ ...newItems, {} ]; + } + else { + newItems = [ + ...newItems.slice(0, beforeIndex), + {}, + ...newItems.slice(beforeIndex + 1), + ]; + } } function deleteRow(index) { @@ -99,7 +115,12 @@ on:delete={() => deleteRow(index)} >
-
+
{/each} @@ -123,15 +144,16 @@ /> {/if} + + {#if collection.viewKey !== 'list'} + + {/if}
- {#if collection.viewKey !== 'list'} - - {/if} {#if insertedIds} Success! {insertedIds.length} document{insertedIds.length > 1 ? 's' : ''} inserted {/if} @@ -145,7 +167,7 @@ {/if}