mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-10 01:16:39 +02:00
refactor: show env buttons when missing permission (#1111)
* refactor: disable environment drag-and-drop when missing permission * refactor: show env buttons when missing permission
This commit is contained in:
parent
b4197e54e6
commit
7d909c6b66
@ -4,11 +4,8 @@ import {
|
|||||||
UPDATE_ENVIRONMENT,
|
UPDATE_ENVIRONMENT,
|
||||||
} from 'component/providers/AccessProvider/permissions';
|
} from 'component/providers/AccessProvider/permissions';
|
||||||
import { Edit, Delete } from '@mui/icons-material';
|
import { Edit, Delete } from '@mui/icons-material';
|
||||||
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
|
||||||
import { IconButton, Tooltip } from '@mui/material';
|
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import AccessContext from 'contexts/AccessContext';
|
import { useState } from 'react';
|
||||||
import { useContext, useState } from 'react';
|
|
||||||
import { IEnvironment } from 'interfaces/environments';
|
import { IEnvironment } from 'interfaces/environments';
|
||||||
import { formatUnknownError } from 'utils/formatUnknownError';
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
||||||
import EnvironmentToggleConfirm from '../../EnvironmentToggleConfirm/EnvironmentToggleConfirm';
|
import EnvironmentToggleConfirm from '../../EnvironmentToggleConfirm/EnvironmentToggleConfirm';
|
||||||
@ -17,8 +14,8 @@ import useEnvironmentApi from 'hooks/api/actions/useEnvironmentApi/useEnvironmen
|
|||||||
import useProjectRolePermissions from 'hooks/api/getters/useProjectRolePermissions/useProjectRolePermissions';
|
import useProjectRolePermissions from 'hooks/api/getters/useProjectRolePermissions/useProjectRolePermissions';
|
||||||
import { useEnvironments } from 'hooks/api/getters/useEnvironments/useEnvironments';
|
import { useEnvironments } from 'hooks/api/getters/useEnvironments/useEnvironments';
|
||||||
import useToast from 'hooks/useToast';
|
import useToast from 'hooks/useToast';
|
||||||
import { useId } from 'hooks/useId';
|
|
||||||
import PermissionSwitch from 'component/common/PermissionSwitch/PermissionSwitch';
|
import PermissionSwitch from 'component/common/PermissionSwitch/PermissionSwitch';
|
||||||
|
import PermissionIconButton from 'component/common/PermissionIconButton/PermissionIconButton';
|
||||||
|
|
||||||
interface IEnvironmentTableActionsProps {
|
interface IEnvironmentTableActionsProps {
|
||||||
environment: IEnvironment;
|
environment: IEnvironment;
|
||||||
@ -28,9 +25,6 @@ export const EnvironmentActionCell = ({
|
|||||||
environment,
|
environment,
|
||||||
}: IEnvironmentTableActionsProps) => {
|
}: IEnvironmentTableActionsProps) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { hasAccess } = useContext(AccessContext);
|
|
||||||
const updatePermission = hasAccess(UPDATE_ENVIRONMENT);
|
|
||||||
|
|
||||||
const { setToastApiError, setToastData } = useToast();
|
const { setToastApiError, setToastData } = useToast();
|
||||||
const { refetchEnvironments } = useEnvironments();
|
const { refetchEnvironments } = useEnvironments();
|
||||||
const { refetch: refetchPermissions } = useProjectRolePermissions();
|
const { refetch: refetchPermissions } = useProjectRolePermissions();
|
||||||
@ -95,88 +89,46 @@ export const EnvironmentActionCell = ({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const toggleIconTooltip = environment.enabled
|
|
||||||
? `Disable environment ${environment.name}`
|
|
||||||
: `Enable environment ${environment.name}`;
|
|
||||||
|
|
||||||
const toggleId = useId();
|
|
||||||
const editId = useId();
|
|
||||||
const deleteId = useId();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ActionCell>
|
<ActionCell>
|
||||||
<ConditionallyRender
|
<PermissionSwitch
|
||||||
condition={updatePermission}
|
permission={UPDATE_ENVIRONMENT}
|
||||||
show={
|
checked={environment.enabled}
|
||||||
<>
|
disabled={environment.protected}
|
||||||
<Tooltip title={toggleIconTooltip} arrow>
|
tooltip={
|
||||||
<div id={toggleId}>
|
environment.enabled
|
||||||
<PermissionSwitch
|
? `Disable environment ${environment.name}`
|
||||||
permission={UPDATE_ENVIRONMENT}
|
: `Enable environment ${environment.name}`
|
||||||
checked={environment.enabled}
|
|
||||||
onClick={() => setToggleModal(true)}
|
|
||||||
disabled={environment.protected}
|
|
||||||
inputProps={{ 'aria-labelledby': toggleId }}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</Tooltip>
|
|
||||||
<ActionCell.Divider />
|
|
||||||
</>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<ConditionallyRender
|
|
||||||
condition={updatePermission}
|
|
||||||
show={
|
|
||||||
<Tooltip
|
|
||||||
title={
|
|
||||||
environment.protected
|
|
||||||
? 'You cannot edit protected environment'
|
|
||||||
: 'Edit environment'
|
|
||||||
}
|
|
||||||
arrow
|
|
||||||
>
|
|
||||||
<span id={editId}>
|
|
||||||
<IconButton
|
|
||||||
aria-labelledby={editId}
|
|
||||||
disabled={environment.protected}
|
|
||||||
onClick={() => {
|
|
||||||
navigate(
|
|
||||||
`/environments/${environment.name}`
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
size="large"
|
|
||||||
>
|
|
||||||
<Edit />
|
|
||||||
</IconButton>
|
|
||||||
</span>
|
|
||||||
</Tooltip>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<ConditionallyRender
|
|
||||||
condition={hasAccess(DELETE_ENVIRONMENT)}
|
|
||||||
show={
|
|
||||||
<Tooltip
|
|
||||||
title={
|
|
||||||
environment.protected
|
|
||||||
? 'You cannot delete protected environment'
|
|
||||||
: 'Delete environment'
|
|
||||||
}
|
|
||||||
describeChild
|
|
||||||
arrow
|
|
||||||
>
|
|
||||||
<span id={deleteId}>
|
|
||||||
<IconButton
|
|
||||||
aria-labelledby={deleteId}
|
|
||||||
disabled={environment.protected}
|
|
||||||
onClick={() => setDeleteModal(true)}
|
|
||||||
size="large"
|
|
||||||
>
|
|
||||||
<Delete />
|
|
||||||
</IconButton>
|
|
||||||
</span>
|
|
||||||
</Tooltip>
|
|
||||||
}
|
}
|
||||||
|
onClick={() => setToggleModal(true)}
|
||||||
/>
|
/>
|
||||||
|
<ActionCell.Divider />
|
||||||
|
<PermissionIconButton
|
||||||
|
permission={UPDATE_ENVIRONMENT}
|
||||||
|
disabled={environment.protected}
|
||||||
|
size="large"
|
||||||
|
tooltipProps={{
|
||||||
|
title: environment.protected
|
||||||
|
? 'You cannot edit protected environment'
|
||||||
|
: 'Edit environment',
|
||||||
|
}}
|
||||||
|
onClick={() => navigate(`/environments/${environment.name}`)}
|
||||||
|
>
|
||||||
|
<Edit />
|
||||||
|
</PermissionIconButton>
|
||||||
|
<PermissionIconButton
|
||||||
|
permission={DELETE_ENVIRONMENT}
|
||||||
|
disabled={environment.protected}
|
||||||
|
size="large"
|
||||||
|
tooltipProps={{
|
||||||
|
title: environment.protected
|
||||||
|
? 'You cannot delete protected environment'
|
||||||
|
: 'Delete environment',
|
||||||
|
}}
|
||||||
|
onClick={() => setDeleteModal(true)}
|
||||||
|
>
|
||||||
|
<Delete />
|
||||||
|
</PermissionIconButton>
|
||||||
<EnvironmentDeleteConfirm
|
<EnvironmentDeleteConfirm
|
||||||
env={environment}
|
env={environment}
|
||||||
setDeldialogue={setDeleteModal}
|
setDeldialogue={setDeleteModal}
|
||||||
|
@ -3,6 +3,9 @@ import { Row } from 'react-table';
|
|||||||
import { TableRow } from '@mui/material';
|
import { TableRow } from '@mui/material';
|
||||||
import { TableCell } from 'component/common/Table';
|
import { TableCell } from 'component/common/Table';
|
||||||
import { useSearchHighlightContext } from 'component/common/Table/SearchHighlightContext/SearchHighlightContext';
|
import { useSearchHighlightContext } from 'component/common/Table/SearchHighlightContext/SearchHighlightContext';
|
||||||
|
import { UPDATE_ENVIRONMENT } from 'component/providers/AccessProvider/permissions';
|
||||||
|
import AccessContext from 'contexts/AccessContext';
|
||||||
|
import { useContext } from 'react';
|
||||||
|
|
||||||
interface IEnvironmentRowProps {
|
interface IEnvironmentRowProps {
|
||||||
row: Row;
|
row: Row;
|
||||||
@ -10,9 +13,10 @@ interface IEnvironmentRowProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const EnvironmentRow = ({ row, moveListItem }: IEnvironmentRowProps) => {
|
export const EnvironmentRow = ({ row, moveListItem }: IEnvironmentRowProps) => {
|
||||||
|
const { hasAccess } = useContext(AccessContext);
|
||||||
const dragItemRef = useDragItem(row.index, moveListItem);
|
const dragItemRef = useDragItem(row.index, moveListItem);
|
||||||
const { searchQuery } = useSearchHighlightContext();
|
const { searchQuery } = useSearchHighlightContext();
|
||||||
const draggable = !searchQuery;
|
const draggable = !searchQuery && hasAccess(UPDATE_ENVIRONMENT);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TableRow hover ref={draggable ? dragItemRef : undefined}>
|
<TableRow hover ref={draggable ? dragItemRef : undefined}>
|
||||||
|
Loading…
Reference in New Issue
Block a user