Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional render of create team button #222

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/pages/TeamOverview/TeamOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import PageSkeleton from '../../components/PageSkeleton/PageSkeleton'

import { fetchTeamOverviewData, TeamOverviewData } from '../../services/teamOverview'
import { formatDisplayName } from '../../utils/utils'
import { ApiError, fetchUserInformationFromAuthToken } from '../../utils/services'
import { ApiError, fetchUserInformationFromAuthToken, isDaplaAdmin } from '../../utils/services'
import FormattedTableColumn from '../../components/FormattedTableColumn/FormattedTableColumn'
import { User } from '../../services/userProfile'

const MY_TEAMS_TAB = {
title: 'Mine team',
Expand All @@ -24,6 +25,7 @@ const ALL_TEAMS_TAB = {

const TeamOverview = () => {
const [activeTab, setActiveTab] = useState<TabProps | string>(MY_TEAMS_TAB)
const [isSectionManager, SetIsSectionManager] = useState<boolean>(false)
const [teamOverviewData, setTeamOverviewData] = useState<TeamOverviewData>()
const [teamOverviewTableData, setTeamOverviewTableData] = useState<TableData['data']>()
const [teamOverviewTableTitle, setTeamOverviewTableTitle] = useState<string>(MY_TEAMS_TAB.title)
Expand Down Expand Up @@ -64,6 +66,27 @@ const TeamOverview = () => {
fetchData()
}, [])

useEffect(() => {
const userProfileItem = localStorage.getItem('userProfile')
if (!userProfileItem) return

const user = JSON.parse(userProfileItem) as User
if (!user) return

const allowViewCreateTeamButton = async () => {
const isAdmin = await isDaplaAdmin(user.principal_name)
if (isAdmin) {
SetIsSectionManager(true)
return
}
if (user.job_title.toLowerCase() === 'seksjonssjef') {
SetIsSectionManager(true)
}
}

allowViewCreateTeamButton()
}, [])

useEffect(() => {
if (teamOverviewData) {
if (teamTab === MY_TEAMS_TAB.path) {
Expand Down Expand Up @@ -138,9 +161,13 @@ const TeamOverview = () => {
title='Teamoversikt'
content={renderContent()}
button={
<Button onClick={() => window.open(import.meta.env.DAPLA_CTRL_DAPLA_START_URL ?? '', 'noopener')}>
+ Opprett team
</Button>
<>
{teamOverviewData && isSectionManager && (
<Button onClick={() => window.open(import.meta.env.DAPLA_CTRL_DAPLA_START_URL ?? '', 'noopener')}>
+ Opprett team
</Button>
)}
</>
}
/>
)
Expand Down
4 changes: 3 additions & 1 deletion src/services/userProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ interface TeamManager {
principal_name: string
}

interface User {
export interface User {
display_name: string
principal_name: string
section_name: string
job_title: string
azure_ad_id?: string
first_name?: string
last_name?: string
Expand All @@ -57,6 +58,7 @@ export const getUserProfile = async (principalName: string): Promise<User | ApiE
'section_name',
'division_name',
'phone',
'job_title',
'section_manager.display_name',
'section_manager.principal_name',
]
Expand Down