Skip to content

Commit

Permalink
tweak role switcher a little
Browse files Browse the repository at this point in the history
  • Loading branch information
nl0 committed Jun 5, 2024
1 parent 6e9cc87 commit 8f3c663
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions catalog/app/containers/NavBar/NavMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,22 @@ const useListItemTextStyles = M.makeStyles((t) => ({
},
}))

const LOADING = Symbol('loading')

Check warning on line 171 in catalog/app/containers/NavBar/NavMenu.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/NavBar/NavMenu.tsx#L171

Added line #L171 was not covered by tests

interface RoleSwitcherProps {
user: Me
close: Dialogs.Close
}

function RoleSwitcher({ user }: RoleSwitcherProps) {
function RoleSwitcher({ user, close }: RoleSwitcherProps) {
const switchRole = GQL.useMutation(SWITCH_ROLE_MUTATION)
const classes = useRoleSwitcherStyles()
const textClasses = useListItemTextStyles()
const loading = true
const [state, setState] = React.useState<Error | typeof loading | null>(null)
const [state, setState] = React.useState<Error | typeof LOADING | null>(null)
const handleClick = React.useCallback(
async (roleName: string) => {

Check warning on line 184 in catalog/app/containers/NavBar/NavMenu.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/NavBar/NavMenu.tsx#L178-L184

Added lines #L178 - L184 were not covered by tests
setState(loading)
if (roleName === user.role.name) return close()
setState(LOADING)

Check warning on line 186 in catalog/app/containers/NavBar/NavMenu.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/NavBar/NavMenu.tsx#L186

Added line #L186 was not covered by tests
try {
const { switchRole: r } = await switchRole({ roleName })

Check warning on line 188 in catalog/app/containers/NavBar/NavMenu.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/NavBar/NavMenu.tsx#L188

Added line #L188 was not covered by tests
switch (r.__typename) {
Expand All @@ -203,12 +206,12 @@ function RoleSwitcher({ user }: RoleSwitcherProps) {
}
}
},
[loading, switchRole],
[close, switchRole, user.role.name],
)
return (

Check warning on line 211 in catalog/app/containers/NavBar/NavMenu.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/NavBar/NavMenu.tsx#L211

Added line #L211 was not covered by tests
<>
<M.DialogTitle>Switch role</M.DialogTitle>
{state !== true ? (
{state !== LOADING ? (
<>

Check warning on line 215 in catalog/app/containers/NavBar/NavMenu.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/NavBar/NavMenu.tsx#L215

Added line #L215 was not covered by tests
{state instanceof Error && (
<Lab.Alert severity="error">{state.message}</Lab.Alert>

Check warning on line 217 in catalog/app/containers/NavBar/NavMenu.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/NavBar/NavMenu.tsx#L217

Added line #L217 was not covered by tests
Expand All @@ -217,11 +220,17 @@ function RoleSwitcher({ user }: RoleSwitcherProps) {
{user.roles.map((role) => (

Check warning on line 220 in catalog/app/containers/NavBar/NavMenu.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/NavBar/NavMenu.tsx#L220

Added line #L220 was not covered by tests
<M.ListItem
button
disabled={role.name === user.role.name}
key={role.name}
onClick={() => handleClick(role.name)}

Check warning on line 224 in catalog/app/containers/NavBar/NavMenu.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/NavBar/NavMenu.tsx#L224

Added line #L224 was not covered by tests
selected={role.name === user.role.name}
>
<M.ListItemIcon>
<M.Radio
checked={role.name === user.role.name}
tabIndex={-1}
disableRipple
/>
</M.ListItemIcon>
<M.ListItemText classes={textClasses}>{role.name}</M.ListItemText>
</M.ListItem>
))}
Expand All @@ -244,7 +253,10 @@ const SWITCH_ROLES_DIALOG_PROPS = {
function useRoleSwitcher() {
const openDialog = Dialogs.use()
return (user: Me) =>
openDialog(() => <RoleSwitcher user={user} />, SWITCH_ROLES_DIALOG_PROPS)
openDialog(
({ close }) => <RoleSwitcher user={user} close={close} />,

Check warning on line 257 in catalog/app/containers/NavBar/NavMenu.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/NavBar/NavMenu.tsx#L253-L257

Added lines #L253 - L257 were not covered by tests
SWITCH_ROLES_DIALOG_PROPS,
)
}

function useLinks(): ItemDescriptor[] {
Expand Down

0 comments on commit 8f3c663

Please sign in to comment.