2021-04-20 19:13:31 +02:00
|
|
|
import React, { useContext, useEffect } from 'react';
|
2021-03-30 15:14:02 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { Link, useHistory } from 'react-router-dom';
|
|
|
|
import useMediaQuery from '@material-ui/core/useMediaQuery';
|
|
|
|
|
2021-04-23 15:21:24 +02:00
|
|
|
import {
|
|
|
|
List,
|
|
|
|
ListItem,
|
|
|
|
ListItemAvatar,
|
|
|
|
IconButton,
|
|
|
|
Icon,
|
|
|
|
ListItemText,
|
|
|
|
Button,
|
|
|
|
Tooltip,
|
|
|
|
} from '@material-ui/core';
|
|
|
|
import {
|
|
|
|
CREATE_STRATEGY,
|
|
|
|
DELETE_STRATEGY,
|
|
|
|
} from '../../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';
|
|
|
|
|
|
|
|
import { useStyles } from './styles';
|
2021-04-20 19:13:31 +02:00
|
|
|
import AccessContext from '../../../contexts/AccessContext';
|
2021-03-30 15:14:02 +02:00
|
|
|
|
|
|
|
const StrategiesList = ({
|
|
|
|
strategies,
|
|
|
|
fetchStrategies,
|
|
|
|
removeStrategy,
|
|
|
|
deprecateStrategy,
|
|
|
|
reactivateStrategy,
|
|
|
|
}) => {
|
|
|
|
const history = useHistory();
|
|
|
|
const styles = useStyles();
|
|
|
|
const smallScreen = useMediaQuery('(max-width:700px)');
|
2021-04-20 19:13:31 +02:00
|
|
|
const { hasAccess } = useContext(AccessContext);
|
2021-03-30 15:14:02 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchStrategies();
|
2021-04-07 09:04:48 +02:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
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={
|
|
|
|
<Tooltip title="Add new strategy">
|
2021-04-23 15:21:24 +02:00
|
|
|
<IconButton
|
|
|
|
onClick={() =>
|
|
|
|
history.push('/strategies/create')
|
|
|
|
}
|
|
|
|
>
|
2021-03-30 15:14:02 +02:00
|
|
|
<Icon>add</Icon>
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
elseShow={
|
2021-04-23 15:21:24 +02:00
|
|
|
<Button
|
|
|
|
onClick={() => history.push('/strategies/create')}
|
|
|
|
color="primary"
|
|
|
|
variant="contained"
|
|
|
|
>
|
2021-03-30 15:14:02 +02:00
|
|
|
Add new strategy
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const strategyLink = ({ name, deprecated }) => (
|
|
|
|
<Link to={`/strategies/view/${name}`}>
|
|
|
|
<strong>{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>
|
|
|
|
);
|
|
|
|
|
|
|
|
const reactivateButton = strategy => (
|
|
|
|
<Tooltip title="Reactivate activation strategy">
|
|
|
|
<IconButton onClick={() => reactivateStrategy(strategy)}>
|
|
|
|
<Icon>visibility</Icon>
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
|
|
|
|
const deprecateButton = strategy => (
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={strategy.name === 'default'}
|
|
|
|
show={
|
|
|
|
<Tooltip title="You cannot deprecate the default strategy">
|
|
|
|
<div>
|
|
|
|
<IconButton disabled>
|
|
|
|
<Icon>visibility_off</Icon>
|
|
|
|
</IconButton>
|
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
elseShow={
|
|
|
|
<Tooltip title="Deprecate activation strategy">
|
|
|
|
<div>
|
|
|
|
<IconButton onClick={() => deprecateStrategy(strategy)}>
|
|
|
|
<Icon>visibility_off</Icon>
|
|
|
|
</IconButton>
|
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const deleteButton = strategy => (
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={strategy.editable}
|
|
|
|
show={
|
|
|
|
<Tooltip title="Delete strategy">
|
|
|
|
<IconButton onClick={() => removeStrategy(strategy)}>
|
|
|
|
<Icon>delete</Icon>
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
elseShow={
|
|
|
|
<Tooltip title="You cannot delete a built-in strategy">
|
|
|
|
<div>
|
|
|
|
<IconButton disabled>
|
|
|
|
<Icon>delete</Icon>
|
|
|
|
</IconButton>
|
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const strategyList = () =>
|
|
|
|
strategies.map(strategy => (
|
|
|
|
<ListItem
|
|
|
|
key={strategy.name}
|
|
|
|
classes={{
|
|
|
|
root: classnames(styles.listItem, {
|
|
|
|
[styles.deprecated]: strategy.deprecated,
|
|
|
|
}),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ListItemAvatar>
|
2021-04-23 15:21:24 +02:00
|
|
|
<Icon style={{ color: '#0000008a' }}>extension</Icon>
|
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>
|
|
|
|
));
|
|
|
|
|
|
|
|
return (
|
2021-04-23 15:21:24 +02:00
|
|
|
<PageContent
|
|
|
|
headerContent={
|
|
|
|
<HeaderTitle title="Strategies" actions={headerButton()} />
|
|
|
|
}
|
|
|
|
>
|
2021-03-30 15:14:02 +02:00
|
|
|
<List>
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={strategies.length > 0}
|
|
|
|
show={strategyList()}
|
|
|
|
elseShow={<ListItem>No strategies found</ListItem>}
|
|
|
|
/>
|
|
|
|
</List>
|
|
|
|
</PageContent>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
StrategiesList.propTypes = {
|
|
|
|
strategies: PropTypes.array.isRequired,
|
|
|
|
fetchStrategies: PropTypes.func.isRequired,
|
|
|
|
removeStrategy: PropTypes.func.isRequired,
|
|
|
|
deprecateStrategy: PropTypes.func.isRequired,
|
|
|
|
reactivateStrategy: PropTypes.func.isRequired,
|
|
|
|
history: PropTypes.object.isRequired,
|
|
|
|
name: PropTypes.string,
|
|
|
|
deprecated: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default StrategiesList;
|