From 2d867ab83461bc2f98225d85a0ad074f371afdd5 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Wed, 23 Nov 2022 13:59:08 +0000 Subject: [PATCH 1/8] fix: createIndex attribute option changing all --- .../indexes/createIndex.svelte | 184 +++++++++--------- 1 file changed, 96 insertions(+), 88 deletions(-) diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte index eb3ebecd45..440ffb3fa5 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte @@ -26,19 +26,19 @@ ]; let newAttr = false; let selectedType = 'key'; - $: attributeOptions = $collection.attributes.map((attribute: Attributes) => ({ + + let attributeOptions = $collection.attributes.map((attribute: Attributes) => ({ value: attribute.key, label: attribute.key })); - $: attributeList = []; + let attributeList = [] as Array<{ value: string; order: string }>; let selectedAttribute = ''; let selectedOrder = ''; onMount(() => { - if (externalAttribute) { - attributeList = [{ value: externalAttribute.key, order: 'ASC' }]; - } + if (!externalAttribute) return; + attributeList = [{ value: externalAttribute.key, order: 'ASC' }]; }); $: if (showCreateIndex) { @@ -47,91 +47,107 @@ selectedType = 'key'; key = null; } - const created = async () => { - if (key && selectedAttribute && selectedOrder && selectedType) { - if (selectedAttribute && selectedOrder) { - attributeList.push({ value: selectedAttribute, order: selectedOrder }); - selectedAttribute = selectedOrder = ''; - } - try { - await sdkForProject.databases.createIndex( - databaseId, - $collection.$id, - key, - selectedType, - attributeList.map((a) => a.value), - attributeList.map((a) => a.order) - ); - invalidate(Dependencies.COLLECTION); - addNotification({ - message: 'Index has been created', - type: 'success' - }); - trackEvent('submit_index_create'); - } catch (error) { - addNotification({ - message: error.message, - type: 'error' - }); - } + const create = async () => { + if (!(key && selectedAttribute && selectedOrder && selectedType)) { + error = 'All fields are required'; + return; + } + + if (selectedAttribute && selectedOrder) { + attributeList.push({ value: selectedAttribute, order: selectedOrder }); + selectedAttribute = selectedOrder = ''; + } + + try { + await sdkForProject.databases.createIndex( + databaseId, + $collection.$id, + key, + selectedType, + attributeList.map((a) => a.value), + attributeList.map((a) => a.order) + ); + invalidate(Dependencies.COLLECTION); + addNotification({ + message: 'Index has been created', + type: 'success' + }); + trackEvent('submit_index_create'); + } catch (error) { + addNotification({ + message: error.message, + type: 'error' + }); + } finally { showCreateIndex = false; - } else error = 'All fields are required'; + } + }; + + const addAttribute = () => { + if (!(selectedAttribute && selectedOrder)) return; + + // We assign instead of pushing to trigger Svelte's reactivity + attributeList = [...attributeList, { value: selectedAttribute, order: selectedOrder }]; + selectedAttribute = ''; + selectedOrder = ''; + newAttr = true; }; + + $: console.log(attributeList); - + Create Index - {#if attributeList?.length} - {#each attributeList as index, i} -
  • -
    - -
    -
    - -
    - -
    - -
    -
  • - {/each} - {/if} + {#each attributeList as attribute, i (selectedAttribute + i)} +
  • +
    + +
    +
    + +
    + +
    + +
    +
  • + {/each} + {#if !attributeList?.length || newAttr}
  • - @@ -151,7 +167,7 @@
    - @@ -171,16 +187,8 @@
  • {/if} - From 77c1f8299ef96986b987b8f00c6d3cf6024d9864 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Wed, 23 Nov 2022 14:23:26 +0000 Subject: [PATCH 2/8] refactor: improve createIndex fix --- package-lock.json | 12 ++++++------ .../indexes/createIndex.svelte | 12 +++--------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index 785ae480ae..c7dbe73b2d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6882,9 +6882,9 @@ } }, "node_modules/svelte": { - "version": "3.52.0", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.52.0.tgz", - "integrity": "sha512-FxcnEUOAVfr10vDU5dVgJN19IvqeHQCS1zfe8vayTfis9A2t5Fhx+JDe5uv/C3j//bB1umpLJ6quhgs9xyUbCQ==", + "version": "3.53.1", + "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.53.1.tgz", + "integrity": "sha512-Q4/hHkktZogGhN5iqxqSi9sjEVoe/NbIxX4hXEHoasTxj+TxEQVAq66LnDMdAZxjmsodkoI5F3slqsS68U7FNw==", "engines": { "node": ">= 8" } @@ -12929,9 +12929,9 @@ "dev": true }, "svelte": { - "version": "3.52.0", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.52.0.tgz", - "integrity": "sha512-FxcnEUOAVfr10vDU5dVgJN19IvqeHQCS1zfe8vayTfis9A2t5Fhx+JDe5uv/C3j//bB1umpLJ6quhgs9xyUbCQ==" + "version": "3.53.1", + "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.53.1.tgz", + "integrity": "sha512-Q4/hHkktZogGhN5iqxqSi9sjEVoe/NbIxX4hXEHoasTxj+TxEQVAq66LnDMdAZxjmsodkoI5F3slqsS68U7FNw==" }, "svelte-check": { "version": "2.9.2", diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte index 440ffb3fa5..ec52794a4f 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte @@ -93,8 +93,6 @@ selectedOrder = ''; newAttr = true; }; - - $: console.log(attributeList); @@ -103,7 +101,7 @@ - {#each attributeList as attribute, i (selectedAttribute + i)} + {#each attributeList as attribute, i}
  • + + @@ -121,6 +113,8 @@
    @@ -129,10 +123,10 @@
    @@ -140,52 +134,11 @@
  • {/each} - {#if !attributeList?.length || newAttr} -
  • -
    - -
    -
    - -
    -
    - -
    -
  • - {/if} - - + {JSON.stringify(attributeList)}
    From b5870f1642b9f75ad0f629ca8c7b8003044bc2ab Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Wed, 23 Nov 2022 16:26:33 +0000 Subject: [PATCH 5/8] chore: add jsdoc to array helper --- src/lib/helpers/array.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib/helpers/array.ts b/src/lib/helpers/array.ts index 8fd324d4d0..12cde8c4b8 100644 --- a/src/lib/helpers/array.ts +++ b/src/lib/helpers/array.ts @@ -14,7 +14,16 @@ export function symmetricDifference(arr1: unknown[], arr2: unknown[]) { return difference(arr1, arr2).concat(difference(arr2, arr1)); } -export function remove(arr: T[], index: number) { +/** + * Removes the element at the specified index from the array, and returns a new array. + * + * @export + * @template T + * @param {T[]} arr + * @param {number} index + * @returns {T[]} + */ +export function remove(arr: T[], index: number): T[] { // Remove the element at the given index, return a new array return [...arr.slice(0, index), ...arr.slice(index + 1)]; } From a5b3f10d6c4dadd76edc18fbabb18d866c80be1e Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Wed, 23 Nov 2022 16:27:10 +0000 Subject: [PATCH 6/8] refactor: remove explicit type --- .../collection-[collection]/indexes/createIndex.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte index 5d3fb61173..8c65ef6ea8 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte @@ -31,7 +31,7 @@ value: attribute.key, label: attribute.key })); - let attributeList = [{ value: '', order: '' }] as Array<{ value: string; order: string }>; + let attributeList = [{ value: '', order: '' }]; $: addAttributeDisabled = !attributeList.at(-1)?.value || !attributeList.at(-1)?.order; From 1da9f10dc6dd089f1e7975c3f3245115bdd02079 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Wed, 23 Nov 2022 16:28:13 +0000 Subject: [PATCH 7/8] chore: remove leftover debug string --- .../collection-[collection]/indexes/createIndex.svelte | 1 - 1 file changed, 1 deletion(-) diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte index 8c65ef6ea8..27eb65ced5 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte @@ -138,7 +138,6 @@