From 4bb7332a8bd45a38e14e4f3a72052e66f556fc1e Mon Sep 17 00:00:00 2001 From: Arman Date: Tue, 11 Apr 2023 16:03:03 +0200 Subject: [PATCH] refactor: team preferences --- src/lib/stores/preferences.ts | 48 ++++++++++++++- src/lib/stores/team.ts | 20 ------- .../collection-[collection]/+layout.ts | 2 + .../attributes/relationship.svelte | 6 +- .../relationshipsModal.svelte | 4 +- .../settings/displayName.svelte | 60 ++++++++----------- .../collection-[collection]/table.svelte | 11 ++-- 7 files changed, 81 insertions(+), 70 deletions(-) delete mode 100644 src/lib/stores/team.ts diff --git a/src/lib/stores/preferences.ts b/src/lib/stores/preferences.ts index f1184c7e2..1c5b21c56 100644 --- a/src/lib/stores/preferences.ts +++ b/src/lib/stores/preferences.ts @@ -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); + } }; } diff --git a/src/lib/stores/team.ts b/src/lib/stores/team.ts deleted file mode 100644 index cc1a89e60..000000000 --- a/src/lib/stores/team.ts +++ /dev/null @@ -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; - updatePrefs: (id: string, prefs: Models.Preferences) => Promise; - } ->('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); - } -})); diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/+layout.ts b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/+layout.ts index 4f8cfc2ed..943014419 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/+layout.ts +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/+layout.ts @@ -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 { 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 71383dee5..8ff8e3867 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 @@ -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'); } diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/relationshipsModal.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/relationshipsModal.svelte index 145286d24..f2576dd6a 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/relationshipsModal.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/relationshipsModal.svelte @@ -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; diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/settings/displayName.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/settings/displayName.svelte index 47fd999ec..d10455874 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/settings/displayName.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/settings/displayName.svelte @@ -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);
@@ -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}
- {#if displayNames[i]} + {#if names[i]} @@ -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; }}>