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

597 Profilbilde i headingen #598

Merged
merged 3 commits into from
Jun 14, 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
9 changes: 0 additions & 9 deletions apps/web/src/api/auth/authApiTypes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
// refresh
export interface RenewResponse {
accessToken: string
expiresAt: number
expiration: number
sameSite: boolean
secure: boolean
}

// userInfo
export interface UserInfo {
name?: string
Expand Down
42 changes: 0 additions & 42 deletions apps/web/src/api/auth/authClient.ts

This file was deleted.

37 changes: 0 additions & 37 deletions apps/web/src/api/auth/authHelpers.ts

This file was deleted.

2 changes: 0 additions & 2 deletions apps/web/src/api/auth/constants.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/web/src/api/data/employee/employeeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
export const getEmployeeTable = () =>
getAtApiV2<EmployeeTableResponse[]>('/employees/employeeTable')

export const getEmployeeProfile = (url: string, email: string) =>
export const getEmployeeProfile = (email: string) =>
getAtApiV2<EmployeeProfileResponse>(`/employees/employeeProfile`, {
params: { email },
})
Expand Down
10 changes: 3 additions & 7 deletions apps/web/src/api/data/employee/employeeQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ export const useEmployeeTable = () =>
})

export const useEmployeeProfile = (email: string) =>
useSWR(
['/employeeProfile', email],
([url, email]) => getEmployeeProfile(url, email),
{
revalidateOnFocus: false,
}
)
useSWR(['/employeeProfile', email], ([email]) => getEmployeeProfile(email), {
revalidateOnFocus: false,
})

export const useEmployeeCompetence = (email: string) =>
useSWR(
Expand Down
25 changes: 13 additions & 12 deletions apps/web/src/components/header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, { FunctionComponent } from 'react'
import { NavLink, Link, useLocation } from 'react-router-dom'
import { AppBar, Avatar, Tabs, Tab, Toolbar } from '@mui/material'
import { styled } from '@mui/material/styles'
import { NavLink, Link, useLocation } from 'react-router-dom'
import { NavMenu } from './NavMenu'
import NavMenu from './NavMenu'
import { ReactComponent as KnowitLogo } from '../../assets/logo.svg'
import { ReactComponent as FallbackUserIcon } from '../../assets/fallback_user.svg'
import ModeSwitch from './ModeSwitch'
import { useUserInfo } from '../../hooks/useUserInfo'
import LoginLogoutButton from '../LoginLogoutButton'
import ModeSwitch from './ModeSwitch'

const ComponentRoot = styled('div')(({ theme }) => ({
top: 0,
Expand Down Expand Up @@ -40,13 +39,10 @@ interface HeaderProps {
onChangeMode: () => void
}

export const Header: FunctionComponent<HeaderProps> = ({
darkMode,
onChangeMode,
}) => {
const { user, isAuthenticated } = useUserInfo()
export default function Header({ darkMode, onChangeMode }: HeaderProps) {
const availablePages = ['/ansatte', '/kunder', '/kompetanse', '/organisasjon']
const activePage = useLocation().pathname
const { isAuthenticated, userEmployeeProfile } = useUserInfo()

let tabsVisiblePage: string | boolean
availablePages.includes(activePage)
Expand Down Expand Up @@ -95,18 +91,23 @@ export const Header: FunctionComponent<HeaderProps> = ({
<Link data-testid="knowit-logo" to={'/'}>
<KnowitLogoStyled title="knowit-logo" />
</Link>

<NavMenu>{isAuthenticated && <HeaderTabs />}</NavMenu>

<ActionsContainer>
<LoginLogoutButton />
<AvatarStyled id="userAvatar" alt={user?.name} src={user?.picture}>
<AvatarStyled
id="userAvatar"
alt={userEmployeeProfile?.name}
src={userEmployeeProfile?.image}
>
<FallbackUserIcon />
</AvatarStyled>

<ModeSwitch onChange={handleModeSwitch} checked={darkMode} />
</ActionsContainer>
</Toolbar>
</AppBar>
</ComponentRoot>
)
}

export default Header
2 changes: 1 addition & 1 deletion apps/web/src/components/header/NavMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ const ComponentRoot = styled('ul')(() => ({
overflow: 'hidden',
}))

export function NavMenu({ children }: { children: React.ReactNode }) {
export default function NavMenu({ children }: { children: React.ReactNode }) {
return <ComponentRoot>{children}</ComponentRoot>
}
43 changes: 29 additions & 14 deletions apps/web/src/context/UserInfoContext.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { createContext, useEffect, useState } from 'react'
import { getEmployeeProfile } from '../api/data/employee/employeeApi'
import { getUserInfo } from '../api/data/user/userApi'
import { isError } from '../api/errorHandling'
import { EmployeeProfileResponse } from '../api/data/employee/employeeApiTypes'
import { UserInfo } from '../api/auth/authApiTypes'

interface UserInfoContextProps {
user: UserInfo | null | undefined
setUser: (val: UserInfo | null) => void
userEmployeeProfile: EmployeeProfileResponse
}

export const UserInfoContext = createContext<UserInfoContextProps | null>(null)
Expand All @@ -14,23 +17,13 @@ const UserInfoProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [userInfo, setUserInfo] = useState<UserInfo | null>(null)
const [userEmployeeProfile, setUserEmployeeProfile] =
useState<EmployeeProfileResponse>()

useEffect(() => {
const fetchUser = async () => {
async function fetchUser() {
try {
/* The timeout is to prevent a user which was logged in in previous seesion from seeing the login page for a short flash.
* When user is undefined, App component will return null, which will prevent the login page from being rendered. */
/* const timeout = setTimeout(
() => {
setUserInfo(null)
},
localStorage.getItem('login') ? 3000 : 0
) */

const user = await getUserInfo()

/* clearTimeout(timeout) */

setUserInfo(user)
} catch (error) {
if (isError(error)) {
Expand All @@ -42,8 +35,30 @@ const UserInfoProvider: React.FC<{ children: React.ReactNode }> = ({
fetchUser()
}, [])

useEffect(() => {
async function fetchEmployeeProfile() {
if (userInfo) {
const userEmail = userInfo.email?.toLowerCase()
if (userEmail) {
const data = await getEmployeeProfile(userEmail)
setUserEmployeeProfile(data)
}
} else {
setUserEmployeeProfile(null)
}
}

fetchEmployeeProfile()
}, [userInfo])

return (
<UserInfoContext.Provider value={{ user: userInfo, setUser: setUserInfo }}>
<UserInfoContext.Provider
value={{
user: userInfo,
setUser: setUserInfo,
userEmployeeProfile,
}}
>
{children}
</UserInfoContext.Provider>
)
Expand Down
10 changes: 6 additions & 4 deletions apps/web/src/hooks/useUserInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export function useUserInfo() {
const [isAuthenticated, setIsAuthenticated] = useState(false)
const userInfoContext = useContext(UserInfoContext)
const { authStatus } = useAuthenticator((context) => [context.authStatus])
const { user, setUser } = userInfoContext
const { user, setUser, userEmployeeProfile } = userInfoContext

useEffect(() => {
setIsAuthenticated(authStatus === 'authenticated')
}, [authStatus])
if (user) {
setIsAuthenticated(authStatus === 'authenticated')
}
}, [authStatus, user, userEmployeeProfile])

return { user, setUser, isAuthenticated }
return { user, setUser, isAuthenticated, userEmployeeProfile }
}
Loading