Merge pull request #191 from appwrite/feat-create-index-from-attributes-page

feat: pre-filled create index from attributes page
This commit is contained in:
Torsten Dittmann authored and GitHub committed 2023-01-10 15:43:07 +01:00
commit 63886d210e
2 files changed
+37 -21

No files matched your search

@@ -88,12 +88,14 @@
on:click={() => {
selectedAttribute = attribute;
showOverview = true;
showDropdown[index] = false;
}}>Overview</DropListItem>
<DropListItem
icon="plus"
on:click={() => {
selectedAttribute = attribute;
showCreateIndex = true;
showDropdown[index] = false;
}}>Create Index</DropListItem>
<DropListItem
icon="trash"
@@ -1,17 +1,17 @@
<script lang="ts">
import { Modal } from '$lib/components';
import { Button, InputText, FormList, InputSelect } from '$lib/elements/forms';
import { collection } from '../store';
import { onMount } from 'svelte';
import { sdkForProject } from '$lib/stores/sdk';
import { addNotification } from '$lib/stores/notifications';
import { goto, invalidate } from '$app/navigation';
import { base } from '$app/paths';
import { page } from '$app/stores';
import type { Attributes } from '../store';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import Select from './select.svelte';
import { trackEvent } from '$lib/actions/analytics';
import { Modal } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Button, FormList, InputSelect, InputText } from '$lib/elements/forms';
import { remove } from '$lib/helpers/array';
import { addNotification } from '$lib/stores/notifications';
import { sdkForProject } from '$lib/stores/sdk';
import type { Attributes } from '../store';
import { collection } from '../store';
import Select from './select.svelte';
export let showCreateIndex = false;
export let externalAttribute: Attributes = null;
@@ -32,26 +32,31 @@
label: attribute.key
}));
let attributeList = [{ value: '', order: '' }];
let creating = false;
$: addAttributeDisabled = !attributeList.at(-1)?.value || !attributeList.at(-1)?.order;
onMount(() => {
if (!externalAttribute) return;
attributeList = [{ value: externalAttribute.key, order: 'ASC' }];
});
$: if (showCreateIndex) {
attributeList = [{ value: '', order: '' }];
function initialize() {
attributeList = externalAttribute
? [{ value: externalAttribute.key, order: 'ASC' }]
: [{ value: '', order: '' }];
selectedType = 'key';
key = null;
}
$: if (showCreateIndex) {
initialize();
}
$: addAttributeDisabled =
!attributeList.at(-1)?.value || !attributeList.at(-1)?.order || creating;
async function create() {
if (!(key && selectedType && !addAttributeDisabled)) {
error = 'All fields are required';
return;
}
creating = true;
try {
await sdkForProject.databases.createIndex(
databaseId,
@@ -61,7 +66,15 @@
attributeList.map((a) => a.value),
attributeList.map((a) => a.order)
);
invalidate(Dependencies.COLLECTION);
await Promise.allSettled([
invalidate(Dependencies.COLLECTION),
invalidate(Dependencies.DATABASE)
]);
goto(
`${base}/console/project-${$page.params.project}/databases/database-${databaseId}/collection-${$collection.$id}/indexes`
);
addNotification({
message: 'Index has been created',
type: 'success'
@@ -74,6 +87,7 @@
});
} finally {
showCreateIndex = false;
creating = false;
}
}
@@ -140,6 +154,6 @@
</FormList>
<svelte:fragment slot="footer">
<Button secondary on:click={() => (showCreateIndex = false)}>Cancel</Button>
<Button submit>Create</Button>
<Button submit disabled={creating}>Create</Button>
</svelte:fragment>
</Modal>