refactor: relationship and more

This commit is contained in:
Arman committed 2023-04-02 15:33:39 +02:00
1 parent b6a4334c77
commit 54119f4221
5 files changed
+125 -130

No files matched your search

+20 -21
View File
@@ -20,7 +20,7 @@
export let required = false;
export let disabled = false;
export let autofocus = false;
export let customOutput = false;
export let interactiveOutput = false;
// Input value
export let search = '';
// The actual selected value
@@ -79,7 +79,7 @@
function selectOption(option: Option) {
value = option.value;
!customOutput && (search = option.label);
search = option.label;
// It's not working without this line.
element.value = search;
hasFocus = false;
@@ -112,32 +112,31 @@
<div class="custom-select">
<div class="input-text-wrapper" style="--amount-of-buttons:2">
{#if $$slots.default && customOutput && selectedOption}
<output class="input-text is-read-only">
<slot option={selectedOption} />
</output>
{:else}
<input
type="text"
class="input-text"
{placeholder}
{disabled}
{required}
bind:value={search}
bind:this={element}
on:focus={() => (hasFocus = true)}
on:click={() => (hasFocus = true)}
on:input={handleInput}
on:keydown={handleKeydown} />
{#if $$slots.output && selectedOption}
<slot name="output" option={selectedOption} />
{/if}
<input
type="text"
class="input-text"
class:u-hide={$$slots.output && selectedOption}
{placeholder}
{disabled}
{required}
bind:value={search}
bind:this={element}
on:focus={() => (hasFocus = true)}
on:click={() => (hasFocus = true)}
on:input={handleInput}
on:keydown={handleKeydown} />
<div class="options-list">
{#if showClearBtn}
<button
class="options-list-button"
aria-label="clear field"
type="button"
disabled={!!selectedOption && customOutput}
disabled={!interactiveOutput}
on:click|preventDefault={clearOption}>
<span class="icon-x" aria-hidden="true" />
</button>
@@ -145,7 +144,7 @@
<button
class="options-list-button"
type="button"
disabled={!!selectedOption && customOutput}
disabled={!interactiveOutput}
on:click={() => (hasFocus = !hasFocus)}>
<span class="icon-cheveron-down" aria-hidden="true" />
</button>
+3
View File
@@ -5,6 +5,9 @@ export function intersection(arr1: unknown[], arr2: unknown[]) {
}
export function difference(arr1: unknown[], arr2: unknown[]) {
if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
return Array.isArray(arr1) ? arr1 : Array.isArray(arr2) ? arr2 : [];
}
const set = new Set(arr2);
const difference = new Set(arr1.filter((elem) => !set.has(elem)));
return Array.from(difference);
@@ -141,6 +141,7 @@
bind:search
bind:value={data.relatedCollection}
required
interactiveOutput
placeholder="Select a collection"
disabled={editing}
options={collections?.map((n) => ({ value: n.$id, label: n.$id, data: [n.name] })) ?? []}
@@ -149,6 +150,13 @@
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
<svelte:fragment slot="output">
<output class="input-text">
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
</output>
</svelte:fragment>
</InputSelectSearch>
{#if data?.relatedCollection}
@@ -74,7 +74,12 @@
value = singleRel;
}
$: paginatedItems = relatedList.slice(offset, offset + limit);
$: paginatedItems = editing
? relatedList
.slice()
.reverse()
.slice(offset, offset + limit)
: [];
$: total = relatedList?.length;
$: options =
documentList?.documents?.map((n) => {
@@ -87,25 +92,65 @@
}) ?? [];
</script>
{#if editing && isRelationshipToMany(attribute) && total > 0}
<div>
{#if isRelationshipToMany(attribute)}
<div class="u-width-full-line">
<div class="u-flex u-cross-center u-main-space-between">
<div>
<Label required={attribute.required} {optionalText} for={id}>
{label}
</Label>
</div>
<Button
text
noMargin
on:click={() => {
showInput = true;
}}>
<span class="icon-plus" aria-hidden="true" />
<span class="text">Add item</span>
</Button>
{#if editing || total === 0}
<Button
text
noMargin
on:click={() => {
showInput = true;
}}>
<span class="icon-plus" aria-hidden="true" />
<span class="text">Add item</span>
</Button>
{/if}
</div>
<ul class="u-flex-vertical u-gap-4 u-margin-block-start-4">
{#if !editing}
{#each relatedList as item, i}
<li class="u-flex u-gap-16">
<InputSelectSearch
{id}
label="Rel"
showLabel={false}
required
bind:search
bind:value={item}
{options}
let:option={o}>
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
<svelte:fragment slot="output">
<output class="input-text is-read-only">
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
</output>
</svelte:fragment>
</InputSelectSearch>
<Button
text
noMargin
ariaLabel={`Delete item ${i}`}
on:click={() => {
relatedList.splice(i, 1);
relatedList = relatedList;
}}>
<span class="icon-x" aria-hidden="true" />
</Button>
</li>
{/each}
{/if}
{#if showInput}
<li class="u-flex u-gap-16">
<InputSelectSearch
@@ -113,7 +158,6 @@
label="Rel"
showLabel={false}
required
customOutput
placeholder={`Select ${attribute.key}`}
bind:search
bind:value={relatedList[total]}
@@ -125,6 +169,13 @@
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
<svelte:fragment slot="output">
<output class="input-text is-read-only">
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
</output>
</svelte:fragment>
</InputSelectSearch>
<Button
text
@@ -135,15 +186,14 @@
</Button>
</li>
{/if}
{#if paginatedItems}
{#each relatedList as item, i}
{#if paginatedItems && editing}
{#each paginatedItems as item, i}
<li class="u-flex u-gap-16">
<InputSelectSearch
{id}
label="Rel"
showLabel={false}
required
customOutput
bind:search
bind:value={item}
{options}
@@ -151,6 +201,13 @@
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
<svelte:fragment slot="output">
<output class="input-text is-read-only">
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
</output>
</svelte:fragment>
</InputSelectSearch>
<Button
text
@@ -166,94 +223,13 @@
{/each}
{/if}
</ul>
<div class="u-flex u-margin-block-start-32 u-main-space-between">
<p class="text">Total results: {total}</p>
<PaginationInline {limit} bind:offset sum={total} hidePages />
</div>
</div>
{:else if isRelationshipToMany(attribute)}
<div class="u-width-full-line">
<div class="u-flex u-cross-center u-main-space-between">
<div>
<Label required={attribute.required} {optionalText} for={id}>
{label}
</Label>
{#if editing}
<div class="u-flex u-margin-block-start-32 u-main-space-between">
<p class="text">Total results: {total}</p>
<PaginationInline {limit} bind:offset sum={total} hidePages />
</div>
{#if total === 0}
<Button
text
noMargin
on:click={() => {
showInput = true;
}}>
<span class="icon-plus" aria-hidden="true" />
<span class="text">Add item</span>
</Button>
{/if}
</div>
<ul class="u-flex-vertical u-gap-4 u-margin-block-start-4">
{#each relatedList as doc, i}
<li class="u-flex u-gap-16">
<InputSelectSearch
{id}
label="Rel"
showLabel={false}
required
customOutput
bind:search
bind:value={doc}
{options}
let:option={o}>
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
</InputSelectSearch>
<Button
text
noMargin
ariaLabel={`Delete item ${i}`}
on:click={() => {
relatedList.splice(i, 1);
relatedList = relatedList;
}}>
<span class="icon-x" aria-hidden="true" />
</Button>
</li>
{/each}
{#if showInput}
<li class="u-flex u-gap-16">
<InputSelectSearch
{id}
label="Rel"
showLabel={false}
required
customOutput
placeholder={`Select ${attribute.key}`}
bind:search
bind:value={relatedList[total]}
options={options?.filter((n) => !relatedList.includes(n.value))}
on:select={() => {
showInput = false;
}}
let:option={o}>
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
</InputSelectSearch>
<Button
text
noMargin
ariaLabel={`Hide input`}
on:click={() => (showInput = false)}>
<span class="icon-x" aria-hidden="true" />
</Button>
</li>
{/if}
</ul>
{#if total > 0}
{/if}
{#if total > 0 && !editing}
<Button
text
noMargin
@@ -274,6 +250,7 @@
required={attribute.required}
name="documents"
placeholder={`Select ${attribute.key}`}
interactiveOutput
bind:search
bind:value={singleRel}
{options}
@@ -281,5 +258,12 @@
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
<svelte:fragment slot="output">
<output class="input-text">
<SelectSearchItem data={o.data}>
{o.label}
</SelectSearchItem>
</output>
</svelte:fragment>
</InputSelectSearch>
{/if}
@@ -14,7 +14,7 @@
import { Container } from '$lib/layout';
import AttributeItem from '../attributeItem.svelte';
import { difference } from '$lib/helpers/array';
import { isRelationshipToMany } from '../attributes/store';
import { isRelationship, isRelationshipToMany } from '../attributes/store';
const databaseId = $page.params.database;
const collectionId = $page.params.collection;
@@ -80,17 +80,18 @@
return !difference(Array.from(workAttribute), Array.from(docAttribute)).length;
}
if (attribute.type === 'relationship') {
if (isRelationship(attribute)) {
//TODO: remove `side` after SDK update
if (
//TODO: remove `side` after SDK update
isRelationshipToMany(attribute as Models.AttributeRelationship & { side: string })
) {
const relatedIds = docAttribute.map((doc) => doc.$id);
return !difference(workAttribute, relatedIds).length;
} else {
return workAttribute && docAttribute
? !difference(Array.from(workAttribute), Array.from(docAttribute)).length
: true;
}
console.log(workAttribute, docAttribute);
return !difference(Array.from(workAttribute), Array.from(docAttribute)).length;
}
return workAttribute === docAttribute;