-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into POLIO-1706-Campaigns-Users-cannot-filter-by-…
…campaign-type
- Loading branch information
Showing
106 changed files
with
1,956 additions
and
1,769 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
hat/assets/js/apps/Iaso/components/nav/AccountSwitch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { | ||
ClickAwayListener, | ||
Grow, | ||
MenuItem, | ||
MenuList, | ||
Paper, | ||
Popper, | ||
Typography, | ||
} from '@mui/material'; | ||
import React, { FunctionComponent, useEffect, useRef, useState } from 'react'; | ||
|
||
import { useSwitchAccount } from '../../hooks/useSwitchAccount'; | ||
import { useCurrentUser } from '../../utils/usersUtils'; | ||
|
||
type Props = { | ||
color?: 'inherit' | 'primary' | 'secondary'; | ||
}; | ||
|
||
export const AccountSwitch: FunctionComponent<Props> = ({ | ||
color = 'inherit', | ||
}) => { | ||
const currentUser = useCurrentUser(); | ||
|
||
const [open, setOpen] = useState(false); | ||
const anchorRef = useRef<HTMLDivElement>(null); | ||
|
||
const { mutateAsync: switchAccount } = useSwitchAccount(() => { | ||
setOpen(false); | ||
window.location.href = '/'; | ||
}); | ||
|
||
const handleToggle = () => { | ||
setOpen(prevOpen => !prevOpen); | ||
}; | ||
|
||
const handleClose = (event: Event | React.SyntheticEvent) => { | ||
if ( | ||
anchorRef.current && | ||
anchorRef.current.contains(event.target as HTMLElement) | ||
) { | ||
return; | ||
} | ||
setOpen(false); | ||
}; | ||
|
||
function handleListKeyDown(event: React.KeyboardEvent) { | ||
if (event.key === 'Tab') { | ||
event.preventDefault(); | ||
setOpen(false); | ||
} else if (event.key === 'Escape') { | ||
setOpen(false); | ||
} | ||
} | ||
|
||
// Return focus to the button when we transitioned from !open -> open | ||
const prevOpen = useRef(open); | ||
useEffect(() => { | ||
if (prevOpen.current === true && open === false) { | ||
anchorRef.current?.focus(); | ||
} | ||
prevOpen.current = open; | ||
}, [open]); | ||
const menuListKeyDownHandler = React.useCallback(handleListKeyDown, []); | ||
|
||
if (currentUser.other_accounts.length === 0) { | ||
return ( | ||
<Typography | ||
variant="body2" | ||
color={color} | ||
sx={{ | ||
padding: theme => theme.spacing(0), | ||
fontSize: 16, | ||
}} | ||
> | ||
{currentUser.account.name} | ||
</Typography> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
<Typography | ||
ref={anchorRef} | ||
variant="body2" | ||
color={color} | ||
onClick={handleToggle} | ||
sx={{ | ||
padding: theme => theme.spacing(0), | ||
cursor: 'pointer', | ||
fontSize: 16, | ||
'&:hover': { | ||
color: theme => theme.palette.secondary.main, | ||
}, | ||
}} | ||
aria-controls={open ? 'account-menu' : undefined} | ||
aria-expanded={open ? 'true' : undefined} | ||
aria-haspopup="true" | ||
> | ||
{currentUser.account.name} | ||
</Typography> | ||
<Popper | ||
open={open} | ||
anchorEl={anchorRef.current} | ||
role={undefined} | ||
placement="bottom-end" | ||
transition | ||
disablePortal | ||
> | ||
{({ TransitionProps }) => ( | ||
<Grow | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...TransitionProps} | ||
style={{ | ||
transformOrigin: 'right bottom', | ||
}} | ||
> | ||
<Paper> | ||
<ClickAwayListener onClickAway={handleClose}> | ||
<MenuList | ||
autoFocusItem={open} | ||
id="account-menu" | ||
aria-labelledby="account-button" | ||
onKeyDown={menuListKeyDownHandler} | ||
> | ||
{currentUser.other_accounts.map(account => ( | ||
<MenuItem | ||
key={account.id} | ||
selected={ | ||
account.id === | ||
currentUser.account.id | ||
} | ||
onClick={() => | ||
switchAccount(account.id) | ||
} | ||
> | ||
{account.name} | ||
</MenuItem> | ||
))} | ||
</MenuList> | ||
</ClickAwayListener> | ||
</Paper> | ||
</Grow> | ||
)} | ||
</Popper> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.