import { useContext, useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { Link, useHistory } from 'react-router-dom'; import useMediaQuery from '@material-ui/core/useMediaQuery'; 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'; import ConditionallyRender from '../../common/ConditionallyRender/ConditionallyRender'; import PageContent from '../../common/PageContent/PageContent'; import HeaderTitle from '../../common/HeaderTitle'; import { useStyles } from './styles'; import AccessContext from '../../../contexts/AccessContext'; import Dialogue from '../../common/Dialogue'; import { ADD_NEW_STRATEGY_ID } from '../../../testIds'; import PermissionIconButton from '../../common/PermissionIconButton/PermissionIconButton'; import PermissionButton from '../../common/PermissionButton/PermissionButton'; import { getHumanReadableStrategyName } from '../../../utils/strategy-names'; const StrategiesList = ({ strategies, fetchStrategies, removeStrategy, deprecateStrategy, reactivateStrategy, }) => { const history = useHistory(); const styles = useStyles(); const smallScreen = useMediaQuery('(max-width:700px)'); const { hasAccess } = useContext(AccessContext); const [dialogueMetaData, setDialogueMetaData] = useState({ show: false }); useEffect(() => { fetchStrategies(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const headerButton = () => ( history.push('/strategies/create')} permission={CREATE_STRATEGY} tooltip={'Add new strategy'} > } elseShow={ history.push('/strategies/create')} color="primary" permission={CREATE_STRATEGY} variant="contained" data-test={ADD_NEW_STRATEGY_ID} tooltip={'Add new strategy'} > Add new strategy } /> } /> ); const strategyLink = ({ name, deprecated }) => ( {getHumanReadableStrategyName(name)} (Deprecated)} /> ); const reactivateButton = strategy => ( setDialogueMetaData({ show: true, title: 'Really reactivate strategy?', onConfirm: () => reactivateStrategy(strategy), }) } permission={UPDATE_STRATEGY} tooltip={'Reactivate activation strategy'} > ); const deprecateButton = strategy => (
} elseShow={
setDialogueMetaData({ show: true, title: 'Really deprecate strategy?', onConfirm: () => deprecateStrategy(strategy), }) } permission={UPDATE_STRATEGY} tooltip={'Deprecate activation strategy'} >
} /> ); const deleteButton = strategy => ( setDialogueMetaData({ show: true, title: 'Really delete strategy?', onConfirm: () => removeStrategy(strategy), }) } permission={DELETE_STRATEGY} tooltip={'Delete strategy'} > } elseShow={
} /> ); const strategyList = () => strategies.map(strategy => ( )); const onDialogConfirm = () => { dialogueMetaData?.onConfirm(); setDialogueMetaData(prev => ({ ...prev, show: false })); }; return ( } > setDialogueMetaData(prev => ({ ...prev, show: false })) } /> 0} show={strategyList()} elseShow={No strategies found} /> ); }; 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;