From 1cf89b32784f13c69f27b207c696c7407b933fa4 Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Sun, 29 Jan 2023 20:00:15 +0100 Subject: [PATCH] Forms! --- frontend/src/actions.js | 77 +++++- frontend/src/components/grid-items.svelte | 2 +- frontend/src/components/icon.svelte | 14 + .../collection/components/form.svelte | 97 +++++++ .../collection/components/forminput.svelte | 76 ++++++ .../indexdetail.svelte} | 8 +- .../collection/components/viewconfig.svelte | 242 ++++++++++++++++++ .../collection/find-viewconfig.svelte | 182 ------------- .../connection/collection/find.svelte | 71 +++-- .../connection/collection/index.svelte | 21 +- .../connection/collection/indexes.svelte | 2 +- .../connection/collection/insert.svelte | 82 +++++- .../connection/collection/remove.svelte | 2 +- .../connection/collection/update.svelte | 25 +- .../connection/export/dumpinfo.svelte | 2 - frontend/src/organisms/settings/index.svelte | 2 +- frontend/src/stores.js | 31 ++- frontend/src/style.css | 130 +++++----- frontend/src/utils.js | 60 ++++- .../{collection_find_views.go => views.go} | 61 ++--- 20 files changed, 817 insertions(+), 370 deletions(-) create mode 100644 frontend/src/organisms/connection/collection/components/form.svelte create mode 100644 frontend/src/organisms/connection/collection/components/forminput.svelte rename frontend/src/organisms/connection/collection/{indexes-detail.svelte => components/indexdetail.svelte} (94%) create mode 100644 frontend/src/organisms/connection/collection/components/viewconfig.svelte delete mode 100644 frontend/src/organisms/connection/collection/find-viewconfig.svelte rename internal/app/{collection_find_views.go => views.go} (72%) diff --git a/frontend/src/actions.js b/frontend/src/actions.js index eca588a..e425351 100644 --- a/frontend/src/actions.js +++ b/frontend/src/actions.js @@ -1,13 +1,72 @@ -export function input(node, { json, autofocus } = { json: false, autofocus: false }) { +import { int32, int64, isInt, uint64 } from './utils'; + +export function input(node, { autofocus, type, onValid, onInvalid, mandatory } = { + autofocus: false, + type: '', + onValid: () => 0, + onInvalid: () => 0, + mandatory: false, +}) { + + const getMessage = () => { + const checkInteger = () => (isInt(node.value) ? false : 'Value must be an integer'); + const checkNumberBoundaries = boundaries => { + if (node.value < boundaries[0]) { + return `Input is too low for type ${type}`; + } + else if (node.value > boundaries[1]) { + return `Input is too high for type ${type}`; + } + else { + return true; + } + }; + + switch (type) { + case 'json': + try { + JSON.parse(node.value); + return false; + } + catch { + return 'Invalid JSON'; + } + + case 'int': // int32 + return checkInteger() || checkNumberBoundaries(int32); + + case 'long': // int64 + return checkInteger() || checkNumberBoundaries(int64); + + case 'uint64': + return checkInteger() || checkNumberBoundaries(uint64); + + case 'string': + if (mandatory && (node.value == '')) { + return 'This field cannot empty'; + } + return false; + + case 'double': + case 'decimal': + default: + return false; + } + }; + const handleInput = () => { - if (json) { - try { - JSON.parse(node.value); - node.classList.remove('invalid'); - } - catch { - node.classList.add('invalid'); - } + const invalid = getMessage(); + if (invalid) { + node.classList.add('invalid'); + node.setCustomValidity(invalid); + node.reportValidity(); + onInvalid?.(); + } + else { + node.classList.remove('invalid'); + node.setCustomValidity(''); + node.reportValidity(); + onValid?.(); } }; diff --git a/frontend/src/components/grid-items.svelte b/frontend/src/components/grid-items.svelte index b1a5b36..6786d7b 100644 --- a/frontend/src/components/grid-items.svelte +++ b/frontend/src/components/grid-items.svelte @@ -111,7 +111,7 @@ {#if item.children?.length} + + + {/each} +{/if} + + diff --git a/frontend/src/organisms/connection/collection/components/forminput.svelte b/frontend/src/organisms/connection/collection/components/forminput.svelte new file mode 100644 index 0000000..97517a6 --- /dev/null +++ b/frontend/src/organisms/connection/collection/components/forminput.svelte @@ -0,0 +1,76 @@ + + +
+ {#if type === 'string'} + + {:else if numericTypes.includes(type)} + + {:else if type === 'bool'} + + {:else if type === 'date'} + {@const isNotDate = !isDate(value)} + + + {/if} +
+ + diff --git a/frontend/src/organisms/connection/collection/indexes-detail.svelte b/frontend/src/organisms/connection/collection/components/indexdetail.svelte similarity index 94% rename from frontend/src/organisms/connection/collection/indexes-detail.svelte rename to frontend/src/organisms/connection/collection/components/indexdetail.svelte index 8f663cb..94d0ef9 100644 --- a/frontend/src/organisms/connection/collection/indexes-detail.svelte +++ b/frontend/src/organisms/connection/collection/components/indexdetail.svelte @@ -1,8 +1,8 @@ + + + removeView(e.detail)} + bind:selectedKey={activeViewKey} + /> + +
+ {#if $views[activeViewKey]} +
+ {#key activeViewKey} + + {/key} + +
+ + {#if $views[activeViewKey].type === 'list'} +
+ + +
+ {:else if $views[activeViewKey].type === 'table'} +
+ {#each $views[activeViewKey].columns as column, columnIndex} +
+ + + + + + + + + + + + +
+ {:else} +

No columns yet

+ {/each} +
+ + + {/if} + {/if} +
+
+ + diff --git a/frontend/src/organisms/connection/collection/find-viewconfig.svelte b/frontend/src/organisms/connection/collection/find-viewconfig.svelte deleted file mode 100644 index 1039468..0000000 --- a/frontend/src/organisms/connection/collection/find-viewconfig.svelte +++ /dev/null @@ -1,182 +0,0 @@ - - - - removeView(e.detail)} - bind:selectedKey={activeViewKey} - /> - -
- {#if $views[activeViewKey]} -
- {#key activeViewKey} - - {/key} - -
- - {#if $views[activeViewKey].type === 'list'} -
- - -
- {:else if $views[activeViewKey].type === 'table'} - {#each $views[activeViewKey].columns as column, columnIndex} -
- - - - - -
- {/each} - - - {/if} - {/if} -
-
- - diff --git a/frontend/src/organisms/connection/collection/find.svelte b/frontend/src/organisms/connection/collection/find.svelte index 4f7e43d..b251a4e 100644 --- a/frontend/src/organisms/connection/collection/find.svelte +++ b/frontend/src/organisms/connection/collection/find.svelte @@ -1,18 +1,18 @@ @@ -108,19 +106,19 @@