From 54119f42212ce24d0e2b788a9bc6fae0f5c4e416 Mon Sep 17 00:00:00 2001 From: Arman Date: Sun, 2 Apr 2023 15:33:39 +0200 Subject: [PATCH] refactor: relationship and more --- .../elements/forms/inputSelectSearch.svelte | 41 ++-- src/lib/helpers/array.ts | 3 + .../attributes/relationship.svelte | 8 + .../attributes/relationship.svelte | 190 ++++++++---------- .../document-[document]/data/+page.svelte | 13 +- 5 files changed, 125 insertions(+), 130 deletions(-) diff --git a/src/lib/elements/forms/inputSelectSearch.svelte b/src/lib/elements/forms/inputSelectSearch.svelte index 9f38acb1c..ad142c59c 100644 --- a/src/lib/elements/forms/inputSelectSearch.svelte +++ b/src/lib/elements/forms/inputSelectSearch.svelte @@ -20,7 +20,7 @@ export let required = false; export let disabled = false; export let autofocus = false; - export let customOutput = false; + export let interactiveOutput = false; // Input value export let search = ''; // The actual selected value @@ -79,7 +79,7 @@ function selectOption(option: Option) { value = option.value; - !customOutput && (search = option.label); + search = option.label; // It's not working without this line. element.value = search; hasFocus = false; @@ -112,32 +112,31 @@
- {#if $$slots.default && customOutput && selectedOption} - - - - {:else} - (hasFocus = true)} - on:click={() => (hasFocus = true)} - on:input={handleInput} - on:keydown={handleKeydown} /> + {#if $$slots.output && selectedOption} + {/if} + (hasFocus = true)} + on:click={() => (hasFocus = true)} + on:input={handleInput} + on:keydown={handleKeydown} /> +
{#if showClearBtn} @@ -145,7 +144,7 @@ diff --git a/src/lib/helpers/array.ts b/src/lib/helpers/array.ts index 4527291b4..096d65d6f 100644 --- a/src/lib/helpers/array.ts +++ b/src/lib/helpers/array.ts @@ -5,6 +5,9 @@ export function intersection(arr1: unknown[], arr2: unknown[]) { } export function difference(arr1: unknown[], arr2: unknown[]) { + if (!Array.isArray(arr1) || !Array.isArray(arr2)) { + return Array.isArray(arr1) ? arr1 : Array.isArray(arr2) ? arr2 : []; + } const set = new Set(arr2); const difference = new Set(arr1.filter((elem) => !set.has(elem))); return Array.from(difference); diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/relationship.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/relationship.svelte index 0e97e2a55..54c648925 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/relationship.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/relationship.svelte @@ -141,6 +141,7 @@ bind:search bind:value={data.relatedCollection} required + interactiveOutput placeholder="Select a collection" disabled={editing} options={collections?.map((n) => ({ value: n.$id, label: n.$id, data: [n.name] })) ?? []} @@ -149,6 +150,13 @@ {o.label} + + + + {o.label} + + + {#if data?.relatedCollection} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/relationship.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/relationship.svelte index 1e99f0e05..f4a44ed70 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/relationship.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/relationship.svelte @@ -74,7 +74,12 @@ value = singleRel; } - $: paginatedItems = relatedList.slice(offset, offset + limit); + $: paginatedItems = editing + ? relatedList + .slice() + .reverse() + .slice(offset, offset + limit) + : []; $: total = relatedList?.length; $: options = documentList?.documents?.map((n) => { @@ -87,25 +92,65 @@ }) ?? []; -{#if editing && isRelationshipToMany(attribute) && total > 0} -
+{#if isRelationshipToMany(attribute)} +
- + {#if editing || total === 0} + + {/if}
+
    + {#if !editing} + {#each relatedList as item, i} +
  • + + + {o.label} + + + + + {o.label} + + + + + +
  • + {/each} + {/if} + {#if showInput}
  • {o.label} + + + + {o.label} + + +
  • {/if} - {#if paginatedItems} - {#each relatedList as item, i} + {#if paginatedItems && editing} + {#each paginatedItems as item, i}
  • {o.label} + + + + {o.label} + + +
-{:else if isRelationshipToMany(attribute)} -
-
-
- + {#if editing} +
+

Total results: {total}

+
- {#if total === 0} - - {/if} -
- -
    - {#each relatedList as doc, i} -
  • - - - {o.label} - - - -
  • - {/each} - - {#if showInput} -
  • - !relatedList.includes(n.value))} - on:select={() => { - showInput = false; - }} - let:option={o}> - - {o.label} - - - -
  • - {/if} -
- - {#if total > 0} + {/if} + {#if total > 0 && !editing}