From a48f2b40c40fcd11d4ecdbf95cd17497d046806e Mon Sep 17 00:00:00 2001 From: Jake Fulmine Date: Sat, 3 Jun 2023 12:27:58 +0200 Subject: [PATCH] refactor(dashboard): use svelte context for lists --- dashboard/src/components/group/Body.svelte | 4 +- .../src/components/group/CardView.svelte | 33 +++-- dashboard/src/components/group/Edit.svelte | 34 ++--- .../src/components/group/MemberEdit.svelte | 35 +++-- .../src/components/group/NewGroup.svelte | 14 +- dashboard/src/components/group/Privacy.svelte | 21 +-- dashboard/src/components/list/CardView.svelte | 41 +++--- .../src/components/list/GroupList.svelte | 103 ++++++++++++++ dashboard/src/components/list/List.svelte | 126 ------------------ .../src/components/list/ListControl.svelte | 11 +- dashboard/src/components/list/ListView.svelte | 56 +++++--- .../src/components/list/MemberList.svelte | 103 ++++++++++++++ dashboard/src/components/member/Body.svelte | 4 +- .../src/components/member/CardView.svelte | 26 ++-- dashboard/src/components/member/Edit.svelte | 32 ++--- .../src/components/member/GroupEdit.svelte | 29 ++-- .../src/components/member/NewMember.svelte | 31 +++-- .../src/components/member/Privacy.svelte | 21 +-- dashboard/src/routes/Dash/Dash.svelte | 56 +++++--- dashboard/src/routes/Dash/Group/Group.svelte | 79 +++++------ .../src/routes/Dash/Member/Member.svelte | 85 ++++++------ dashboard/src/routes/Dash/Profile.svelte | 62 ++++++--- dashboard/src/routes/Dash/Random.svelte | 25 ++-- 23 files changed, 598 insertions(+), 433 deletions(-) create mode 100644 dashboard/src/components/list/GroupList.svelte delete mode 100644 dashboard/src/components/list/List.svelte create mode 100644 dashboard/src/components/list/MemberList.svelte diff --git a/dashboard/src/components/group/Body.svelte b/dashboard/src/components/group/Body.svelte index 757b3fb1..1cb5db3e 100644 --- a/dashboard/src/components/group/Body.svelte +++ b/dashboard/src/components/group/Body.svelte @@ -142,9 +142,9 @@ {:else if editMode} - + {:else if memberMode} - + {/if} diff --git a/dashboard/src/components/group/CardView.svelte b/dashboard/src/components/group/CardView.svelte index a1030580..b62e80af 100644 --- a/dashboard/src/components/group/CardView.svelte +++ b/dashboard/src/components/group/CardView.svelte @@ -1,6 +1,6 @@ + + + +{#if listLoading && !err} +
+ +
+{:else if err} + + + {err} + + + + + +{:else} +{members.length} {pageOptions.type}s ({currentGroups.length} shown) {e.preventDefault(); fetchList()}}>Refresh list + + + +{#if !err && !pageOptions.isPublic} + +{/if} +{#if pageOptions.view === "card"} + +{:else if pageOptions.view === "tiny"} + tiny! +{:else} + +{/if} + +{/if} + + \ No newline at end of file diff --git a/dashboard/src/components/list/List.svelte b/dashboard/src/components/list/List.svelte deleted file mode 100644 index 9c55e181..00000000 --- a/dashboard/src/components/list/List.svelte +++ /dev/null @@ -1,126 +0,0 @@ - - - - -{#if listLoading && !err} -
- -
-{:else if err} - - - {err} - - - - - -{:else} -{lists.processedList.length} {pageOptions.type}s ({lists.currentPage.length} shown) {e.preventDefault(); fetchList()}}>Refresh list - - - -{#if !err && !pageOptions.isPublic} - {#if pageOptions.type === "member"} - - {:else if pageOptions.type === "group"} - - {/if} -{/if} -{#if pageOptions.view === "card"} - -{:else if pageOptions.view === "tiny"} - tiny! -{:else} - -{/if} - -{/if} - - \ No newline at end of file diff --git a/dashboard/src/components/list/ListControl.svelte b/dashboard/src/components/list/ListControl.svelte index 6b5e37ea..984e6493 100644 --- a/dashboard/src/components/list/ListControl.svelte +++ b/dashboard/src/components/list/ListControl.svelte @@ -10,8 +10,9 @@ import { Link, useParams } from 'svelte-navigator'; import type { ListOptions, List, PageOptions, ShortList } from './types'; export let options: ListOptions; -export let lists: List; export let pageOptions: PageOptions; +export let shortGroups: any[] = [] +export let shortMembers: any[] = [] let advanced = false; @@ -286,9 +287,9 @@ function resetPage() {

Include {pageOptions.type === 'group' ? "groups with the following members" : "members in the following groups"}

{#if pageOptions.type === "member"} - + {:else if pageOptions.type === "group"} - + {/if} @@ -304,9 +305,9 @@ function resetPage() {

Exclude {pageOptions.type === 'group' ? "groups with the following members" : "members in the following groups"}

{#if pageOptions.type === "member"} - + {:else if pageOptions.type === "group"} - + {/if} diff --git a/dashboard/src/components/list/ListView.svelte b/dashboard/src/components/list/ListView.svelte index b66c3ae8..9026b990 100644 --- a/dashboard/src/components/list/ListView.svelte +++ b/dashboard/src/components/list/ListView.svelte @@ -10,13 +10,13 @@ import MemberBody from '../member/Body.svelte'; import GroupBody from '../group/Body.svelte'; import CardsHeader from '../common/CardsHeader.svelte'; - import { defaultListOptions, type List, type ListOptions, type PageOptions } from './types'; + import { defaultListOptions, type ListOptions, type PageOptions } from './types'; let settings = JSON.parse(localStorage.getItem("pk-settings")); export let options: ListOptions = JSON.parse(JSON.stringify(defaultListOptions)); - export let otherList: List ; - export let lists: List; + export let fullListLength: number; + export let currentList: Member[]|Group[]; export let pageOptions: PageOptions; function getItemLink(item: Member | Group): string { @@ -39,13 +39,13 @@ let el; if (event.key === "ArrowDown") { - if (index + 1 < indexStart + pageOptions.itemsPerPage && index + 1 < lists.processedList.length) el = document.getElementById(`${pageOptions.type}-card-${index + 1}`); + if (index + 1 < indexStart + pageOptions.itemsPerPage && index + 1 < fullListLength) el = document.getElementById(`${pageOptions.type}-card-${index + 1}`); else el = document.getElementById(`${pageOptions.type}-card-${indexStart}`); } if (event.key === "ArrowUp") { if (index - 1 >= indexStart) el = document.getElementById(`${pageOptions.type}-card-${index - 1}`); - else if (lists.processedList.length <= indexStart + pageOptions.itemsPerPage) el = document.getElementById(`${pageOptions.type}-card-${lists.processedList.length - 1}`); + else if (fullListLength <= indexStart + pageOptions.itemsPerPage) el = document.getElementById(`${pageOptions.type}-card-${fullListLength - 1}`); else el = document.getElementById(`${pageOptions.type}-card-${indexStart + pageOptions.itemsPerPage - 1}`); } @@ -114,12 +114,12 @@ {#if (settings && settings.accessibility ? (!settings.accessibility.expandedcards && !settings.accessibility.pagelinks) : true)}
- {#each lists.currentPage as item, index (pageOptions.randomized ? item.uuid + '-' + index : item.uuid)} + {#each currentList as item, index (pageOptions.randomized ? item.uuid + '-' + index : item.uuid)}

+ {copiedArray[indexStart + index] ? "Copied!" : "Copy public link"} @@ -135,9 +135,9 @@ {#if pageOptions.type === "member"} - + {:else if pageOptions.type === "group"} - + {/if} @@ -145,12 +145,12 @@ {/each} {:else if settings.accessibility.expandedcards} - {#each lists.currentPage as item, index (pageOptions.randomized ? item.uuid + '-' + index : item.uuid)} + {#each currentList as item, index (pageOptions.randomized ? item.uuid + '-' + index : item.uuid)} - + {copiedArray[indexStart + index] ? "Copied!" : "Copy public link"} - + {#if pageOptions.type === "member"} - + {:else if pageOptions.type === "group"} - + {/if} {/each} {:else} -{/if} \ No newline at end of file +{/if} + + \ No newline at end of file diff --git a/dashboard/src/components/list/MemberList.svelte b/dashboard/src/components/list/MemberList.svelte new file mode 100644 index 00000000..7ed3b33e --- /dev/null +++ b/dashboard/src/components/list/MemberList.svelte @@ -0,0 +1,103 @@ + + + + +{#if listLoading && !err} +
+ +
+{:else if err} + + + {err} + + + + + +{:else} +{members.length} {pageOptions.type}s ({currentMembers.length} shown) {e.preventDefault(); fetchList()}}>Refresh list + + + +{#if !err && !pageOptions.isPublic} + +{/if} +{#if pageOptions.view === "card"} + +{:else if pageOptions.view === "tiny"} + tiny! +{:else} + +{/if} + +{/if} + + \ No newline at end of file diff --git a/dashboard/src/components/member/Body.svelte b/dashboard/src/components/member/Body.svelte index 954c314d..0bc6293f 100644 --- a/dashboard/src/components/member/Body.svelte +++ b/dashboard/src/components/member/Body.svelte @@ -198,8 +198,8 @@ {/if} {:else if editMode} - + {:else if groupMode} - + {/if} diff --git a/dashboard/src/components/member/CardView.svelte b/dashboard/src/components/member/CardView.svelte index 147cbf87..d81c0390 100644 --- a/dashboard/src/components/member/CardView.svelte +++ b/dashboard/src/components/member/CardView.svelte @@ -1,5 +1,5 @@ {#if settings && settings.appearance.color_background && !notOwnSystem} @@ -173,7 +162,7 @@ {:else if group && group.id} - +
copyShortLink()} on:keydown={(e) => copyShortLink(e)} tabindex={0} >
@@ -181,7 +170,7 @@ {copied ? "Copied!" : "Copy public link"}
- +
{/if} @@ -189,14 +178,14 @@ Fetching members... {:else if memberErr} {memberErr} - {:else if lists.rawList && lists.rawList.length > 0} + {:else if groupMembers && groupMembers.length > 0} - {lists.processedList.length} {pageOptions.type}s ({lists.currentPage.length} shown) + {processedList.length} {pageOptions.type}s ({currentPage.length} shown) {#if pageOptions.view === "card"} - + {:else} - + {/if} {/if} diff --git a/dashboard/src/routes/Dash/Member/Member.svelte b/dashboard/src/routes/Dash/Member/Member.svelte index 70b8e8fc..c2a7104a 100644 --- a/dashboard/src/routes/Dash/Member/Member.svelte +++ b/dashboard/src/routes/Dash/Member/Member.svelte @@ -3,7 +3,7 @@ import Body from '../../../components/member/Body.svelte'; import ListView from '../../../components/list/ListView.svelte'; import { useParams, Link, navigate, useLocation } from 'svelte-navigator'; - import { onMount } from 'svelte'; + import { onMount, setContext } from 'svelte'; import api from "../../../api"; import type { Member, Group } from "../../../api/types"; import CardsHeader from "../../../components/common/CardsHeader.svelte"; @@ -14,6 +14,7 @@ import { defaultListOptions, defaultPageOptions } from '../../../components/list/types'; import { filterList, getPageAmount, paginateList } from '../../../components/list/functions'; import PageControl from "../../../components/list/PageControl.svelte"; + import { writable, type Writable } from "svelte/store"; // get the state from the navigator so that we know which tab to start on let location = useLocation(); @@ -26,8 +27,6 @@ let params = useParams(); let err = ""; let groupErr = ""; - let member: Member; - let systemGroups: Group[] = []; let isDeleted = false; let notOwnSystem = false; let copied = false; @@ -41,18 +40,33 @@ if (!user) navigate("/"); } - onMount(() => { - fetchMember(); + onMount(async () => { + await fetchMember(); }); + let membersStore: Writable = writable([]) + let groupsStore: Writable = writable([]) + $: members = setContext>("members", membersStore) + $: groups = setContext>("groups", groupsStore) + $: member = $members.filter(m => m.id === $params.id)[0] || {} + let title = isPublic ? "member" : "member (dash)"; async function fetchMember() { try { - member = await api().members($params.id).get({auth: !isPublic}); - if (!isPublic && !member.privacy) { - notOwnSystem = true; - throw new Error("Member is not from own system."); + if (isPublic) { + const res: Member = await api().members($params.id).get({auth: !isPublic}); + $members = [res] + member = $members.filter(m => m.id === $params.id)[0] + } else { + const res: Member[] = await api().systems("@me").members.get({ auth: true}); + $members = res; + member = $members.filter(m => m.id === $params.id)[0] + + if (!member.privacy) { + notOwnSystem = true; + throw new Error("Member is not from own system."); + } } err = ""; loading = false; @@ -70,9 +84,13 @@ async function fetchGroups() { try { - lists.rawList = await api().members($params.id).groups().get({auth: !isPublic, query: { with_members: !isPublic } }); + let memberGroups: Group[] = await api().members($params.id).groups().get({auth: !isPublic }); + memberGroups.forEach(g => g.members = []) + groups.set(memberGroups) + if (!isPublic) { - systemGroups = await api().systems("@me").groups.get({ auth: true, query: { with_members: true } }); + const systemGroups: Group[] = await api().systems("@me").groups.get({ auth: true, query: { with_members: true } }); + groups.set(systemGroups) } groupErr = ""; groupLoading = false; @@ -83,24 +101,6 @@ } } - let lists: Lists = { - rawList: [], - processedList: [], - currentPage: [], - - shortGroups: [], - shortMembers: [], - } - - let nope: Lists = { - rawList: [], - processedList: [], - currentPage: [], - - shortGroups: [], - shortMembers: [], - } - let listOptions: ListOptions = {...defaultListOptions}; let pageOptions: PageOptions = {...defaultPageOptions, @@ -133,17 +133,10 @@ } } - $: lists.processedList = filterList(lists.rawList, listOptions); - $: lists.currentPage = paginateList(lists.processedList, pageOptions); - $: pageAmount = getPageAmount(lists.processedList, pageOptions); - - function updateDelete(event: any) { - lists.rawList = lists.rawList.filter(m => m.id !== event.detail); - } - - function update(event: any) { - lists.rawList = lists.rawList.map(m => m.id === event.detail.id ? m = event.detail : m); - } + $: memberGroups = $groups.filter(g => g.members.includes(member.uuid)); + $: processedList = filterList(memberGroups, listOptions); + $: currentPage = paginateList(processedList, pageOptions); + $: pageAmount = getPageAmount(processedList, pageOptions); {#if settings && settings.appearance.color_background && !notOwnSystem} @@ -171,7 +164,7 @@ {:else if member && member.id} - +
copyShortLink()} on:keydown={(e) => copyShortLink(e)} tabindex={0} >
@@ -179,7 +172,7 @@ {copied ? "Copied!" : "Copy public link"}
- +
{/if} @@ -187,14 +180,14 @@ Fetching groups... {:else if groupErr} {groupErr} - {:else if lists.rawList && lists.rawList.length > 0} + {:else if memberGroups && memberGroups.length > 0} - {lists.processedList.length} {pageOptions.type}s ({lists.currentPage.length} shown) + {processedList.length} {pageOptions.type}s ({currentPage.length} shown) {#if pageOptions.view === "card"} - + {:else} - + {/if} {/if} diff --git a/dashboard/src/routes/Dash/Profile.svelte b/dashboard/src/routes/Dash/Profile.svelte index 1caf184f..4f950a31 100644 --- a/dashboard/src/routes/Dash/Profile.svelte +++ b/dashboard/src/routes/Dash/Profile.svelte @@ -1,20 +1,22 @@