From 3c64279d2e5384414668af2f995edc2a77ff6be6 Mon Sep 17 00:00:00 2001 From: ssb-jnk Date: Mon, 5 Feb 2024 09:40:56 +0100 Subject: [PATCH] . --- server.js | 52 +++--- src/pages/TeamDetail/TeamDetail.tsx | 260 ++++++++++++++-------------- src/provider/DaplaCtrlProvider.tsx | 22 +-- 3 files changed, 163 insertions(+), 171 deletions(-) diff --git a/server.js b/server.js index 3b0bdab5..37205397 100644 --- a/server.js +++ b/server.js @@ -247,35 +247,35 @@ app.get('/api/userProfile/:principalName/team', tokenVerificationMiddleware, asy app.get('/api/teamDetail/:teamUniformName', tokenVerificationMiddleware, async (req, res, next) => { try { - const token = req.token; - const teamUniformName = req.params.teamUniformName; - const teamInfoUrl = `${DAPLA_TEAM_API_URL}/teams/${teamUniformName}`; - const teamUsersUrl = `${DAPLA_TEAM_API_URL}/teams/${teamUniformName}/users`; + const token = req.token + const teamUniformName = req.params.teamUniformName + const teamInfoUrl = `${DAPLA_TEAM_API_URL}/teams/${teamUniformName}` + const teamUsersUrl = `${DAPLA_TEAM_API_URL}/teams/${teamUniformName}/users` const [teamInfo, teamUsers] = await Promise.all([ - fetchAPIData(token, teamInfoUrl, 'Failed to fetch team info') - .then(async (teamInfo) => { - const manager = await fetchTeamManager(token, teamInfo) - return { ...teamInfo, manager}; - }), - fetchAPIData(token, teamUsersUrl, 'Failed to fetch team users') - .then(async (teamUsers) => { - const resolvedUsers = await fetchTeamUsersWithGroups(token, teamUsers, teamUniformName); - return { ...teamUsers, _embedded: { users: resolvedUsers } }; - }), - ]); - - res.json({ teamInfo, teamUsers: teamUsers._embedded.users }); + fetchAPIData(token, teamInfoUrl, 'Failed to fetch team info').then(async (teamInfo) => { + const manager = await fetchTeamManager(token, teamInfo) + return { ...teamInfo, manager } + }), + fetchAPIData(token, teamUsersUrl, 'Failed to fetch team users').then(async (teamUsers) => { + const resolvedUsers = await fetchTeamUsersWithGroups(token, teamUsers, teamUniformName) + return { ...teamUsers, _embedded: { users: resolvedUsers } } + }), + ]) + + res.json({ teamInfo, teamUsers: teamUsers._embedded.users }) } catch (error) { - next(error); + next(error) } -}); +}) async function fetchTeamManager(token, teamInfo) { const teamManagerUrl = `${DAPLA_TEAM_API_URL}/groups/${teamInfo.uniform_name}-managers/users` - return await fetchAPIData(token, teamManagerUrl, 'Failed to fetch team manager').then((teamManager) => { - return teamManager.count > 0 ? teamManager._embedded.users[0] : managerFallback() - }).catch(() => managerFallback()) + return await fetchAPIData(token, teamManagerUrl, 'Failed to fetch team manager') + .then((teamManager) => { + return teamManager.count > 0 ? teamManager._embedded.users[0] : managerFallback() + }) + .catch(() => managerFallback()) } async function fetchTeamUsersWithGroups(token, teamUsers, teamUniformName) { @@ -290,10 +290,10 @@ async function fetchTeamUsersWithGroups(token, teamUsers, teamUniformName) { .flatMap((group) => group) currentUser.groups = flattenedGroups - + return { ...currentUser } - }); - return await Promise.all(userPromises); + }) + return await Promise.all(userPromises) } async function fetchAPIData(token, url, fallbackErrorMessage) { @@ -383,7 +383,7 @@ function sectionFallback(uniformName) { } function groupFallback() { - return { "_embedded": { "groups": [] }, "count": "0"} + return { _embedded: { groups: [] }, count: '0' } } //const lightship = await createLightship(); diff --git a/src/pages/TeamDetail/TeamDetail.tsx b/src/pages/TeamDetail/TeamDetail.tsx index 69353209..a5d0ddac 100644 --- a/src/pages/TeamDetail/TeamDetail.tsx +++ b/src/pages/TeamDetail/TeamDetail.tsx @@ -14,144 +14,136 @@ import { Text, Title, Link, Dialog } from '@statisticsnorway/ssb-component-libra import { Skeleton } from '@mui/material' export default function TeamDetail() { - - const { setBreadcrumbTeamDetailDisplayName } = useContext(DaplaCtrlContext) - const [error, setError] = useState() - const [loadingTeamDetailData, setLoadingTeamDetailData] = useState(true) - const [loadingTeamData, setLoadingTeamData] = useState(true) - const [teamDetailData, setTeamDetailData] = useState() - const [teamDetailTableData, setTeamDetailTableData] = useState() - - const { teamId } = useParams<{ teamId: string }>() - - const prepTeamData = useCallback( - (response: TeamDetailTeamResult): TableData['data'] => { - return response.teamUsers.map((user) => ({ - id: user?.principal_name, - navn: renderUsernameColumn(user), - gruppe: user.groups?.map((group) => getGroupType(group.uniform_name)).join(', '), - epost: user?.principal_name - })) - }, - [teamDetailData] + const { setBreadcrumbTeamDetailDisplayName } = useContext(DaplaCtrlContext) + const [error, setError] = useState() + const [loadingTeamData, setLoadingTeamData] = useState(true) + const [teamDetailData, setTeamDetailData] = useState() + const [teamDetailTableData, setTeamDetailTableData] = useState() + + const { teamId } = useParams<{ teamId: string }>() + + const prepTeamData = useCallback((response: TeamDetailTeamResult): TableData['data'] => { + return response.teamUsers.map((user) => ({ + id: user?.principal_name, + navn: renderUsernameColumn(user), + gruppe: user.groups?.map((group) => getGroupType(group.uniform_name)).join(', '), + epost: user?.principal_name, + })) + }, []) + + useEffect(() => { + if (!teamId) return + getTeamDetail(teamId as string) + .then((response) => { + if ((response as ErrorResponse).error) { + setError(response as ErrorResponse) + } else { + console.log(response) + setTeamDetailData(response as TeamDetailData) + } + }) + .catch((error) => { + setError({ error: { message: error.message, code: '500' } }) + }) + }, [teamId]) + + useEffect(() => { + if (!teamDetailData) return + + getTeamDetail(teamId as string) + .then((response) => { + if ((response as ErrorResponse).error) { + setError(response as ErrorResponse) + } else { + setTeamDetailTableData(prepTeamData(response as TeamDetailTeamResult)) + } + }) + .finally(() => setLoadingTeamData(false)) + .catch((error) => { + setError({ error: { message: error.message, code: '500' } }) + }) + }, [teamDetailData, teamId, prepTeamData]) + + // required for breadcrumb + useEffect(() => { + if (!teamDetailData) return + + const displayName = teamDetailData.teamInfo.display_name + teamDetailData.teamInfo.display_name = displayName + setBreadcrumbTeamDetailDisplayName({ displayName }) + }, [teamDetailData, setBreadcrumbTeamDetailDisplayName]) + + function renderUsernameColumn(user: User) { + return ( + <> + + + {user.display_name.split(', ').reverse().join(' ')} + + + {user && {user.section_name ? user.section_name : 'Mangler seksjon'}} + ) + } - useEffect(() => { - if (!teamId) return - getTeamDetail(teamId as string) - .then((response) => { - if ((response as ErrorResponse).error) { - setError(response as ErrorResponse) - } else { - console.log((response)) - setTeamDetailData(response as TeamDetailData) - } - }) - .finally(() => setLoadingTeamDetailData(false)) - .catch((error) => { - setError({ error: { message: error.message, code: '500' } }) - }) - - }, [teamId]) - - useEffect(() => { - if (!teamDetailData) return - - getTeamDetail(teamId as string) - .then((response) => { - if ((response as ErrorResponse).error) { - setError(response as ErrorResponse) - } else { - setTeamDetailTableData(prepTeamData(response as TeamDetailTeamResult)) - } - }) - .finally(() => setLoadingTeamData(false)) - .catch((error) => { - setError({ error: { message: error.message, code: '500' } }) - }) - }, [teamDetailData, teamId, prepTeamData]) - - // required for breadcrumb - useEffect(() => { - if (!teamDetailData) return - - const displayName = teamDetailData.teamInfo.display_name - teamDetailData.teamInfo.display_name = displayName - setBreadcrumbTeamDetailDisplayName({ displayName }) - - }, [teamDetailData, setBreadcrumbTeamDetailDisplayName]) - - function renderUsernameColumn(user: User) { - return ( - <> - - - {user.display_name.split(', ').reverse().join(' ')} - - - {user && {user.section_name ? user.section_name : "Mangler seksjon"}} - - ) - } - - function renderErrorAlert() { - return ( - - {error?.error.message} - - ) - } + function renderErrorAlert() { + return ( + + {error?.error.message} + + ) + } - function renderSkeletonOnLoad() { - return ( - <> - - - - ) - } + function renderSkeletonOnLoad() { + return ( + <> + + + + ) + } - function renderContent() { - if (error) return renderErrorAlert() - if (loadingTeamData) return renderSkeletonOnLoad() + function renderContent() { + if (error) return renderErrorAlert() + if (loadingTeamData) return renderSkeletonOnLoad() - if (teamDetailTableData) { - const teamOverviewTableHeaderColumns = [ - { - id: 'navn', - label: 'Navn', - }, - { - id: 'gruppe', - label: 'Gruppe', - }, - { - id: 'epost', - label: 'Epost ?', - } - ] - return ( - <> - - Team - - - - ) - } + if (teamDetailTableData) { + const teamOverviewTableHeaderColumns = [ + { + id: 'navn', + label: 'Navn', + }, + { + id: 'gruppe', + label: 'Gruppe', + }, + { + id: 'epost', + label: 'Epost ?', + }, + ] + return ( + <> + + Team + +
+ + ) } - - return ( - - {teamDetailData?.teamInfo.uniform_name} - {teamDetailData?.teamInfo.manager.display_name.split(', ').reverse().join(' ')} - {teamDetailData?.teamInfo.section_name} - - } - /> - ) + } + + return ( + + {teamDetailData?.teamInfo.uniform_name} + {teamDetailData?.teamInfo.manager.display_name.split(', ').reverse().join(' ')} + {teamDetailData?.teamInfo.section_name} + + } + /> + ) } diff --git a/src/provider/DaplaCtrlProvider.tsx b/src/provider/DaplaCtrlProvider.tsx index 8c5de52f..98206d10 100644 --- a/src/provider/DaplaCtrlProvider.tsx +++ b/src/provider/DaplaCtrlProvider.tsx @@ -15,16 +15,14 @@ interface DaplaCtrlContextType { ) => void breadcrumbTeamDetailDisplayName: BreadcrumbTeamDetailDisplayName | null - setBreadcrumbTeamDetailDisplayName: ( - breadcrumbTeamDetailDisplayName: BreadcrumbTeamDetailDisplayName | null - ) => void + setBreadcrumbTeamDetailDisplayName: (breadcrumbTeamDetailDisplayName: BreadcrumbTeamDetailDisplayName | null) => void } const DaplaCtrlContext = createContext({ breadcrumbUserProfileDisplayName: null, - setBreadcrumbUserProfileDisplayName: () => { }, + setBreadcrumbUserProfileDisplayName: () => {}, breadcrumbTeamDetailDisplayName: null, - setBreadcrumbTeamDetailDisplayName: () => { }, + setBreadcrumbTeamDetailDisplayName: () => {}, }) const DaplaCtrlProvider: FC<{ children: ReactNode }> = ({ children }) => { @@ -34,12 +32,14 @@ const DaplaCtrlProvider: FC<{ children: ReactNode }> = ({ children }) => { useState(null) return ( - + {children} )