From f3069817c1c08d9a827ab9da231d0fe147248db7 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 12 Oct 2021 04:40:48 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20`GroupMembersBox`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/App.tsx | 12 ++++-- .../src/components/group/GroupCreateBox.tsx | 2 +- .../src/components/group/GroupMembersBox.tsx | 39 +++++++++++++++++++ frontend/src/contexts/cache.tsx | 31 ++++++++++++--- 4 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 frontend/src/components/group/GroupMembersBox.tsx diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 345b7b6..1db2de7 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,6 +1,6 @@ import * as Reach from "@reach/router" import {RouteComponentProps} from "@reach/router" -import {LayoutThreeCol} from "@steffo/bluelib-react" +import {Chapter, LayoutThreeCol} from "@steffo/bluelib-react" import * as React from "react" import {AuthorizationRouter} from "./components/authorization/AuthorizationRouter" import {AuthorizationStepPage} from "./components/authorization/AuthorizationStepPage" @@ -9,6 +9,7 @@ import {ErrorCatcherBox} from "./components/errors/ErrorCatcherBox" import {GroupCreateBox} from "./components/group/GroupCreateBox" import {GroupDescriptionBox} from "./components/group/GroupDescriptionBox" import {GroupListBox} from "./components/group/GroupListBox" +import {GroupMembersBox} from "./components/group/GroupMembersBox" import {GroupRouter} from "./components/group/GroupRouter" import {InstanceDescriptionBox} from "./components/instance/InstanceDescriptionBox" import {InstanceRouter} from "./components/instance/InstanceRouter" @@ -48,10 +49,15 @@ function App({..._}: RouteComponentProps) { } selectedRoute={({selection}) => <> - + + + + } + unselectedRoute={({viewSet}) => <> + + } selectedRoute={({selection}) => <> cache.users?.resources?.filter(m => m.value.id !== authorization?.state.user?.id).map(m => { + () => cache?.users?.resources?.filter(m => m.value.id !== authorization?.state.user?.id).map(m => { const obj: { [key: string]: number } = {} obj[m.value.username] = m.value.id return obj diff --git a/frontend/src/components/group/GroupMembersBox.tsx b/frontend/src/components/group/GroupMembersBox.tsx new file mode 100644 index 0000000..8725727 --- /dev/null +++ b/frontend/src/components/group/GroupMembersBox.tsx @@ -0,0 +1,39 @@ +import {Box, Heading, Idiomatic, ListUnordered} from "@steffo/bluelib-react" +import * as React from "react" +import {useCacheContext} from "../../contexts/cache" +import {ManagedResource} from "../../hooks/useManagedViewSet" +import {SophonResearchGroup} from "../../types/SophonTypes" + + +export interface GroupMembersBoxProps { + resource: ManagedResource +} + + +export function GroupMembersBox({resource}: GroupMembersBoxProps): JSX.Element | null { + const cache = useCacheContext() + + if(!cache) { + return null + } + if(!cache.users) { + return null + } + + const trueMembers = [...new Set([resource.value.owner, ...resource.value.members])] + + return ( + + + Members of {resource.value.name} + + + {trueMembers.map((id, index) => ( + + {cache.getUserById(id)!.value.username} + + ))} + + + ) +} diff --git a/frontend/src/contexts/cache.tsx b/frontend/src/contexts/cache.tsx index 8d9e77b..d468d49 100644 --- a/frontend/src/contexts/cache.tsx +++ b/frontend/src/contexts/cache.tsx @@ -1,5 +1,5 @@ import * as React from "react" -import {ManagedViewSet, useManagedViewSet} from "../hooks/useManagedViewSet" +import {ManagedResource, ManagedViewSet, useManagedViewSet} from "../hooks/useManagedViewSet" import {WithChildren} from "../types/ExtraTypes" import {SophonUser} from "../types/SophonTypes" @@ -8,18 +8,19 @@ import {SophonUser} from "../types/SophonTypes" * The contents of the {@link cacheContext}. */ type Cache = { - users?: ManagedViewSet, + users: ManagedViewSet | undefined, + getUserById: (id: number) => ManagedResource | undefined } -const cacheContext = React.createContext({}) +const cacheContext = React.createContext(undefined) const CacheContext = cacheContext /** * Hook to access the {@link cacheContext}. */ -export function useCacheContext(): Cache { +export function useCacheContext(): Cache | undefined { return React.useContext(cacheContext) } @@ -35,5 +36,25 @@ export function useCacheContext(): Cache { export function CacheProvider({children}: WithChildren): JSX.Element { const users = useManagedViewSet("/api/core/users/", "id") - return + const usersIdMap = + React.useMemo( + () => { + return users?.resources?.map(u => { + const obj: { [key: string]: ManagedResource } = {} + obj[u.value.id.toString()] = u + return obj + }).reduce((a, b) => { + return {...a, ...b} + }) + }, + [users], + ) + + const getUserById = + React.useCallback( + (id: number) => usersIdMap?.[id.toString()], + [usersIdMap], + ) + + return }