fix: edit existing document

This commit is contained in:
Arman committed 2023-03-31 16:38:54 +02:00
1 parent c992498d12
commit 4b34ceebf4
4 files changed
+59 -79

No files matched your search

@@ -0,0 +1,11 @@
import type { Models } from '@appwrite.io/console';
export function isRelationshipToMany(attribute: Models.AttributeRelationship) {
if (!attribute) return false;
const { side, relationType } = attribute || {};
if (side === 'parent') {
return !['oneToOne', 'manyToOne'].includes(relationType);
} else {
return !['oneToOne', 'oneToMany'].includes(relationType);
}
}
@@ -9,6 +9,7 @@
import { Query, type Models } from '@appwrite.io/console';
import { onMount } from 'svelte';
import { doc } from '../store';
import { isRelationshipToMany } from './realtionship';
export let id: string;
export let label: string;
@@ -61,27 +62,25 @@
}
}
function isArrayFunc() {
const { side, relationType } = attribute || {};
if (side === 'parent') {
return !['oneToOne', 'manyToOne'].includes(relationType);
} else {
return !['oneToOne', 'oneToMany'].includes(relationType);
}
}
$: isArray = isArrayFunc();
// Reactive statements
$: getDocuments(search).then((res) => (documentList = res));
$: if (isArray) value = relatedList;
$: if (isRelationshipToMany(attribute)) value = relatedList;
$: paginatedItems = relatedList.slice(offset, offset + limit);
$: total = relatedList?.length;
$: options =
documentList?.documents?.map((n) => {
const data = displayNames.filter((name) => name !== '$id').map((name) => n?.[name]);
return {
value: n.$id,
label: n.$id,
data
};
}) ?? [];
</script>
{#if editing && isArray && total > 0}
{#if editing && isRelationshipToMany(attribute) && total > 0}
<div>
<div class="u-flex u-cross-center u-main-space-between">
<div>
@@ -93,9 +92,7 @@
text
noMargin
on:click={() => {
console.log('test');
showInput = true;
console.log(showInput);
}}>
<span class="icon-plus" aria-hidden="true" />
<span class="text">Add item</span>
@@ -113,18 +110,7 @@
placeholder={`Select ${attribute.key}`}
bind:search
bind:value={relatedList[total]}
options={documentList?.documents
?.filter((n) => !relatedList.includes(n.$id))
.map((n) => {
const data = displayNames
.filter((name) => name !== '$id')
.map((name) => n?.[name]);
return {
value: n.$id,
label: n.$id,
data
};
}) ?? []}
options={options?.filter((n) => !relatedList.includes(n.value))}
on:select={() => {
showInput = false;
}}
@@ -153,16 +139,7 @@
customOutput
bind:search
bind:value={item}
options={documentList?.documents?.map((n) => {
const data = displayNames
.filter((name) => name !== '$id')
.map((name) => n?.[name]);
return {
value: n.$id,
label: n.$id,
data
};
}) ?? []}
{options}
let:option={o}>
<SelectSearchItem data={o.data}>
{o.label}
@@ -187,7 +164,7 @@
<PaginationInline {limit} bind:offset sum={total} hidePages />
</div>
</div>
{:else if isArray}
{:else if isRelationshipToMany(attribute)}
<div class="u-width-full-line">
<div class="u-flex u-cross-center u-main-space-between">
<div>
@@ -219,16 +196,7 @@
customOutput
bind:search
bind:value={doc}
options={documentList?.documents?.map((n) => {
const data = displayNames
.filter((name) => name !== '$id')
.map((name) => n?.[name]);
return {
value: n.$id,
label: n.$id,
data
};
}) ?? []}
{options}
let:option={o}>
<SelectSearchItem data={o.data}>
{o.label}
@@ -258,16 +226,7 @@
placeholder={`Select ${attribute.key}`}
bind:search
bind:value={relatedList[total]}
options={documentList?.documents?.map((n) => {
const data = displayNames
.filter((name) => name !== '$id')
.map((name) => n?.[name]);
return {
value: n.$id,
label: n.$id,
data
};
}) ?? []}
options={options?.filter((n) => !relatedList.includes(n.value))}
on:select={() => {
showInput = false;
}}
@@ -310,14 +269,7 @@
placeholder={`Select ${attribute.key}`}
bind:search
bind:value
options={documentList?.documents?.map((n) => {
const data = displayNames.filter((name) => name !== '$id').map((name) => n?.[name]);
return {
value: n.$id,
label: n.$id,
data
};
}) ?? []}
{options}
let:option={o}>
<SelectSearchItem data={o.data}>
{o.label}
@@ -14,6 +14,7 @@
import { Container } from '$lib/layout';
import AttributeItem from '../attributeItem.svelte';
import { difference } from '$lib/helpers/array';
import { isRelationshipToMany } from '../attributes/realtionship';
const databaseId = $page.params.database;
const collectionId = $page.params.collection;
@@ -63,15 +64,32 @@
}
}
function isButtonDisabled(attribute: Attributes) {
if (attribute?.array)
return !difference(
Array.from($work?.[attribute.key]),
Array.from($doc?.[attribute.key])
).length;
if (attribute?.type === 'relatioship') {
return $work?.[attribute.key] === $doc?.[attribute.key];
} else return $work?.[attribute.key] === $doc?.[attribute.key];
function compareAttributes(
attribute: Attributes,
$work: Models.Document,
$doc: Models.Document
) {
if (!attribute) {
return false;
}
const workAttribute = $work?.[attribute.key];
const docAttribute = $doc?.[attribute.key];
if (attribute.array) {
return !difference(Array.from(workAttribute), Array.from(docAttribute)).length;
}
if (attribute.type === 'relationship') {
if (isRelationshipToMany(attribute as Models.AttributeRelationship)) {
const relatedIds = docAttribute.map((doc) => doc.$id);
return !difference(workAttribute, relatedIds).length;
}
console.log(workAttribute, docAttribute);
return !difference(Array.from(workAttribute), Array.from(docAttribute)).length;
}
return workAttribute === docAttribute;
}
</script>
@@ -90,8 +108,9 @@
</svelte:fragment>
<svelte:fragment slot="actions">
<Button disabled={isButtonDisabled(attribute)} on:click={() => updateData()}
>Update</Button>
<Button
disabled={compareAttributes(attribute, $work, $doc)}
on:click={() => updateData()}>Update</Button>
</svelte:fragment>
</CardGrid>
{/each}
@@ -2,8 +2,6 @@
import { WizardStep } from '$lib/layout';
import AttributeForm from '../document-[document]/attributeForm.svelte';
import { createDocument } from './store';
$: console.log($createDocument);
</script>
<WizardStep>