feat: document view

This commit is contained in:
Arman committed 2022-07-26 17:59:27 +02:00
1 parent f6cfb6e76c
commit 82fcce06f5
4 files changed
+203 -63

No files matched your search

@@ -0,0 +1,44 @@
<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 { collection } from '../../store';
export let showDelete = false;
const deleteElement = async () => {
try {
await sdkForProject.databases.deleteDocument(
$page.params.collection,
$page.params.document
);
showDelete = false;
await goto(
`${base}/console/${$page.params.project}/databases/database/${$page.params.database}/collection/${$page.params.collection}`
);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
}
};
</script>
<Form on:submit={deleteElement}>
<Modal warning={true} bind:show={showDelete}>
<svelte:fragment slot="header">Delete Document</svelte:fragment>
<p>
Are you sure you want to delete <b>the document 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>
@@ -1,44 +1,57 @@
<script lang="ts">
import { Collapsible } from '$lib/components';
import { Button, InputTags } from '$lib/elements/forms';
import { Button, FormItem } from '$lib/elements/forms';
import { CardGrid } from '$lib/components';
import { collection } from '../../store';
import { doc } from './store';
import Attribute from './_attribute.svelte';
</script>
{#each $collection.attributes.filter((a) => a.status === 'available') as attribute}
{#if attribute.array}
{#each $doc[attribute.key] as _v, index}
<Attribute
{attribute}
id={`${attribute.key}-${index}`}
label={index === 0 ? attribute.key : ''}
bind:value={$doc[attribute.key][index]} />
<Button on:click={() => doc.removeAttribute(attribute.key, index)}>X</Button>
{:else}
<p>{attribute.key}</p>
{/each}
<Button on:click={() => doc.addAttribute(attribute.key)}>Add Attribute</Button>
{:else}
<Attribute
{attribute}
id={attribute.key}
label={attribute.key}
bind:value={$doc[attribute.key]} />
{/if}
{/each}
<Collapsible>
<svelte:fragment slot="header">Permissions</svelte:fragment>
<InputTags
id="read"
label="Read Permissions"
bind:tags={$doc.$read}
helper="Add 'role:all' for wildcard access"
placeholder="User ID, Team ID or Role" />
<InputTags
id="write"
label="Write Permissions"
bind:tags={$doc.$write}
helper="Add 'role:all' for wildcard access"
placeholder="User ID, Team ID or Role" />
</Collapsible>
<CardGrid>
<h6 class="heading-level-7">Update Data</h6>
<p>Update document data based on the attributes created earlier.</p>
<svelte:fragment slot="aside">
<ul class="common-section form-list">
{#each $collection.attributes.filter((a) => a.status === 'available') as attribute}
<FormItem>
{#if attribute.array}
{#each $doc[attribute.key] as _v, index}
<li class="form-item is-multiple">
<div class="form-item-part u-stretch">
<Attribute
{attribute}
id={`${attribute.key}-${index}`}
label={index === 0 ? attribute.key : ''}
bind:value={$doc[attribute.key][index]} />
</div>
<div class="form-item-part u-cross-child-end">
<Button
text
on:click={() => doc.removeAttribute(attribute.key, index)}>
<span class="icon-x" aria-hidden="true" />
</Button>
</div>
</li>
{:else}
<p>{attribute.key}</p>
{/each}
<Button text on:click={() => doc.addAttribute(attribute.key)}>
<span class="icon-plus" aria-hidden="true" />
<span class="text"> Add Attribute</span>
</Button>
{:else}
<Attribute
{attribute}
id={attribute.key}
label={attribute.key}
bind:value={$doc[attribute.key]} />
{/if}
</FormItem>
{/each}
</ul>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button disabled={true} on:click={() => {}}>Update</Button>
</svelte:fragment>
</CardGrid>
@@ -1,5 +1,5 @@
<script lang="ts">
import { InputSwitch } from '$lib/elements/forms';
import { InputCheckbox } from '$lib/elements/forms';
import type { Models } from '@aw-labs/appwrite-console';
export let id: string;
@@ -8,4 +8,4 @@
export let attribute: Models.AttributeInteger;
</script>
<InputSwitch {id} {label} required={attribute.required} bind:value />
<InputCheckbox {id} {label} required={attribute.required} bind:value />
@@ -1,26 +1,34 @@
<script lang="ts">
import { page } from '$app/stores';
import { Card, CopyInput } from '$lib/components';
import { Button, Form } from '$lib/elements/forms';
import { CardGrid, Box, Alert } from '$lib/components';
import { Container } from '$lib/layout';
import { Button, InputTags } from '$lib/elements/forms';
import { sdkForProject } from '$lib/stores/sdk';
import { collection } from '../../store';
import { doc } from './store';
import { addNotification } from '$lib/stores/notifications';
import { toLocaleDateTime } from '$lib/helpers/date';
import Document from './_document.svelte';
import Delete from './_delete.svelte';
const documentId = $page.params.document;
let showDelete = false;
let fileRead = $doc?.$read;
let fileWrite = $doc?.$write;
let arePermsDisabled = true;
const updateDocument = async () => {
async function updatePermissions() {
try {
$doc = await sdkForProject.databases.updateDocument(
$collection.$id,
documentId,
$doc,
$doc.$read,
$doc.$write
await sdkForProject.databases.updateDocument(
$doc.collectionId,
$doc.$id,
$doc.data,
fileRead,
fileWrite
);
$doc.$read = fileRead;
$doc.$write = fileWrite;
arePermsDisabled = true;
addNotification({
message: 'Document was updated!',
message: 'Permissions have been updated',
type: 'success'
});
} catch (error) {
@@ -29,22 +37,97 @@
type: 'error'
});
}
};
}
$: if (fileRead || fileWrite) {
if (fileRead) {
if (JSON.stringify(fileRead) !== JSON.stringify($doc.$read)) {
arePermsDisabled = false;
} else arePermsDisabled = true;
} else if (fileWrite) {
if (JSON.stringify(fileWrite) !== JSON.stringify($doc.$write)) {
arePermsDisabled = false;
} else arePermsDisabled = true;
}
}
</script>
<svelte:head>
<title>Appwrite - Document</title>
</svelte:head>
<h1>Overview</h1>
<Card>
<Container>
{#if $doc}
<Form on:submit={updateDocument}>
<CopyInput value={$doc.$id} />
<Document />
<Button submit>Update</Button>
</Form>
{:else}
loading
<CardGrid>
<h2 class="heading-level-7">Document</h2>
<svelte:fragment slot="aside">
<div>
<p>Created: {toLocaleDateTime($doc.$createdAt)}</p>
<p>Last Updated: {toLocaleDateTime($doc.$updatedAt)}</p>
</div>
</svelte:fragment>
</CardGrid>
<CardGrid>
<h6 class="heading-level-7">Update Permissions</h6>
<p>
Assign read or write permissions at the <b> Collection Level</b> or
<b> Document Level</b>. If collection Level permissions are assigned, permissions
applied to individual documents are ignored.
</p>
<svelte:fragment slot="aside">
<Alert type="info">
<p>
Tip: Add role:all for wildcard access. Check out our documentation for more
on <a href="#?"> Permissions</a>
</p>
</Alert>
<ul class="common-section">
<InputTags
id="read"
label="Read Access"
placeholder="User ID, Team ID, or Role"
bind:tags={fileRead} />
<InputTags
id="write"
label="Write Access"
placeholder="User ID, Team ID, or Role"
bind:tags={fileWrite} />
</ul>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button
disabled={arePermsDisabled}
on:click={() => {
updatePermissions();
}}>Update</Button>
</svelte:fragment>
</CardGrid>
<Document />
<CardGrid>
<h6 class="heading-level-7">Delete document</h6>
<p>
The document will be permanently deleted, including all the data within it. This
action is irreversible.
</p>
<svelte:fragment slot="aside">
<Box>
<svelte:fragment slot="title">
<h6 class="u-bold">{$doc.$id}</h6>
</svelte:fragment>
<p>
Last Updated: {toLocaleDateTime($doc.$updatedAt)}
</p>
</Box>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button secondary on:click={() => (showDelete = true)}>Delete</Button>
</svelte:fragment>
</CardGrid>
{/if}
</Card>
</Container>
<Delete bind:showDelete />