2022-02-11 00:08:55 +01:00
|
|
|
import { useContext, useState } from 'react';
|
2021-03-30 15:14:02 +02:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { Link, useHistory } from 'react-router-dom';
|
|
|
|
import useMediaQuery from '@material-ui/core/useMediaQuery';
|
2021-11-29 14:29:58 +01:00
|
|
|
import {
|
|
|
|
IconButton,
|
|
|
|
List,
|
|
|
|
ListItem,
|
|
|
|
ListItemAvatar,
|
|
|
|
ListItemText,
|
|
|
|
Tooltip,
|
|
|
|
} from '@material-ui/core';
|
|
|
|
import {
|
|
|
|
Add,
|
|
|
|
Delete,
|
|
|
|
Extension,
|
|
|
|
Visibility,
|
|
|
|
VisibilityOff,
|
|
|
|
} from '@material-ui/icons';
|
|
|
|
import {
|
|
|
|
CREATE_STRATEGY,
|
|
|
|
DELETE_STRATEGY,
|
|
|
|
UPDATE_STRATEGY,
|
|
|
|
} from '../../providers/AccessProvider/permissions';
|
2021-03-30 15:14:02 +02:00
|
|
|
import ConditionallyRender from '../../common/ConditionallyRender/ConditionallyRender';
|
|
|
|
import PageContent from '../../common/PageContent/PageContent';
|
|
|
|
import HeaderTitle from '../../common/HeaderTitle';
|
2022-02-11 00:08:55 +01:00
|
|
|
import { useStyles } from './StrategiesList.styles';
|
2021-04-20 19:13:31 +02:00
|
|
|
import AccessContext from '../../../contexts/AccessContext';
|
2021-05-10 13:22:22 +02:00
|
|
|
import Dialogue from '../../common/Dialogue';
|
2021-09-30 11:44:30 +02:00
|
|
|
import { ADD_NEW_STRATEGY_ID } from '../../../testIds';
|
2021-10-21 13:25:39 +02:00
|
|
|
import PermissionIconButton from '../../common/PermissionIconButton/PermissionIconButton';
|
|
|
|
import PermissionButton from '../../common/PermissionButton/PermissionButton';
|
2021-11-29 14:29:58 +01:00
|
|
|
import { getHumanReadableStrategyName } from '../../../utils/strategy-names';
|
2022-02-11 00:08:55 +01:00
|
|
|
import useStrategies from '../../../hooks/api/getters/useStrategies/useStrategies';
|
|
|
|
import useStrategiesApi from '../../../hooks/api/actions/useStrategiesApi/useStrategiesApi';
|
|
|
|
import useToast from '../../../hooks/useToast';
|
|
|
|
import { IStrategy } from '../../../interfaces/strategy';
|
|
|
|
|
|
|
|
interface IDialogueMetaData {
|
|
|
|
show: boolean;
|
|
|
|
title: string;
|
|
|
|
onConfirm: () => void;
|
|
|
|
}
|
2021-03-30 15:14:02 +02:00
|
|
|
|
2022-02-11 00:08:55 +01:00
|
|
|
export const StrategiesList = () => {
|
2021-03-30 15:14:02 +02:00
|
|
|
const history = useHistory();
|
|
|
|
const styles = useStyles();
|
|
|
|
const smallScreen = useMediaQuery('(max-width:700px)');
|
2021-04-20 19:13:31 +02:00
|
|
|
const { hasAccess } = useContext(AccessContext);
|
2022-02-11 00:08:55 +01:00
|
|
|
const [dialogueMetaData, setDialogueMetaData] = useState<IDialogueMetaData>(
|
|
|
|
{ show: false, title: '', onConfirm: () => {} }
|
|
|
|
);
|
|
|
|
const { strategies, refetchStrategies } = useStrategies();
|
|
|
|
const { removeStrategy, deprecateStrategy, reactivateStrategy } =
|
|
|
|
useStrategiesApi();
|
|
|
|
const { setToastData, setToastApiError } = useToast();
|
2021-03-30 15:14:02 +02:00
|
|
|
|
|
|
|
const headerButton = () => (
|
|
|
|
<ConditionallyRender
|
2021-04-20 19:13:31 +02:00
|
|
|
condition={hasAccess(CREATE_STRATEGY)}
|
2021-03-30 15:14:02 +02:00
|
|
|
show={
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={smallScreen}
|
|
|
|
show={
|
2021-11-29 14:29:58 +01:00
|
|
|
<PermissionIconButton
|
|
|
|
data-test={ADD_NEW_STRATEGY_ID}
|
|
|
|
onClick={() => history.push('/strategies/create')}
|
|
|
|
permission={CREATE_STRATEGY}
|
|
|
|
tooltip={'Add new strategy'}
|
|
|
|
>
|
|
|
|
<Add />
|
|
|
|
</PermissionIconButton>
|
2021-03-30 15:14:02 +02:00
|
|
|
}
|
|
|
|
elseShow={
|
2021-10-21 13:25:39 +02:00
|
|
|
<PermissionButton
|
2021-04-23 15:21:24 +02:00
|
|
|
onClick={() => history.push('/strategies/create')}
|
|
|
|
color="primary"
|
2021-10-21 13:25:39 +02:00
|
|
|
permission={CREATE_STRATEGY}
|
2021-09-30 11:44:30 +02:00
|
|
|
data-test={ADD_NEW_STRATEGY_ID}
|
2021-10-21 13:25:39 +02:00
|
|
|
tooltip={'Add new strategy'}
|
2021-04-23 15:21:24 +02:00
|
|
|
>
|
2021-03-30 15:14:02 +02:00
|
|
|
Add new strategy
|
2021-10-21 13:25:39 +02:00
|
|
|
</PermissionButton>
|
2021-03-30 15:14:02 +02:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const strategyLink = ({ name, deprecated }) => (
|
|
|
|
<Link to={`/strategies/view/${name}`}>
|
2021-11-29 14:29:58 +01:00
|
|
|
<strong>{getHumanReadableStrategyName(name)}</strong>
|
2021-04-23 15:21:24 +02:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={deprecated}
|
|
|
|
show={<small> (Deprecated)</small>}
|
|
|
|
/>
|
2021-03-30 15:14:02 +02:00
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
|
2022-02-11 00:08:55 +01:00
|
|
|
const onReactivateStrategy = (strategy: IStrategy) => {
|
|
|
|
setDialogueMetaData({
|
|
|
|
show: true,
|
|
|
|
title: 'Really reactivate strategy?',
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await reactivateStrategy(strategy);
|
|
|
|
refetchStrategies();
|
|
|
|
setToastData({
|
|
|
|
type: 'success',
|
|
|
|
title: 'Success',
|
|
|
|
text: 'Strategy reactivated successfully',
|
|
|
|
});
|
|
|
|
} catch (e: any) {
|
|
|
|
setToastApiError(e.toString());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDeprecateStrategy = (strategy: IStrategy) => {
|
|
|
|
setDialogueMetaData({
|
|
|
|
show: true,
|
|
|
|
title: 'Really deprecate strategy?',
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await deprecateStrategy(strategy);
|
|
|
|
refetchStrategies();
|
|
|
|
setToastData({
|
|
|
|
type: 'success',
|
|
|
|
title: 'Success',
|
|
|
|
text: 'Strategy deprecated successfully',
|
|
|
|
});
|
|
|
|
} catch (e: any) {
|
|
|
|
setToastApiError(e.toString());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDeleteStrategy = (strategy: IStrategy) => {
|
|
|
|
setDialogueMetaData({
|
|
|
|
show: true,
|
|
|
|
title: 'Really delete strategy?',
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await removeStrategy(strategy);
|
|
|
|
refetchStrategies();
|
|
|
|
setToastData({
|
|
|
|
type: 'success',
|
|
|
|
title: 'Success',
|
|
|
|
text: 'Strategy deleted successfully',
|
|
|
|
});
|
|
|
|
} catch (e: any) {
|
|
|
|
setToastApiError(e.toString());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const reactivateButton = (strategy: IStrategy) => (
|
2021-03-30 15:14:02 +02:00
|
|
|
<Tooltip title="Reactivate activation strategy">
|
2021-10-21 13:25:39 +02:00
|
|
|
<PermissionIconButton
|
2022-02-11 00:08:55 +01:00
|
|
|
onClick={() => onReactivateStrategy(strategy)}
|
2021-10-21 13:25:39 +02:00
|
|
|
permission={UPDATE_STRATEGY}
|
|
|
|
tooltip={'Reactivate activation strategy'}
|
2021-11-29 14:29:58 +01:00
|
|
|
>
|
|
|
|
<VisibilityOff />
|
|
|
|
</PermissionIconButton>
|
2021-03-30 15:14:02 +02:00
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
|
2022-02-11 00:08:55 +01:00
|
|
|
const deprecateButton = (strategy: IStrategy) => (
|
2021-03-30 15:14:02 +02:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={strategy.name === 'default'}
|
|
|
|
show={
|
|
|
|
<Tooltip title="You cannot deprecate the default strategy">
|
|
|
|
<div>
|
|
|
|
<IconButton disabled>
|
2021-10-21 13:25:39 +02:00
|
|
|
<Visibility />
|
2021-03-30 15:14:02 +02:00
|
|
|
</IconButton>
|
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
elseShow={
|
2021-10-21 13:25:39 +02:00
|
|
|
<div>
|
|
|
|
<PermissionIconButton
|
2022-02-11 00:08:55 +01:00
|
|
|
onClick={() => onDeprecateStrategy(strategy)}
|
2021-10-21 13:25:39 +02:00
|
|
|
permission={UPDATE_STRATEGY}
|
|
|
|
tooltip={'Deprecate activation strategy'}
|
|
|
|
>
|
|
|
|
<Visibility />
|
|
|
|
</PermissionIconButton>
|
|
|
|
</div>
|
2021-03-30 15:14:02 +02:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2022-02-11 00:08:55 +01:00
|
|
|
const deleteButton = (strategy: IStrategy) => (
|
2021-03-30 15:14:02 +02:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={strategy.editable}
|
|
|
|
show={
|
2021-11-29 14:29:58 +01:00
|
|
|
<PermissionIconButton
|
2022-02-11 00:08:55 +01:00
|
|
|
onClick={() => onDeleteStrategy(strategy)}
|
2021-11-29 14:29:58 +01:00
|
|
|
permission={DELETE_STRATEGY}
|
|
|
|
tooltip={'Delete strategy'}
|
|
|
|
>
|
|
|
|
<Delete />
|
|
|
|
</PermissionIconButton>
|
2021-03-30 15:14:02 +02:00
|
|
|
}
|
|
|
|
elseShow={
|
|
|
|
<Tooltip title="You cannot delete a built-in strategy">
|
|
|
|
<div>
|
|
|
|
<IconButton disabled>
|
2021-06-29 10:21:54 +02:00
|
|
|
<Delete />
|
2021-03-30 15:14:02 +02:00
|
|
|
</IconButton>
|
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const strategyList = () =>
|
|
|
|
strategies.map(strategy => (
|
|
|
|
<ListItem
|
|
|
|
key={strategy.name}
|
|
|
|
classes={{
|
|
|
|
root: classnames(styles.listItem, {
|
|
|
|
[styles.deprecated]: strategy.deprecated,
|
|
|
|
}),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ListItemAvatar>
|
2021-06-29 10:21:54 +02:00
|
|
|
<Extension style={{ color: '#0000008a' }} />
|
2021-03-30 15:14:02 +02:00
|
|
|
</ListItemAvatar>
|
2021-04-23 15:21:24 +02:00
|
|
|
<ListItemText
|
|
|
|
primary={strategyLink(strategy)}
|
|
|
|
secondary={strategy.description}
|
|
|
|
/>
|
2021-03-30 15:14:02 +02:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={strategy.deprecated}
|
|
|
|
show={reactivateButton(strategy)}
|
|
|
|
elseShow={deprecateButton(strategy)}
|
|
|
|
/>
|
2021-04-23 15:21:24 +02:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={hasAccess(DELETE_STRATEGY)}
|
|
|
|
show={deleteButton(strategy)}
|
|
|
|
/>
|
2021-03-30 15:14:02 +02:00
|
|
|
</ListItem>
|
|
|
|
));
|
|
|
|
|
2021-05-10 13:22:22 +02:00
|
|
|
const onDialogConfirm = () => {
|
|
|
|
dialogueMetaData?.onConfirm();
|
|
|
|
setDialogueMetaData(prev => ({ ...prev, show: false }));
|
|
|
|
};
|
|
|
|
|
2021-03-30 15:14:02 +02:00
|
|
|
return (
|
2021-04-23 15:21:24 +02:00
|
|
|
<PageContent
|
|
|
|
headerContent={
|
|
|
|
<HeaderTitle title="Strategies" actions={headerButton()} />
|
|
|
|
}
|
|
|
|
>
|
2021-05-10 13:22:22 +02:00
|
|
|
<Dialogue
|
|
|
|
open={dialogueMetaData.show}
|
|
|
|
onClick={onDialogConfirm}
|
|
|
|
title={dialogueMetaData?.title}
|
|
|
|
onClose={() =>
|
|
|
|
setDialogueMetaData(prev => ({ ...prev, show: false }))
|
|
|
|
}
|
|
|
|
/>
|
2021-03-30 15:14:02 +02:00
|
|
|
<List>
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={strategies.length > 0}
|
2022-02-11 00:08:55 +01:00
|
|
|
show={<>{strategyList()}</>}
|
2021-03-30 15:14:02 +02:00
|
|
|
elseShow={<ListItem>No strategies found</ListItem>}
|
|
|
|
/>
|
|
|
|
</List>
|
|
|
|
</PageContent>
|
|
|
|
);
|
|
|
|
};
|