refactor: team preferences

This commit is contained in:
Arman committed 2023-04-11 16:03:03 +02:00
1 parent 23433127f5
commit 4bb7332a8b
7 files changed
+81 -70

No files matched your search

+47 -1
View File
@@ -4,6 +4,8 @@ import type { View } from '$lib/helpers/load';
import type { Page } from '@sveltejs/kit';
import { get, writable } from 'svelte/store';
import { sdk } from './sdk';
import type { Models } from '@appwrite.io/console';
import { organization } from './organization';
type Preferences = {
limit?: number;
@@ -11,12 +13,19 @@ type Preferences = {
columns?: string[];
};
type TeamPreferences = {
names?: string[];
};
type PreferencesStore = {
[key: string]: {
[key: string]: Preferences;
collections?: {
[key: string]: Preferences['columns'];
};
displayNames?: {
[key: string]: TeamPreferences['names'];
};
};
};
@@ -39,6 +48,7 @@ function createPreferences() {
}
);
},
getCustomCollectionColumns: (collectionId: string): Preferences['columns'] => {
let preferences: PreferencesStore;
subscribe((n) => (preferences = n))();
@@ -100,7 +110,43 @@ function createPreferences() {
n[project].collections[collection] = columns;
return n;
})
}),
loadTeamPrefs: async () => {
const id = get(organization)?.$id;
if (!id) return {};
const teamPrefs = await sdk.forConsole.teams.getPrefs(id);
update((n) => {
n[id] = teamPrefs;
return n;
});
return teamPrefs;
},
getDisplayNames: () => {
const id = get(organization)?.$id;
if (!id) return {};
let preferences: PreferencesStore;
subscribe((n) => (preferences = n))();
return preferences?.[id]?.displayNames ?? {};
},
setDisplayNames: async (collectionId: string, names: TeamPreferences['names']) => {
const id = get(organization).$id;
let teamPrefs: Models.Preferences;
update((n) => {
if (!n[id]?.displayNames) {
n[id] ??= {};
n[id].displayNames ??= {};
}
teamPrefs = n[id];
n[id].displayNames[collectionId] = names;
return n;
});
await sdk.forConsole.teams.updatePrefs(id, teamPrefs);
}
};
}
-20
View File
@@ -1,20 +0,0 @@
import { cachedStore } from '$lib/helpers/cache';
import type { Models } from '@appwrite.io/console';
import { sdk } from './sdk';
export const teamPrefs = cachedStore<
Models.Preferences,
{
load: (id: string) => Promise<void>;
updatePrefs: (id: string, prefs: Models.Preferences) => Promise<void>;
}
>('teamPrefs', (store) => ({
load: async (id: string) => {
const prefs = await sdk.forConsole.teams.getPrefs(id);
store.set(prefs);
},
updatePrefs: async (id: string, prefs: Models.Preferences) => {
await sdk.forConsole.teams.updatePrefs(id, prefs);
store.set(prefs);
}
}));
@@ -6,8 +6,10 @@ import Header from './header.svelte';
import { error } from '@sveltejs/kit';
import SubNavigation from './subNavigation.svelte';
import { Query } from '@appwrite.io/console';
import { preferences } from '$lib/stores/preferences';
export const load: LayoutLoad = async ({ params, depends }) => {
await preferences.loadTeamPrefs();
depends(Dependencies.COLLECTION);
try {
return {
@@ -3,13 +3,12 @@
import { PaginationInline } from '$lib/components';
import { SelectSearchItem } from '$lib/elements';
import { Button, InputSelectSearch, Label } from '$lib/elements/forms';
import { organization } from '$lib/stores/organization';
import { sdk } from '$lib/stores/sdk';
import { teamPrefs } from '$lib/stores/team';
import { Query, type Models } from '@appwrite.io/console';
import { onMount } from 'svelte';
import { doc } from '../store';
import { isRelationshipToMany } from './store';
import { preferences } from '$lib/stores/preferences';
export let id: string;
export let label: string;
@@ -31,7 +30,6 @@
let offset = 0;
onMount(async () => {
await teamPrefs.load($organization.$id);
documentList = await getDocuments();
if (editing && $doc?.[attribute.key]) {
@@ -45,7 +43,7 @@
}
}
displayNames = $teamPrefs?.displayNames?.[attribute?.relatedCollection] ?? ['$id'];
displayNames = preferences.getDisplayNames()?.[attribute?.relatedCollection] ?? ['$id'];
if (!displayNames?.includes('$id')) {
displayNames.unshift('$id');
}
@@ -2,7 +2,7 @@
import { base } from '$app/paths';
import { page } from '$app/stores';
import { ClickableList, ClickableListItem, Modal, Paginator } from '$lib/components';
import { teamPrefs } from '$lib/stores/team';
import { preferences } from '$lib/stores/preferences';
import type { Models } from '@appwrite.io/console';
export let show = false;
@@ -12,7 +12,7 @@
const databaseId = $page.params.database;
const limit = 10;
$: args = $teamPrefs?.displayNames?.[selectedRelationship?.relatedCollection];
$: args = preferences.getDisplayNames()?.[selectedRelationship?.relatedCollection] ?? [];
$: if (!show) {
data = null;
@@ -4,35 +4,26 @@
import { CardGrid, Heading } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Button, Form, InputSelectSearch, InputText } from '$lib/elements/forms';
import { symmetricDifference } from '$lib/helpers/array';
import { last, symmetricDifference } from '$lib/helpers/array';
import { addNotification } from '$lib/stores/notifications';
import { organization } from '$lib/stores/organization';
import { teamPrefs } from '$lib/stores/team';
import type { Models } from '@appwrite.io/console';
import { onMount } from 'svelte';
import { attributes, collection } from '../store';
import { attributes } from '../store';
import { preferences } from '$lib/stores/preferences';
import { page } from '$app/stores';
let displayNames = [];
const collectionId = $page.params.collection;
let names: string[] = [];
let search: string;
onMount(async () => {
await teamPrefs.load($organization.$id);
$teamPrefs.displayNames ??= {};
$teamPrefs.displayNames[$collection.$id] ??= [];
displayNames = [...$teamPrefs.displayNames[$collection.$id]];
names = [...(preferences.getDisplayNames()?.[collectionId] ?? [])];
});
async function updateDisplayName() {
try {
const pref = {
...$teamPrefs,
displayNames: {
...$teamPrefs.displayNames,
[$collection.$id]: displayNames
}
};
await teamPrefs.updatePrefs($organization.$id, pref);
await preferences.setDisplayNames(collectionId, names);
names = [...(preferences.getDisplayNames()?.[collectionId] ?? [])];
await invalidate(Dependencies.TEAM);
addNotification({
message: 'Display names has been updated',
@@ -54,7 +45,7 @@
attr.type === 'string' &&
attr?.size <= 50 &&
!attr?.array &&
!displayNames?.some((name) => name === attr.key)
!names?.some((name) => name === attr.key)
)
.map((attr) => {
return {
@@ -63,14 +54,11 @@
};
});
$: addAttributeDisabled =
displayNames?.length >= 5 ||
(displayNames?.length && !displayNames[displayNames?.length - 1]);
$: addAttributeDisabled = names?.length >= 5 || (names?.length && !names[names?.length - 1]);
$: updateBtnDisabled = !symmetricDifference(
displayNames,
$teamPrefs?.displayNames?.[$collection.$id] ?? []
)?.length;
$: updateBtnDisabled =
!symmetricDifference(names, preferences.getDisplayNames()?.[collectionId] ?? [])?.length ||
!last(names);
</script>
<Form onSubmit={updateDisplayName}>
@@ -90,18 +78,18 @@
showLabel={false}
placeholder="Document ID"
readonly />
{#if displayNames?.length}
{#each displayNames as name, i}
{#if names?.length}
{#each names as name, i}
<div class="u-flex u-gap-8">
{#if displayNames[i]}
{#if names[i]}
<InputSelectSearch
id={name}
label={name}
showLabel={false}
interactiveOutput
placeholder="Select attribute"
bind:value={displayNames[i]}
bind:search={displayNames[i]}
bind:value={names[i]}
bind:search={names[i]}
name="attributes"
disabled
{options} />
@@ -111,7 +99,7 @@
label={name}
showLabel={false}
placeholder="Select attribute"
bind:value={displayNames[i]}
bind:value={names[i]}
bind:search
name="attributes"
{options} />
@@ -121,8 +109,8 @@
text
noMargin
on:click={() => {
displayNames.splice(i, 1);
displayNames = displayNames;
names.splice(i, 1);
names = names;
}}>
<span class="icon-x" aria-hidden="true" />
</Button>
@@ -136,9 +124,9 @@
text
disabled={addAttributeDisabled}
on:click={() => {
displayNames[displayNames.length] = null;
names[names.length] = null;
search = null;
displayNames = displayNames;
names = names;
}}>
<span class="icon-plus" aria-hidden="true" />
<span class="text">Add attribute</span>
@@ -13,9 +13,7 @@
TableRowLink,
TableScroll
} from '$lib/elements/table';
import { organization } from '$lib/stores/organization';
import { preferences } from '$lib/stores/preferences';
import { teamPrefs } from '$lib/stores/team';
import type { Models } from '@appwrite.io/console';
import { onMount } from 'svelte';
import type { PageData } from './$types';
@@ -30,9 +28,10 @@
let showRelationships = false;
let selectedRelationship: Models.AttributeRelationship = null;
let relationshipData: [];
let displayNames = {};
onMount(() => {
teamPrefs.load($organization.$id);
onMount(async () => {
displayNames = preferences.getDisplayNames();
});
function formatArray(array: unknown[]) {
@@ -111,9 +110,7 @@
{#if column.show}
{@const attr = $attributes.find((n) => n.key === column.id)}
{#if isRelationship(attr)}
{@const args = $teamPrefs?.displayNames?.[attr.relatedCollection] ?? [
'$id'
]}
{@const args = displayNames?.[attr.relatedCollection] ?? ['$id']}
<TableCell title={column.title}>
{#if !isRelationshipToMany(attr)}
{#if document[column.id]}