diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/realtionship.ts b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/realtionship.ts new file mode 100644 index 000000000..8edc0b3ad --- /dev/null +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/realtionship.ts @@ -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); + } +} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/relationship.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/relationship.svelte index 2af53d8f6..7d8196e70 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/relationship.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/relationship.svelte @@ -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 + }; + }) ?? []; -{#if editing && isArray && total > 0} +{#if editing && isRelationshipToMany(attribute) && total > 0}
@@ -93,9 +92,7 @@ text noMargin on:click={() => { - console.log('test'); showInput = true; - console.log(showInput); }}>
-{:else if isArray} +{:else if isRelationshipToMany(attribute)}
@@ -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}> {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}> {o.label} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/data/+page.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/data/+page.svelte index c3ca75532..fe0abcf7b 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/data/+page.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/data/+page.svelte @@ -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; } @@ -90,8 +108,9 @@ - + {/each} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/step1.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/step1.svelte index 0661045e2..fc7bb5c78 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/step1.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/step1.svelte @@ -2,8 +2,6 @@ import { WizardStep } from '$lib/layout'; import AttributeForm from '../document-[document]/attributeForm.svelte'; import { createDocument } from './store'; - - $: console.log($createDocument);