Skip to content

Commit

Permalink
Allow admins to view everything a manager can (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssb-jnk authored Apr 9, 2024
1 parent e3173c1 commit 582caef
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/pages/TeamDetail/TeamDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useCallback, useContext, useEffect, useState } from 'react'
import PageLayout from '../../components/PageLayout/PageLayout'
import { TeamDetailData, getTeamDetail, Team, SharedBuckets, Group } from '../../services/teamDetail'
import { useParams, useNavigate } from 'react-router-dom'
import { ApiError, TokenData, fetchUserInformationFromAuthToken } from '../../utils/services'
import { ApiError, TokenData, fetchUserInformationFromAuthToken, isDaplaAdmin } from '../../utils/services'

import { DaplaCtrlContext } from '../../provider/DaplaCtrlProvider'
import Table, { TableData } from '../../components/Table/Table'
Expand Down Expand Up @@ -73,6 +73,7 @@ const TeamDetail = () => {
const [loadingUsers, setLoadingUsers] = useState<boolean>(false)
const [teamDetailData, setTeamDetailData] = useState<TeamDetailData>()
const [userData, setUserData] = useState<User[]>()
const [isManager, setIsManager] = useState<boolean>(false)
const [teamDetailTableTitle, setTeamDetailTableTitle] = useState<string>(TEAM_USERS_TAB.title)
const [teamDetailTableHeaderColumns, setTeamDetailTableHeaderColumns] = useState<TableData['columns']>(
TEAM_USERS_TAB.columns
Expand Down Expand Up @@ -157,9 +158,25 @@ const TeamDetail = () => {
[activeTab]
)

const isTeamManager = useCallback(() => {
const teamManagers = (teamDetailData && (teamDetailData.team as Team).managers) ?? []
return teamManagers?.some((manager) => manager.principal_name.toLowerCase() === tokenData?.email.toLowerCase())
useEffect(() => {
const checkIsTeamManager = async () => {
const teamManagers = (teamDetailData && (teamDetailData.team as Team).managers) ?? []
if (tokenData) {
const isAdmin = await isDaplaAdmin(tokenData.email.toLowerCase())
if (isAdmin) {
setIsManager(true)
return
}
const isManagerResult = teamManagers.some(
(manager) => manager.principal_name.toLowerCase() === tokenData.email.toLowerCase()
)
setIsManager(isManagerResult)
} else {
setIsManager(false)
}
}

checkIsTeamManager()
}, [tokenData, teamDetailData])

useEffect(() => {
Expand Down Expand Up @@ -208,7 +225,7 @@ const TeamDetail = () => {
}, [refreshData])

useEffect(() => {
if (isTeamManager()) {
if (isManager) {
setTeamDetailTableHeaderColumns([
...TEAM_USERS_TAB.columns,
{
Expand All @@ -219,7 +236,7 @@ const TeamDetail = () => {
},
])
}
}, [isTeamManager])
}, [isManager])

useEffect(() => {
if (teamDetailData) {
Expand All @@ -228,7 +245,7 @@ const TeamDetail = () => {
setTeamDetailTableHeaderColumns(SHARED_BUCKETS_TAB.columns)
} else {
setTeamDetailTableTitle(TEAM_USERS_TAB.title)
if (isTeamManager()) {
if (isManager) {
setTeamDetailTableHeaderColumns([
...TEAM_USERS_TAB.columns,
{
Expand Down Expand Up @@ -361,7 +378,7 @@ const TeamDetail = () => {
}
content={renderContent()}
button={
isTeamManager() && teamDetailData ? (
isManager && teamDetailData ? (
<Button onClick={() => setOpenAddUserSidebarModal(true)}>+ Nytt medlem</Button>
) : undefined
}
Expand Down

0 comments on commit 582caef

Please sign in to comment.