Merge pull request #1810 from automatisch/AUT-921
fix: disable add connection button for unauthorized users
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import PropTypes from 'prop-types';
|
||||||
import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
|
import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
|
||||||
import Button from '@mui/material/Button';
|
import Button from '@mui/material/Button';
|
||||||
import ButtonGroup from '@mui/material/ButtonGroup';
|
import ButtonGroup from '@mui/material/ButtonGroup';
|
||||||
@@ -9,21 +10,26 @@ import Paper from '@mui/material/Paper';
|
|||||||
import Popper from '@mui/material/Popper';
|
import Popper from '@mui/material/Popper';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
|
|
||||||
export default function SplitButton(props) {
|
export default function SplitButton(props) {
|
||||||
const { options, disabled, defaultActionIndex = 0 } = props;
|
const { options, disabled, defaultActionIndex = 0 } = props;
|
||||||
const [open, setOpen] = React.useState(false);
|
const [open, setOpen] = React.useState(false);
|
||||||
const anchorRef = React.useRef(null);
|
const anchorRef = React.useRef(null);
|
||||||
|
|
||||||
const multiOptions = options.length > 1;
|
const multiOptions = options.length > 1;
|
||||||
const selectedOption = options[defaultActionIndex];
|
const selectedOption = options[defaultActionIndex];
|
||||||
|
|
||||||
const handleToggle = () => {
|
const handleToggle = () => {
|
||||||
setOpen((prevOpen) => !prevOpen);
|
setOpen((prevOpen) => !prevOpen);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleClose = (event) => {
|
const handleClose = (event) => {
|
||||||
if (anchorRef.current && anchorRef.current.contains(event.target)) {
|
if (anchorRef.current && anchorRef.current.contains(event.target)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<ButtonGroup
|
<ButtonGroup
|
||||||
@@ -42,6 +48,7 @@ export default function SplitButton(props) {
|
|||||||
borderRadius: 0,
|
borderRadius: 0,
|
||||||
borderRight: '1px solid #bdbdbd',
|
borderRight: '1px solid #bdbdbd',
|
||||||
}}
|
}}
|
||||||
|
disabled={selectedOption.disabled}
|
||||||
>
|
>
|
||||||
{selectedOption.label}
|
{selectedOption.label}
|
||||||
</Button>
|
</Button>
|
||||||
@@ -80,6 +87,7 @@ export default function SplitButton(props) {
|
|||||||
selected={index === defaultActionIndex}
|
selected={index === defaultActionIndex}
|
||||||
component={Link}
|
component={Link}
|
||||||
to={option.to}
|
to={option.to}
|
||||||
|
disabled={option.disabled}
|
||||||
>
|
>
|
||||||
{option.label}
|
{option.label}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
@@ -94,3 +102,17 @@ export default function SplitButton(props) {
|
|||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SplitButton.propTypes = {
|
||||||
|
options: PropTypes.arrayOf(
|
||||||
|
PropTypes.shape({
|
||||||
|
label: PropTypes.string.isRequired,
|
||||||
|
key: PropTypes.string.isRequired,
|
||||||
|
'data-test': PropTypes.string.isRequired,
|
||||||
|
to: PropTypes.string.isRequired,
|
||||||
|
disabled: PropTypes.bool.isRequired,
|
||||||
|
}).isRequired,
|
||||||
|
).isRequired,
|
||||||
|
disabled: PropTypes.bool,
|
||||||
|
defaultActionIndex: PropTypes.number,
|
||||||
|
};
|
||||||
|
@@ -77,6 +77,7 @@ export default function Application() {
|
|||||||
key: 'addConnection',
|
key: 'addConnection',
|
||||||
'data-test': 'add-connection-button',
|
'data-test': 'add-connection-button',
|
||||||
to: URLS.APP_ADD_CONNECTION(appKey, appConfig?.data?.canConnect),
|
to: URLS.APP_ADD_CONNECTION(appKey, appConfig?.data?.canConnect),
|
||||||
|
disabled: !currentUserAbility.can('create', 'Connection'),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -86,11 +87,12 @@ export default function Application() {
|
|||||||
key: 'addCustomConnection',
|
key: 'addCustomConnection',
|
||||||
'data-test': 'add-custom-connection-button',
|
'data-test': 'add-custom-connection-button',
|
||||||
to: URLS.APP_ADD_CONNECTION(appKey),
|
to: URLS.APP_ADD_CONNECTION(appKey),
|
||||||
|
disabled: !currentUserAbility.can('create', 'Connection'),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
}, [appKey, appConfig?.data]);
|
}, [appKey, appConfig?.data, currentUserAbility]);
|
||||||
|
|
||||||
if (loading) return null;
|
if (loading) return null;
|
||||||
|
|
||||||
@@ -140,9 +142,10 @@ export default function Application() {
|
|||||||
element={
|
element={
|
||||||
<SplitButton
|
<SplitButton
|
||||||
disabled={
|
disabled={
|
||||||
appConfig?.data &&
|
(appConfig?.data &&
|
||||||
!appConfig?.data?.canConnect &&
|
!appConfig?.data?.canConnect &&
|
||||||
!appConfig?.data?.canCustomConnect
|
!appConfig?.data?.canCustomConnect) ||
|
||||||
|
connectionOptions.every(({ disabled }) => disabled)
|
||||||
}
|
}
|
||||||
options={connectionOptions}
|
options={connectionOptions}
|
||||||
/>
|
/>
|
||||||
|
Reference in New Issue
Block a user