diff --git a/src/lib/helpers/array.ts b/src/lib/helpers/array.ts index cd7a923fba..12cde8c4b8 100644 --- a/src/lib/helpers/array.ts +++ b/src/lib/helpers/array.ts @@ -3,6 +3,7 @@ export function intersection(arr1: unknown[], arr2: unknown[]) { const intersection = new Set(arr1.filter((elem) => set.has(elem))); return Array.from(intersection); } + export function difference(arr1: unknown[], arr2: unknown[]) { const set = new Set(arr2); const intersection = new Set(arr1.filter((elem) => !set.has(elem))); @@ -12,3 +13,17 @@ export function difference(arr1: unknown[], arr2: unknown[]) { export function symmetricDifference(arr1: unknown[], arr2: unknown[]) { return difference(arr1, arr2).concat(difference(arr2, arr1)); } + +/** + * 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)]; +} 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..5e7ef6481b 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 @@ -11,6 +11,7 @@ import { Dependencies } from '$lib/constants'; import Select from './select.svelte'; import { trackEvent } from '$lib/actions/analytics'; + import { remove } from '$lib/helpers/array'; export let showCreateIndex = false; export let externalAttribute: Attributes = null; @@ -24,115 +25,77 @@ { value: 'unique', label: 'Unique' }, { value: 'fulltext', label: 'FullText' } ]; - 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 = [{ value: '', order: '' }]; - let selectedAttribute = ''; - let selectedOrder = ''; + $: addAttributeDisabled = !attributeList.at(-1)?.value || !attributeList.at(-1)?.order; onMount(() => { - if (externalAttribute) { - attributeList = [{ value: externalAttribute.key, order: 'ASC' }]; - } + if (!externalAttribute) return; + attributeList = [{ value: externalAttribute.key, order: 'ASC' }]; }); $: if (showCreateIndex) { - attributeList = []; - selectedOrder = selectedAttribute = ''; + attributeList = [{ value: '', order: '' }]; 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' - }); - } + async function create() { + if (!(key && selectedType && !addAttributeDisabled)) { + error = 'All fields are required'; + return; + } + + 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'; - }; + } + } + + function addAttribute() { + if (addAttributeDisabled) return; + + // We assign instead of pushing to trigger Svelte's reactivity + attributeList = [...attributeList, { value: '', order: '' }]; + } - + Create Index - {#if attributeList?.length} - {#each attributeList as index, i} -
  • -
    - -
    -
    - -
    - -
    - -
    -
  • - {/each} - {/if} - {#if !attributeList?.length || newAttr} + {#each attributeList as attribute, i}
  • -
    - + @@ -141,9 +104,7 @@ {#each attributeOptions as option} - {/each} @@ -151,36 +112,28 @@
    - +
    +
  • - {/if} -