From f112f45e77128885f73c318fb60b61e5aafa60a6 Mon Sep 17 00:00:00 2001 From: Jake Fulmine Date: Sat, 3 Jun 2023 13:33:10 +0200 Subject: [PATCH] feat(dashboard): add filtering groups based on whether they have members and vice versa --- .../src/components/list/GroupList.svelte | 2 +- .../src/components/list/ListControl.svelte | 23 +++++++++++++++ .../src/components/list/MemberList.svelte | 2 +- dashboard/src/components/list/functions.ts | 29 ++++++++++++++++--- dashboard/src/components/list/types.ts | 4 +-- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/dashboard/src/components/list/GroupList.svelte b/dashboard/src/components/list/GroupList.svelte index b8e333a9..44626f15 100644 --- a/dashboard/src/components/list/GroupList.svelte +++ b/dashboard/src/components/list/GroupList.svelte @@ -52,7 +52,7 @@ listLoading = false; } - $: processedGroups = filterList(groups, options, "group") + $: processedGroups = filterList(groups, groups, options, "group") $: currentGroups = paginateList(processedGroups, pageOptions) $: shortMembers = createShortList(members) $: pageAmount = getPageAmount(processedGroups, pageOptions) diff --git a/dashboard/src/components/list/ListControl.svelte b/dashboard/src/components/list/ListControl.svelte index 984e6493..9fc59fe8 100644 --- a/dashboard/src/components/list/ListControl.svelte +++ b/dashboard/src/components/list/ListControl.svelte @@ -403,6 +403,29 @@ function resetPage() { + {#if pageOptions.type === 'member'} + + + Groups + resetPage()}> + + + + + + + {:else} + + + Members + resetPage()}> + + + + + + + {/if} {/if} diff --git a/dashboard/src/components/list/MemberList.svelte b/dashboard/src/components/list/MemberList.svelte index 7ed3b33e..243df610 100644 --- a/dashboard/src/components/list/MemberList.svelte +++ b/dashboard/src/components/list/MemberList.svelte @@ -52,7 +52,7 @@ listLoading = false; } - $: processedMembers = filterList(members, options, "member") + $: processedMembers = filterList(members, groups, options, "member") $: currentMembers = paginateList(processedMembers, pageOptions) $: shortMembers = createShortList(members) $: pageAmount = getPageAmount(processedMembers, pageOptions) diff --git a/dashboard/src/components/list/functions.ts b/dashboard/src/components/list/functions.ts index 7786feb9..913ca768 100644 --- a/dashboard/src/components/list/functions.ts +++ b/dashboard/src/components/list/functions.ts @@ -1,11 +1,11 @@ import type { Group, Member } from '../../api/types'; import type { ListOptions, PageOptions } from './types'; -export function filterList(list: T[], options: ListOptions, type?: string): T[] { +export function filterList(list: Group[]|Member[], groups: Group[], options: ListOptions, type?: string): Group[]|Member[] { let searchedList = search(list, options); let groupedList = [...searchedList]; if (type) - groupedList = group(searchedList, options, type); + groupedList = group(searchedList, groups, options, type); let filteredList = filter(groupedList, options); let sortedList = sort(filteredList, options); let orderedList = reorder(sortedList, options); @@ -143,8 +143,29 @@ function sort(list: T[], options: ListOptions): T[] { return newList; } -function group(list: T[], options: ListOptions, type?: string): T[] { - let groupIncludedList = [...list]; +function group(members: Member[], groups: Group[], options: ListOptions, type?: string): Group[]|Member[] { + let list = type === "member" ? [...members] : [...groups] || [] + let groupFilterList = [...list] + + if (options.groups.filter === "include") + if (type === "member") { + groupFilterList = [...list].filter(m => + groups.some(g => (g as Group).members.includes(m.uuid)) + ); + } else if (type === "group") { + groupFilterList = [...list].filter((g: Group) => g.members && g.members.length > 0) + } + + if (options.groups.filter === "exclude") + if (type === "member") { + groupFilterList = [...list].filter(m => + !groups.some(g => (g as Group).members.includes(m.uuid)) + ); + } else if (type === "group") { + groupFilterList = [...list].filter((g: Group) => !g.members || g.members.length < 1) + } + + let groupIncludedList = [...groupFilterList]; if (options.groups.include.list.length > 0) { // include has items! check the type and whether to match exactly diff --git a/dashboard/src/components/list/types.ts b/dashboard/src/components/list/types.ts index c7e72e0d..1d404e31 100644 --- a/dashboard/src/components/list/types.ts +++ b/dashboard/src/components/list/types.ts @@ -31,7 +31,7 @@ export interface ListOptions { exact: boolean, // only exclude members who are in ALL groups list: [] }, - none: boolean + filter: "all"|"include"|"exclude", }, // filter members based on whether fields have a value set or not // if set to true: only include items with a value @@ -87,7 +87,7 @@ export interface List { export const defaultListOptions: ListOptions = { search: {}, groups: { - none: false, + filter: "all", include: { exact: false, list: []