import { useContext, useState } from 'react'; 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, Edit, Extension, Visibility, VisibilityOff, } from '@material-ui/icons'; import { CREATE_STRATEGY, DELETE_STRATEGY, UPDATE_STRATEGY, } from 'component/providers/AccessProvider/permissions'; import ConditionallyRender from 'component/common/ConditionallyRender/ConditionallyRender'; import PageContent from 'component/common/PageContent/PageContent'; import HeaderTitle from 'component/common/HeaderTitle'; import { useStyles } from './StrategiesList.styles'; import AccessContext from 'contexts/AccessContext'; import Dialogue from 'component/common/Dialogue'; import { ADD_NEW_STRATEGY_ID } from 'utils/testIds'; import PermissionIconButton from 'component/common/PermissionIconButton/PermissionIconButton'; import PermissionButton from 'component/common/PermissionButton/PermissionButton'; import { formatStrategyName } from 'utils/strategyNames'; import { useStrategies } from 'hooks/api/getters/useStrategies/useStrategies'; import useStrategiesApi from 'hooks/api/actions/useStrategiesApi/useStrategiesApi'; import useToast from 'hooks/useToast'; import { formatUnknownError } from 'utils/formatUnknownError'; import { ICustomStrategy } from 'interfaces/strategy'; interface IDialogueMetaData { show: boolean; title: string; onConfirm: () => void; } export const StrategiesList = () => { const history = useHistory(); const styles = useStyles(); const smallScreen = useMediaQuery('(max-width:700px)'); const { hasAccess } = useContext(AccessContext); const [dialogueMetaData, setDialogueMetaData] = useState( { show: false, title: '', onConfirm: () => {}, } ); const { strategies, refetchStrategies } = useStrategies(); const { removeStrategy, deprecateStrategy, reactivateStrategy } = useStrategiesApi(); const { setToastData, setToastApiError } = useToast(); const headerButton = () => ( history.push('/strategies/create')} permission={CREATE_STRATEGY} > } elseShow={ history.push('/strategies/create')} color="primary" permission={CREATE_STRATEGY} data-test={ADD_NEW_STRATEGY_ID} > New strategy } /> } /> ); const strategyLink = (name: string, deprecated: boolean) => ( {formatStrategyName(name)} (Deprecated)} /> ); const onReactivateStrategy = (strategy: ICustomStrategy) => { setDialogueMetaData({ show: true, title: 'Really reactivate strategy?', onConfirm: async () => { try { await reactivateStrategy(strategy); refetchStrategies(); setToastData({ type: 'success', title: 'Success', text: 'Strategy reactivated successfully', }); } catch (error: unknown) { setToastApiError(formatUnknownError(error)); } }, }); }; const onDeprecateStrategy = (strategy: ICustomStrategy) => { setDialogueMetaData({ show: true, title: 'Really deprecate strategy?', onConfirm: async () => { try { await deprecateStrategy(strategy); refetchStrategies(); setToastData({ type: 'success', title: 'Success', text: 'Strategy deprecated successfully', }); } catch (error: unknown) { setToastApiError(formatUnknownError(error)); } }, }); }; const onDeleteStrategy = (strategy: ICustomStrategy) => { setDialogueMetaData({ show: true, title: 'Really delete strategy?', onConfirm: async () => { try { await removeStrategy(strategy); refetchStrategies(); setToastData({ type: 'success', title: 'Success', text: 'Strategy deleted successfully', }); } catch (error: unknown) { setToastApiError(formatUnknownError(error)); } }, }); }; const reactivateButton = (strategy: ICustomStrategy) => (
onReactivateStrategy(strategy)} permission={UPDATE_STRATEGY} >
); const deprecateButton = (strategy: ICustomStrategy) => (
} elseShow={
onDeprecateStrategy(strategy)} permission={UPDATE_STRATEGY} >
} /> ); const editButton = (strategy: ICustomStrategy) => ( history.push(`/strategies/${strategy?.name}/edit`) } permission={UPDATE_STRATEGY} > } elseShow={
} /> ); const deleteButton = (strategy: ICustomStrategy) => ( onDeleteStrategy(strategy)} permission={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} /> ); };