Skip to content

Commit

Permalink
fix(impersonifcation): retrieve current impersonification info in loc…
Browse files Browse the repository at this point in the history
…al storage

fix(impersonifcation): retrieve current impersonification info in localstorage + add overlay position swap
  • Loading branch information
pl-buiquang authored Jul 12, 2024
1 parent 958903c commit 96187ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/components/Impersonation/Overlay.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Button } from '@mui/material'
import React from 'react'
import React, { useState } from 'react'
import { Button, IconButton } from '@mui/material'
import { useAppSelector, useAppDispatch } from 'state'
import { impersonate } from 'state/me'
import SwapVertIcon from '@mui/icons-material/SwapVert'

export default () => {
const me = useAppSelector((state) => state.me)
const dispatch = useAppDispatch()
const [bottomPosition, setBottomPosition] = useState(true)

const stopImpersonation = () => {
dispatch(impersonate(undefined))
Expand All @@ -19,7 +21,8 @@ export default () => {
width: '100%',
backgroundColor: '#ffac41',
position: 'fixed',
bottom: 0,
bottom: bottomPosition ? 0 : undefined,
top: bottomPosition ? undefined : 0,
left: 0,
height: '3em',
display: 'flex',
Expand All @@ -36,6 +39,11 @@ export default () => {
>
<Button onClick={stopImpersonation}>Arrêter</Button>
</div>
<div style={{ position: 'absolute', right: '1em' }}>
<IconButton onClick={() => setBottomPosition((prev) => !prev)}>
<SwapVertIcon color="primary" />
</IconButton>
</div>
</div>
</>
)
Expand Down
15 changes: 14 additions & 1 deletion src/components/Impersonation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import Overlay from './Overlay'
import { addRequestConfigHook as addRequestConfigHookForFhir } from 'services/apiFhir'
import { addRequestConfigHook as addRequestConfigHookForBackend } from 'services/apiBackend'
import services from 'services/aphp'
import { useAppSelector } from 'state'
import { useAppDispatch, useAppSelector } from 'state'
import PeopleAltIcon from '@mui/icons-material/PeopleAlt'
import { IconButton } from '@mui/material'
import { User } from 'types'
import { CODE_DISPLAY_JWT } from 'constants.js'
import { impersonate } from 'state/me'

export const IMPERSONATED_USER = 'impersonated_user'

Expand All @@ -30,6 +31,7 @@ type ImpersonationProps = {

const Impersonation = (props: ImpersonationProps) => {
const { UserInfo } = props
const dispatch = useAppDispatch()
const me = useAppSelector((state) => state.me)
const [modalOpen, setModalOpen] = useState(false)
const [haveRightFullAdmin, setHaveRightFullAdmin] = useState(false)
Expand All @@ -40,6 +42,17 @@ const Impersonation = (props: ImpersonationProps) => {
display_name: me?.displayName
})

// this effect is used to impersonate the user if the impersonated user is stored in the local storage after the window was closed
useEffect(() => {
const impersonatedUser = localStorage.getItem(IMPERSONATED_USER)
if (impersonatedUser) {
const user: User = JSON.parse(impersonatedUser)
if (me?.impersonation?.username !== user.username) {
dispatch(impersonate(user))
}
}
}, [dispatch, me?.impersonation?.username])

useEffect(() => {
const fetchAccesses = async () => {
return services.perimeters.getAccesses()
Expand Down

0 comments on commit 96187ee

Please sign in to comment.