feat: attributes and indexes pages

This commit is contained in:
Arman committed 2022-07-21 17:43:05 +02:00
1 parent 7a9a43a919
commit fc9dba96f9
5 files changed
+421 -24

No files matched your search

@@ -0,0 +1,196 @@
<script lang="ts">
import { Modal } from '$lib/components';
import { Button, InputText, Form, FormList, InputSelect } from '$lib/elements/forms';
import { collection } from './store';
import type { Attributes } from './store';
import { onMount } from 'svelte';
import { sdkForProject } from '$lib/stores/sdk';
export let showCreateIndex = false;
export let externalAttribute: Attributes = null;
let key: string = null;
let types = [
{ value: 'key', label: 'Key' },
{ value: 'unique', label: 'Unique' },
{ value: 'fulltext', label: 'FullText' }
];
let newAttr = false;
let selectedType: string = '';
let attributeOptions = $collection.attributes.map((attribute: Attributes) => ({
value: attribute.key,
label: attribute.key
}));
$: attributeList = [];
let selectedAttribute = '';
let selectedOrder: string = '';
onMount(() => {
if (externalAttribute) {
attributeList = [{ value: externalAttribute.key, order: 'ASC' }];
}
});
$: if (showCreateIndex) {
attributeList = [];
selectedType = selectedOrder = selectedAttribute = '';
key = null;
}
const created = async () => {
if (selectedAttribute && selectedOrder) {
attributeList.push({ value: selectedAttribute, order: selectedOrder });
selectedAttribute = selectedOrder = '';
}
await sdkForProject.databases.createIndex(
$collection.$id,
key,
selectedType,
attributeList.map((a) => a.value),
attributeList.map((a) => a.order)
);
showCreateIndex = false;
};
</script>
<Form on:submit={created}>
<Modal size="big" bind:show={showCreateIndex}>
<svelte:fragment slot="header">Create Index</svelte:fragment>
<FormList>
<InputText
id="key"
label="Index Key"
placeholder="Enter Key"
bind:value={key}
autofocus
required />
<InputSelect
options={types}
id="type"
label="Index type"
placeholder="Select type"
bind:value={selectedType}
required />
{#if attributeList?.length}
{#each attributeList as index, i}
<li class="form-item is-multiple">
<div class="form-item-part u-stretch">
<label class="label" for="attribute">Attribute</label>
<div class="select">
<select id="attribute" bind:value={index.value}>
<optgroup label="Internal">
<option value="$id">$id</option>
<option value="$createdAt">$createdAt</option>
<option value="$updatedAt">$updatedAt</option>
</optgroup>
<optgroup label="Attributes">
{#each attributeOptions as option}
<option
value={option.value}
selected={option.value === selectedAttribute}>
{option.label}
</option>
{/each}
</optgroup>
</select>
<span class="icon-cheveron-down" aria-hidden="true" />
</div>
</div>
<div class="form-item-part u-stretch">
<label class="label" for="order">Order</label>
<div class="select">
<select id="order" bind:value={index.order}>
<option value="ASC"> ASC </option>
<option value="DESC"> DESC </option>
</select>
<span class="icon-cheveron-down" aria-hidden="true" />
</div>
</div>
<div class="form-item-part u-cross-child-end">
<Button
text
disabled={externalAttribute && i === 0}
on:click={() => {
if (i === 0) attributeList = [];
attributeList = attributeList.splice(i, 1);
}}>
<span class="icon-x" aria-hidden="true" />
</Button>
</div>
</li>
{/each}
{/if}
{#if !attributeList?.length || newAttr}
<li class="form-item is-multiple">
<div class="form-item-part u-stretch">
<label class="label" for="attribute">Attribute</label>
<div class="select">
<select required id="attribute" bind:value={selectedAttribute}>
<option value="" disabled selected hidden>Select Attribute</option>
<optgroup label="Internal">
<option value="$id">$id</option>
<option value="$createdAt">$createdAt</option>
<option value="$updatedAt">$updatedAt</option>
</optgroup>
<optgroup label="Attributes">
{#each attributeOptions as option}
<option
value={option.value}
selected={option.value === selectedAttribute}>
{option.label}
</option>
{/each}
</optgroup>
</select>
<span class="icon-cheveron-down" aria-hidden="true" />
</div>
</div>
<div class="form-item-part u-stretch">
<label class="label" for="order">Order</label>
<div class="select">
<select id="order" bind:value={selectedOrder}>
<option value="" disabled selected hidden>Select Order</option>
<option value="ASC"> ASC </option>
<option value="DESC"> DESC </option>
</select>
<span class="icon-cheveron-down" aria-hidden="true" />
</div>
</div>
<div class="form-item-part u-cross-child-end">
<Button
text
disabled={false}
on:click={() => {
newAttr = false;
selectedAttribute = selectedOrder = '';
}}>
<span class="icon-x" aria-hidden="true" />
</Button>
</div>
</li>
{/if}
<Button
text
on:click={() => {
newAttr = true;
if (selectedAttribute && selectedOrder) {
attributeList.push({ value: selectedAttribute, order: selectedOrder });
selectedAttribute = selectedOrder = '';
}
}}>
<span class="icon-plus" aria-hidden="true" />
<span class="text"> Add attribute </span>
</Button>
</FormList>
<svelte:fragment slot="footer">
<Button secondary on:click={() => (showCreateIndex = false)}>Cancel</Button>
<Button submit>Create</Button>
</svelte:fragment>
</Modal>
</Form>
@@ -0,0 +1,41 @@
<script lang="ts">
import { Modal } from '$lib/components';
import { Button, Form } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { collection } from './store';
import { sdkForProject } from '$lib/stores/sdk';
import type { Models } from '@aw-labs/appwrite-console';
import { createEventDispatcher } from 'svelte';
export let showDelete = false;
export let selectedIndex: Models.Index;
const dispatch = createEventDispatcher();
const deleteElement = async () => {
try {
await sdkForProject.databases.deleteIndex($collection.$id, selectedIndex.key);
showDelete = false;
dispatch('deleted');
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
}
};
</script>
<Form on:submit={deleteElement}>
<Modal warning={true} bind:show={showDelete}>
<svelte:fragment slot="header">Delete Index</svelte:fragment>
<p>
Are you sure you want to delete <b>'{selectedIndex.key}' from {$collection.name}</b>?
</p>
<svelte:fragment slot="footer">
<Button text on:click={() => (showDelete = false)}>Cancel</Button>
<Button secondary submit>Delete</Button>
</svelte:fragment>
</Modal>
</Form>
@@ -17,6 +17,7 @@
import { onMount } from 'svelte';
import { page } from '$app/stores';
import Create from './_createAttribute.svelte';
import CreateIndex from './_createIndex.svelte';
import Delete from './_deleteAttribute.svelte';
import Overview from './_overview.svelte';
@@ -27,6 +28,7 @@
let showCreate = false;
let showDelete = false;
let showOverview = false;
let showCreateIndex = false;
onMount(async () => {
await collection.load($page.params.collection);
@@ -74,7 +76,7 @@
<TableCellText title="Default Value">
{attribute.default ? attribute.default : '-'}
</TableCellText>
<TableCellText showOverflow title="">
<TableCell showOverflow>
<DropList
bind:show={showDropdown[index]}
position="bottom"
@@ -96,7 +98,13 @@
selectedAttribute = attribute;
showOverview = true;
}}>Overview</DropListItem>
<DropListItem icon="plus">Create Index</DropListItem>
<DropListItem
icon="plus"
on:click={(e) => {
e.preventDefault();
selectedAttribute = attribute;
showCreateIndex = true;
}}>Create Index</DropListItem>
<DropListItem
icon="trash"
on:click={(e) => {
@@ -106,7 +114,7 @@
}}>Delete</DropListItem>
</svelte:fragment>
</DropList>
</TableCellText>
</TableCell>
</TableRow>
{/each}
</TableBody>
@@ -138,4 +146,5 @@
{#if selectedAttribute}
<Delete bind:showDelete {selectedAttribute} />
<Overview bind:showOverview {selectedAttribute} />
<CreateIndex bind:showCreateIndex externalAttribute={selectedAttribute} />
{/if}
@@ -1,50 +1,186 @@
<script lang="ts">
import { Empty } from '$lib/components';
import { Pill } from '$lib/elements';
import { page } from '$app/stores';
import { Empty, Pagination, Search, DropList, DropListItem } from '$lib/components';
import { Button } from '$lib/elements/forms';
import {
Table,
TableHeader,
TableBody,
TableRow,
TableCellHead,
TableCellText
TableCell,
TableCellText,
TableRow
} from '$lib/elements/table';
// import Create from './_createUser.svelte';
import { Container } from '$lib/layout';
import { collection } from './store';
import { indexList, collection } from './store';
import { onMount } from 'svelte';
import Delete from './_deleteIndex.svelte';
import Create from './_createIndex.svelte';
import type { Models } from '@aw-labs/appwrite-console';
let showDropdown = [];
let selectedIndex: Models.Index = null;
let showCreateIndex = false;
let showOverview = false;
let showDelete = false;
let search = '';
let offset: number = null;
const limit = 5;
const collectionId = $page.params.collection;
const handleDelete = async () => {
await indexList.load(collectionId);
};
// $: indexes.load(search);
// $: if (search) offset = 0;
// $: {
// //TODO: refactor this into something maintainable without the use of goto
// if (offset !== null) {
// $page.url.searchParams.set('offset', offset.toString());
// goto(`?${$page.url.searchParams.toString()}`, { replaceState: true, keepfocus: true });
// }
// }
// onMount(() => {
// const queryOffset = +$page.url.searchParams.get('offset') ?? 0;
// if (offset && offset !== queryOffset) {
// offset = queryOffset;
// }
// });
onMount(async () => {
await indexList.load(collectionId);
});
</script>
<Container>
<h1>Indexes</h1>
{#if $collection.indexes.length}
<Search bind:search placeholder="Search by key or type">
<span>
<Button
disabled={!$collection.attributes?.length}
on:click={() => (showCreateIndex = true)}>
<span class="icon-plus" aria-hidden="true" /> <span class="text">Create index</span>
</Button>
</span>
</Search>
{#if $indexList?.total}
<Table>
<TableHeader>
<TableCellHead>Status</TableCellHead>
<TableCellHead>Key</TableCellHead>
<TableCellHead>Type</TableCellHead>
<TableCellHead>Attributes</TableCellHead>
<TableCellHead />
<TableCellHead>Asc/Desc</TableCellHead>
<TableCellHead width={30} />
</TableHeader>
<TableBody>
{#each $collection.indexes as index}
{#each $indexList.indexes as index, i}
<TableRow>
<TableCellText title="Status">{index.status}</TableCellText>
<TableCellText title="Key">{index.key}</TableCellText>
<TableCellText title="Key">
{index.key}
</TableCellText>
<TableCellText title="Type">{index.type}</TableCellText>
<TableCellText title="Attributes">
{#each index.attributes as attribute, i}
<Pill>{attribute} ({index.orders[i]})</Pill>
{/each}
{index.attributes}
</TableCellText>
<TableCellText title="Action"><Pill danger>Delete</Pill></TableCellText>
<TableCellText title="ASC/DESC">
{index.orders}
</TableCellText>
<TableCell showOverflow>
<DropList
bind:show={showDropdown[i]}
position="bottom"
horizontal="left"
arrow={false}>
<button
class="button is-only-icon is-text"
aria-label="More options"
on:click|preventDefault={() => {
showDropdown[i] = !showDropdown[i];
}}>
<span class="icon-dots-horizontal" aria-hidden="true" />
</button>
<svelte:fragment slot="list">
<DropListItem
icon="eye"
on:click={(e) => {
e.preventDefault();
selectedIndex = index;
showOverview = true;
}}>Overview</DropListItem>
<DropListItem
icon="trash"
on:click={(e) => {
e.preventDefault();
selectedIndex = index;
showDelete = true;
}}>Delete</DropListItem>
</svelte:fragment>
</DropList>
</TableCell>
</TableRow>
{/each}
</TableBody>
</Table>
{:else}
<div class="u-flex u-margin-block-start-32 u-main-space-between">
<p class="text">Total results: {$indexList?.total}</p>
<Pagination {limit} bind:offset sum={$indexList?.total} />
</div>
{:else if search}
<Empty>
<svelte:fragment slot="header">No Indexes Found</svelte:fragment>
Add your first index to get started.
<div class="u-flex u-flex-vertical">
<b>Sorry, we couldn’t find ‘{search}’</b>
<div class="common-section">
<p>There are no indexes that match your search.</p>
</div>
<div class="common-section">
<Button secondary on:click={() => (search = '')}>Clear Search</Button>
</div>
</div>
</Empty>
<div class="u-flex u-margin-block-start-32 u-main-space-between">
<p class="text">Total results: {$indexList?.total}</p>
<Pagination {limit} bind:offset sum={$indexList?.total} />
</div>
{:else if $collection.indexes?.length}
<Empty dashed centered>
<div class="u-flex u-flex-vertical u-cross-center">
<div class="common-section">
<Button secondary round on:click={() => (showCreateIndex = true)}>
<span class="icon-plus" aria-hidden="true" />
</Button>
</div>
<div class="common-section">
<p>Create your first attribute to get started</p>
</div>
<div class="common-section">
<Button secondary href="#">Documentation</Button>
</div>
</div>
</Empty>
{:else}
<Empty dashed centered>
<div class="u-flex u-flex-vertical u-cross-center">
<div class="common-section">
<Button secondary round on:click={() => (showCreateIndex = true)}>
<span class="icon-plus" aria-hidden="true" />
</Button>
</div>
<div class="common-section">
<p>Create your first index to get started</p>
</div>
<div class="common-section">
<Button secondary href="#">Documentation</Button>
</div>
</div>
</Empty>
{/if}
</Container>
<Create bind:showCreateIndex />
{#if selectedIndex}
<Delete bind:showDelete {selectedIndex} on:deleted={handleDelete} />
{/if}
@@ -54,7 +54,7 @@ function createCollection() {
function createDocumentListStore() {
const { subscribe, set } = writable<Models.DocumentList<Models.Document>>(
browser ? JSON.parse(sessionStorage.getItem('documents')) : null
browser ? JSON.parse(sessionStorage.getItem('documentList')) : null
);
return {
subscribe,
@@ -70,11 +70,26 @@ function createDocumentListStore() {
}
};
}
function createIndexListStore() {
const { subscribe, set } = writable<Models.IndexList>(
browser ? JSON.parse(sessionStorage.getItem('indexList')) : null
);
return {
subscribe,
set,
load: async (collectionId: string) => {
const response = await sdkForProject.databases.listIndexes(collectionId);
set(response);
}
};
}
export const collection = createCollection();
export const documentList = createDocumentListStore();
export const indexList = createIndexListStore();
if (browser) {
collection.subscribe((n) => sessionStorage?.setItem('collection', JSON.stringify(n ?? '')));
documentList.subscribe((n) => sessionStorage?.setItem('documentList', JSON.stringify(n ?? '')));
indexList.subscribe((n) => sessionStorage?.setItem('indexList', JSON.stringify(n ?? '')));
}