1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-06-18 01:18:23 +02:00
unleash.unleash/frontend/src/component/context/ContextList/ContextList.jsx
olav 3959e846e8 refactor: fix misc TS errors (#729)
* refactor: update test deps

* refactor: remove unused ts-expect-error annotations

* refactor: add missing arg and return types

* refactor: the loading prop is optional

* refactor: add missing arg and return types

* reafactor: fix value arg type

* refactor: fix missing array type

* refactor: the parameters field is an array

* refactor: use undefined instead of null in state

* refactor: add missing params type

* refactor: add missing children prop

* refactor: add missing array type

* refactor: add missing React imports

* refactor: use correct IProjectEnvironment type

* refactor: type errors as unknown

* refactor: the index prop is required

* refactor: fix date prop type

* refactor: fix tooltip placement prop type

* refactor: fix environments state type

* refactor: add missing arg types

* refactor: add guard for undefined field

* refactor: fix ChangePassword prop types

* refactor: fix MUI import paths

* refactor: add missing arg type

* refactor: fix showDialog prop type

* refactor: remove unused openUpdateDialog prop

* refactor: add missing non-null assertion

* refactor: remove unused types prop

* refactor: stricten API error handler types

* refactor: add missing undefined check

* refactor: add missing IProject id field

* refactor: fix ConditionallyRender condition prop types

* refactor: remove unused args

* refactor: add AddVariant prop types

* refactor: add types to UIContext

* refactor: fix event arg type

* refactor: add missing default impressionData field

* refactor: fix handleDeleteEnvironment prop args

* refactor: fix IFeatureMetrics field requirements

* refactor: add missing element types to ConditionallyRender

* refactor: remove unused ProjectAccess projectId prop

* refactor: add missing undefined check

* refactor: fix getCreateTogglePath arg type

* refactor: add missing IStrategyPayload import

* refactor: remove unused user arg

* refactor: add missing event arg type

* refactor: add missing style object types

* refactor: improve userApiErrors prop type

* refactor: the Dialogue onClose prop is optional

* refactor: fix the AddonEvents setEventValue prop type
2022-02-25 10:55:39 +01:00

168 lines
6.0 KiB
JavaScript

import PageContent from '../../common/PageContent/PageContent';
import HeaderTitle from '../../common/HeaderTitle';
import ConditionallyRender from '../../common/ConditionallyRender/ConditionallyRender';
import {
CREATE_CONTEXT_FIELD,
DELETE_CONTEXT_FIELD,
UPDATE_CONTEXT_FIELD,
} from '../../providers/AccessProvider/permissions';
import {
Button,
IconButton,
List,
ListItem,
ListItemIcon,
ListItemText,
Tooltip,
useMediaQuery,
} from '@material-ui/core';
import { Add, Album, Delete, Edit } from '@material-ui/icons';
import { useContext, useState } from 'react';
import { Link, useHistory } from 'react-router-dom';
import { useStyles } from './styles';
import ConfirmDialogue from '../../common/Dialogue';
import AccessContext from '../../../contexts/AccessContext';
import useUnleashContext from '../../../hooks/api/getters/useUnleashContext/useUnleashContext';
import useContextsApi from '../../../hooks/api/actions/useContextsApi/useContextsApi';
import useToast from '../../../hooks/useToast';
import { formatUnknownError } from '../../../utils/format-unknown-error';
const ContextList = () => {
const { hasAccess } = useContext(AccessContext);
const [showDelDialogue, setShowDelDialogue] = useState(false);
const smallScreen = useMediaQuery('(max-width:700px)');
const [name, setName] = useState();
const { context, refetch } = useUnleashContext();
const { removeContext } = useContextsApi();
const { setToastData, setToastApiError } = useToast();
const history = useHistory();
const styles = useStyles();
const onDeleteContext = async name => {
try {
await removeContext(name);
refetch();
setToastData({
type: 'success',
title: 'Successfully deleted context',
text: 'Your context is now deleted',
});
} catch (error) {
setToastApiError(formatUnknownError(error));
}
setName(undefined);
setShowDelDialogue(false);
};
const contextList = () =>
context.map(field => (
<ListItem key={field.name} classes={{ root: styles.listItem }}>
<ListItemIcon>
<Album />
</ListItemIcon>
<ListItemText
primary={
<ConditionallyRender
condition={hasAccess(UPDATE_CONTEXT_FIELD)}
show={
<Link to={`/context/edit/${field.name}`}>
<strong>{field.name}</strong>
</Link>
}
elseShow={<strong>{field.name}</strong>}
/>
}
secondary={field.description}
/>
<ConditionallyRender
condition={hasAccess(UPDATE_CONTEXT_FIELD)}
show={
<Tooltip title="Edit context field">
<IconButton
aria-label="edit"
onClick={() =>
history.push(`/context/edit/${field.name}`)
}
>
<Edit />
</IconButton>
</Tooltip>
}
/>
<ConditionallyRender
condition={hasAccess(DELETE_CONTEXT_FIELD)}
show={
<Tooltip title="Delete context field">
<IconButton
aria-label="delete"
onClick={() => {
setName(field.name);
setShowDelDialogue(true);
}}
>
<Delete />
</IconButton>
</Tooltip>
}
/>
</ListItem>
));
const headerButton = () => (
<ConditionallyRender
condition={hasAccess(CREATE_CONTEXT_FIELD)}
show={
<ConditionallyRender
condition={smallScreen}
show={
<Tooltip title="Add context type">
<IconButton
onClick={() => history.push('/context/create')}
>
<Add />
</IconButton>
</Tooltip>
}
elseShow={
<Button
onClick={() => history.push('/context/create')}
color="primary"
variant="contained"
>
Add new context field
</Button>
}
/>
}
/>
);
return (
<PageContent
headerContent={
<HeaderTitle
actions={headerButton()}
title={'Context fields'}
/>
}
>
<List>
<ConditionallyRender
condition={context.length > 0}
show={contextList}
elseShow={<ListItem>No context fields defined</ListItem>}
/>
</List>
<ConfirmDialogue
open={showDelDialogue}
onClick={() => onDeleteContext(name)}
onClose={() => {
setName(undefined);
setShowDelDialogue(false);
}}
title="Really delete context field"
/>
</PageContent>
);
};
export default ContextList;