feat: databases,database, settings screens

This commit is contained in:
Arman committed 2022-07-14 12:50:19 +02:00
1 parent ccc679d0b6
commit 83dcb9b7f9
10 files changed
+458 -96

No files matched your search

@@ -1,25 +1,37 @@
<script>
import { afterNavigate } from '$app/navigation';
import { tabs, title, backButton, copyData } from '$lib/stores/layout';
import { onMount } from 'svelte';
const path = 'databases';
onMount(handle);
afterNavigate(handle);
function handle() {
title.set('Databases');
async function handle() {
title.set('Database');
backButton.set('');
tabs.set([]);
copyData.set({
text: '',
value: ''
});
tabs.set([
{
href: path,
title: 'Databases'
},
{
href: `${path}/usage`,
title: 'Usage'
}
]);
}
</script>
<svelte:head>
<title>Appwrite - Databases</title>
<title>Appwrite - Database</title>
</svelte:head>
<slot />
@@ -1,9 +1,8 @@
<script lang="ts">
import { Modal } from '$lib/components';
import { Button, Form, InputText } from '$lib/elements/forms';
import InputCustomId from '$lib/elements/forms/inputCustomId.svelte';
import { Modal, InnerModal } from '$lib/components';
import { Pill } from '$lib/elements';
import { Button, InputText, Form, FormList } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { sdkForProject, setDatabase } from '$lib/stores/sdk';
import { createEventDispatcher } from 'svelte';
@@ -11,12 +10,13 @@
const dispatch = createEventDispatcher();
let id = '';
let name = '';
let id: string = null;
let showDropdown = false;
const create = async () => {
try {
setDatabase(id);
setDatabase(id ? id : 'unique()');
const database = await sdkForProject.databases.create(name);
name = id = null;
showCreate = false;
@@ -31,13 +31,57 @@
</script>
<Form on:submit={create}>
<Modal bind:show={showCreate}>
<svelte:fragment slot="header">Create Function</svelte:fragment>
<InputCustomId id="id" label="ID" bind:value={id} required />
<InputText id="name" label="Name" bind:value={name} required />
<Modal size="big" bind:show={showCreate}>
<svelte:fragment slot="header">Create Database</svelte:fragment>
<FormList>
<InputText
id="name"
label="Name"
placeholder="Enter database name"
bind:value={name}
autofocus
required />
{#if !showDropdown}
<div>
<Pill button on:click={() => (showDropdown = !showDropdown)}
><span class="icon-pencil" aria-hidden="true" /><span class="text">
Database ID
</span></Pill>
</div>
{:else}
<InnerModal bind:show={showDropdown}>
<svelte:fragment slot="title">Database ID</svelte:fragment>
<p>
Enter a custom database ID. Leave blank for a randomly generated database
ID.
</p>
<svelte:fragment slot="content">
<div class="form">
<InputText
id="id"
label="Custom ID"
showLabel={false}
placeholder="Enter ID"
autofocus={true}
bind:value={id} />
<div class="u-flex u-gap-4 u-margin-block-start-8 u-small">
<span
class="icon-info u-cross-center u-margin-block-start-2 u-line-height-1 u-icon-small"
aria-hidden="true" />
<span class="text u-line-height-1-5"
>Allowed characters: alphanumeric, hyphen, non-leading
underscore, period</span>
</div>
</div>
</svelte:fragment>
</InnerModal>
{/if}
</FormList>
<svelte:fragment slot="footer">
<Button submit>Create</Button>
<Button secondary on:click={() => (showCreate = false)}>Cancel</Button>
<Button submit>Create</Button>
</svelte:fragment>
</Modal>
</Form>
@@ -1,8 +1,7 @@
<script>
import { afterNavigate } from '$app/navigation';
import { base } from '$app/paths';
import { page } from '$app/stores';
import { tabs, title, backButton, copyData } from '$lib/stores/layout';
import { setDatabase } from '$lib/stores/sdk';
import { onMount } from 'svelte';
@@ -18,15 +17,14 @@
if ($database?.$id !== databaseId) {
setDatabase($page.params.database);
await database.load();
title.set(`Database - ${$database.name}`);
title.set($database.name);
} else if ($database) {
title.set(`Database - ${$database.name}`);
title.set($database.name);
}
backButton.set('');
backButton.set(`${base}/console/${$page.params.project}/databases`);
copyData.set({
text: '',
value: ''
text: 'Database ID',
value: databaseId
});
tabs.set([
@@ -37,6 +35,10 @@
{
href: `${path}/usage`,
title: 'Usage'
},
{
href: `${path}/settings`,
title: 'Settings'
}
]);
}
@@ -0,0 +1,39 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { page } from '$app/stores';
import { Modal } from '$lib/components';
import { Button, Form } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { sdkForProject } from '$lib/stores/sdk';
import { database } from './store';
export let showDelete = false;
const deleteElement = async () => {
try {
await sdkForProject.databases.delete();
showDelete = false;
await goto(`${base}/console/${$page.params.project}/databases`);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
}
};
</script>
<Form on:submit={deleteElement}>
<Modal warning={true} bind:show={showDelete}>
<svelte:fragment slot="header">Delete Database</svelte:fragment>
<p>
Are you sure you want to delete <b>{$database.name}</b>?
</p>
<svelte:fragment slot="footer">
<Button text on:click={() => (showDelete = false)}>Cancel</Button>
<Button secondary submit>Delete</Button>
</svelte:fragment>
</Modal>
</Form>
@@ -1,26 +1,30 @@
<script lang="ts">
import { page } from '$app/stores';
import { sdkForProject } from '$lib/stores/sdk';
import { Button } from '$lib/elements/forms';
import { Empty, Pagination, Tile, Tiles } from '$lib/components';
import Create from './_create.svelte';
import { goto } from '$app/navigation';
import { Button } from '$lib/elements/forms';
import { Empty, Pagination, Copy, Bucket } from '$lib/components';
import { Pill } from '$lib/elements';
import type { Models } from '@aw-labs/appwrite-console';
import Create from './_create.svelte';
import { Container } from '$lib/layout';
import { base } from '$app/paths';
import { collections } from './store';
let search = '';
let showCreate = false;
let search = '';
let offset = 0;
const limit = 25;
const projectId = $page.params.project;
const limit = 5;
const project = $page.params.project;
const databaseId = $page.params.database;
const collectionCreated = async (event: CustomEvent<Models.Collection>) => {
await goto(`${base}/console/${projectId}/databases/collection/${event.detail.$id}`);
showCreate = false;
await goto(
`${base}/console/${project}/databases/database/${databaseId}/collection/${event.detail.$id}`
);
};
$: request = sdkForProject.databases.listCollections(search, limit, offset);
$: collections.load(search, limit, offset ?? 0);
$: if (search) offset = 0;
</script>
@@ -30,31 +34,78 @@
<Button on:click={() => (showCreate = true)}>
<span class="icon-plus" aria-hidden="true" />
<span class="text">Create Collection</span>
<span class="text">Create collection</span>
</Button>
</div>
{#await request}
<div aria-busy="true" />
{:then response}
{#if response.total}
<Tiles>
{#each response.collections as collection}
<Tile
href={`${base}/console/${projectId}/databases/database/${databaseId}/collection/${collection.$id}`}
title={collection.name} />
{/each}
</Tiles>
<div class="u-flex common-section u-main-space-between">
<p class="text">Total results: {response.total}</p>
<Pagination {limit} bind:offset sum={response.total} />
{#if $collections?.total}
<div class="grid-box common-section" style="--grid-gap:2rem; --grid-item-size:25rem;">
{#each $collections.collections as collection}
<Bucket
href={`${base}/console/${project}/databases/database/${databaseId}/collection/${collection.$id}`}>
<svelte:fragment slot="eyebrow">XX Documents</svelte:fragment>
<svelte:fragment slot="title">{collection.name}</svelte:fragment>
<Copy value={collection.$id}>
<Pill button><i class="icon-duplicate" />Collection ID</Pill>
</Copy>
</Bucket>
{/each}
{#if $collections.total % 2 !== 0}
<article class="card is-border-dashed">
<div class="bucket">
<div class="u-flex u-flex-vertical u-cross-center">
<div class="common-section">
<Button secondary round on:click={() => (showCreate = true)}>
<i class="icon-plus" />
</Button>
</div>
<div class="common-section">
<p>Create a new collection</p>
</div>
</div>
</div>
</article>
{/if}
</div>
<div class="u-flex u-margin-block-start-32 u-main-space-between">
<p class="text">Total results: {$collections.total}</p>
<Pagination {limit} bind:offset sum={$collections.total} />
</div>
{:else if search}
<Empty>
<div class="u-flex u-flex-vertical">
<b>Sorry, we couldn’t find ‘{search}’</b>
<div class="common-section">
<p>There are no collections that match your search.</p>
</div>
<div class="common-section">
<Button secondary on:click={() => (search = '')}>Clear Search</Button>
</div>
</div>
{:else if search}
<Empty>
No results found for <b>{search}</b>
</Empty>
{:else}
<Empty>You haven't created any collections for your project yet.</Empty>
{/if}
{/await}
</Empty>
<div class="u-flex u-margin-block-start-32 u-main-space-between">
<p class="text">Total results: {$collections?.total}</p>
<Pagination {limit} bind:offset sum={$collections?.total} />
</div>
{:else}
<Empty dashed centered>
<div class="u-flex u-flex-vertical u-cross-center">
<div class="common-section">
<Button secondary round on:click={() => (showCreate = true)}>
<i class="icon-plus" />
</Button>
</div>
<div class="common-section">
<p>Create your first collection to get started</p>
</div>
<div class="common-section">
<Button secondary href="#">Documentation</Button>
</div>
</div>
</Empty>
{/if}
</Container>
<Create bind:showCreate on:created={collectionCreated} />
@@ -0,0 +1,115 @@
<script lang="ts">
import { page } from '$app/stores';
import { Card, Avatar, CardGrid, Box } from '$lib/components';
import { Container } from '$lib/layout';
import { Button, InputText, Helper } from '$lib/elements/forms';
import { sdkForProject } from '$lib/stores/sdk';
import { addNotification } from '$lib/stores/notifications';
import { database } from './store';
import Delete from './_delete.svelte';
import { onMount } from 'svelte';
const getAvatar = (name: string) => sdkForProject.avatars.getInitials(name, 48, 48).toString();
let showDelete = false;
let showError: false | 'name' | 'email' | 'password' = false;
let errorMessage = 'Something went wrong';
let errorType: 'error' | 'warning' | 'success' = 'error';
let databaseName: string = null;
onMount(async () => {
await database.load();
databaseName ??= $database.name;
});
function addError(location: typeof showError, message: string, type: typeof errorType) {
showError = location;
errorMessage = message;
errorType = type;
}
async function updateName() {
try {
await sdkForProject.teams.update($page.params.team, databaseName);
$database.name = databaseName;
addNotification({
message: 'Name has been updated',
type: 'success'
});
} catch (error) {
addError('name', error.message, 'error');
}
}
</script>
{#if $database}
<Container>
<Card>
<div class="common-section grid-1-2">
<div class="grid-1-2-col-1">
<div class="grid-1-2-col-1 u-flex u-cross-center u-gap-16">
<Avatar size={48} name={$database.name} src={getAvatar($database.name)} />
<h6 class="heading-level-7">{$database.name}</h6>
</div>
</div>
<div class="grid-1-2-col-2">
<p>Created: TO IMPLEMENT</p>
<p>Last Updated: TO IMPLEMENT</p>
</div>
</div>
</Card>
<CardGrid>
<h6 class="heading-level-7">Update Name</h6>
<svelte:fragment slot="aside">
<ul>
<InputText
id="name"
label="Name"
placeholder="Enter database name"
autocomplete={false}
bind:value={databaseName} />
{#if showError === 'name'}
<Helper type={errorType}>{errorMessage}</Helper>
{/if}
</ul>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button
disabled={databaseName === $database.name || !databaseName}
on:click={() => {
updateName();
}}>Update</Button>
</svelte:fragment>
</CardGrid>
<CardGrid>
<div>
<h6 class="heading-level-7">Danger Zone</h6>
</div>
<p>
The database will be permanently deleted, including all data associated with this
team. This action is irreversible.
</p>
<svelte:fragment slot="aside">
<Box>
<svelte:fragment slot="image">
<Avatar size={48} name={$database.name} src={getAvatar($database.name)} />
</svelte:fragment>
<svelte:fragment slot="title">
<h6 class="u-bold">{$database.name}</h6>
<span>Last Updated: TO IMPLEMENT</span>
</svelte:fragment>
</Box>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button secondary on:click={() => (showDelete = true)}>Delete</Button>
</svelte:fragment>
</CardGrid>
</Container>
<Delete bind:showDelete />
{/if}
@@ -1,6 +1,21 @@
import { sdkForProject } from '$lib/stores/sdk';
import { writable } from 'svelte/store';
import type { Models } from '@aw-labs/appwrite-console';
import { writable } from 'svelte/store';
import { browser } from '$app/env';
function createCollectionStore() {
const { subscribe, set } = writable<Models.CollectionList>(
browser ? JSON.parse(sessionStorage.getItem('database')) : null
);
return {
subscribe,
set,
load: async (search: string, limit: number, offset: number) => {
const response = await sdkForProject.databases.listCollections(search, limit, offset);
set(response);
}
};
}
function createDatabase() {
const { subscribe, set } = writable<Models.Database>();
@@ -13,5 +28,10 @@ function createDatabase() {
}
};
}
export const collections = createCollectionStore();
export const database = createDatabase();
if (browser) {
collections.subscribe((n) => sessionStorage?.setItem('collections', JSON.stringify(n ?? '')));
database.subscribe((n) => sessionStorage?.setItem('database', JSON.stringify(n ?? '')));
}
@@ -1,62 +1,106 @@
<script lang="ts">
import { page } from '$app/stores';
import { sdkForProject } from '$lib/stores/sdk';
import { Button, InputSearch } from '$lib/elements/forms';
import { Card, Empty, Pagination, Tile, Tiles } from '$lib/components';
import { goto } from '$app/navigation';
import { Button } from '$lib/elements/forms';
import { Empty, Pagination, Copy, Bucket } from '$lib/components';
import { Pill } from '$lib/elements';
import type { Models } from '@aw-labs/appwrite-console';
import Create from './_create.svelte';
import { Container } from '$lib/layout';
import { base } from '$app/paths';
import type { Models } from '@aw-labs/appwrite-console';
import { goto } from '$app/navigation';
import { databaseList } from './store';
let search = '';
let showCreate = false;
let search = '';
let offset = 0;
const limit = 25;
const limit = 5;
const project = $page.params.project;
const databaseCreated = async (event: CustomEvent<Models.Database>) => {
showCreate = false;
await goto(`${base}/console/${project}/databases/database/${event.detail.$id}`);
};
$: request = sdkForProject.databases.list(search, limit, offset);
$: databaseList.load(search, limit, offset ?? 0);
$: if (search) offset = 0;
</script>
<Container>
<h1>Databases</h1>
<Card>
<InputSearch bind:value={search} />
</Card>
<div class="u-flex u-gap-12 common-section u-main-space-between">
<h2 class="heading-level-5">Databases</h2>
{#await request}
<div aria-busy="true" />
{:then response}
{#if response.total}
<p>{response.total} databases found</p>
<Tiles>
{#each response.databases as database}
<Tile
href={`${base}/console/${project}/databases/database/${database.$id}`}
title={database.name} />
{/each}
</Tiles>
<Button on:click={() => (showCreate = true)}>
<span class="icon-plus" aria-hidden="true" /> <span class="text">Create database</span>
</Button>
</div>
<Pagination {limit} bind:offset sum={response.total} />
{:else if search}
<Empty>
<svelte:fragment slot="header">
No results found for <b>{search}</b>
</svelte:fragment>
</Empty>
{:else}
<Empty>
<svelte:fragment slot="header">No Databases Found</svelte:fragment>
You haven't created any database for your project yet.
</Empty>
{/if}
{/await}
{#if $databaseList?.total}
<div class="grid-box common-section" style="--grid-gap:2rem; --grid-item-size:25rem;">
{#each $databaseList.databases as database}
<Bucket href={`${base}/console/${project}/databases/database/${database.$id}`}>
<svelte:fragment slot="eyebrow">XX Collections</svelte:fragment>
<svelte:fragment slot="title">{database.name}</svelte:fragment>
<Button on:click={() => (showCreate = true)}>Create Database</Button>
<Copy value={database.$id}>
<Pill button><i class="icon-duplicate" />Database ID</Pill>
</Copy>
</Bucket>
{/each}
{#if $databaseList.total % 2 !== 0}
<article class="card is-border-dashed">
<div class="bucket">
<div class="u-flex u-flex-vertical u-cross-center">
<div class="common-section">
<Button secondary round on:click={() => (showCreate = true)}>
<i class="icon-plus" />
</Button>
</div>
<div class="common-section">
<p>Create a new database</p>
</div>
</div>
</div>
</article>
{/if}
</div>
<div class="u-flex u-margin-block-start-32 u-main-space-between">
<p class="text">Total results: {$databaseList.total}</p>
<Pagination {limit} bind:offset sum={$databaseList.total} />
</div>
{:else if search}
<Empty>
<div class="u-flex u-flex-vertical">
<b>Sorry, we couldn’t find ‘{search}’</b>
<div class="common-section">
<p>There are no databases 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: {$databaseList?.total}</p>
<Pagination {limit} bind:offset sum={$databaseList?.total} />
</div>
{:else}
<Empty dashed centered>
<div class="u-flex u-flex-vertical u-cross-center">
<div class="common-section">
<Button secondary round on:click={() => (showCreate = true)}>
<i class="icon-plus" />
</Button>
</div>
<div class="common-section">
<p>Create your first Database to get started</p>
</div>
<div class="common-section">
<Button secondary href="#">Documentation</Button>
</div>
</div>
</Empty>
{/if}
</Container>
<Create bind:showCreate on:created={databaseCreated} />
@@ -0,0 +1,25 @@
import { sdkForProject } from '$lib/stores/sdk';
import type { Models } from '@aw-labs/appwrite-console';
import { writable } from 'svelte/store';
import { browser } from '$app/env';
function createDatabaseListStore() {
const { subscribe, set } = writable<Models.DatabaseList>(
browser ? JSON.parse(sessionStorage.getItem('databaseList')) : null
);
return {
subscribe,
set,
load: async (search: string, limit: number, offset: number) => {
const response = await sdkForProject.databases.list(search, limit, offset);
set(response);
}
};
}
export const databaseList = createDatabaseListStore();
if (browser) {
databaseList.subscribe((n) => sessionStorage?.setItem('databaseList', JSON.stringify(n ?? '')));
}
@@ -0,0 +1,10 @@
<script>
import { Card } from '$lib/components';
import { Container } from '$lib/layout';
</script>
<Container>
<h1>Usage</h1>
<Card />
</Container>