Skip to content

Commit

Permalink
feat: constraints values limit in a strategy UI (#7557)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Jul 8, 2024
1 parent 8f8ff13 commit 46b1eed
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
9 changes: 5 additions & 4 deletions frontend/src/component/common/Limit/Limit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { ConditionallyRender } from '../ConditionallyRender/ConditionallyRender'
const StyledBox = styled(Box)(({ theme }) => ({
display: 'flex',
flexDirection: 'column',
border: `2px solid ${theme.palette.background.application}`,
backgroundColor: theme.palette.background.default,
border: `1px solid ${theme.palette.divider}`,
borderRadius: `${theme.shape.borderRadiusMedium}px`,
width: '100%',
}));
Expand All @@ -33,7 +34,7 @@ const Header = styled(Box)(({ theme }) => ({
gap: theme.spacing(1),
alignItems: 'center',
fontWeight: 'bold',
borderBottom: `1px solid ${theme.palette.background.application}`,
borderBottom: `1px solid ${theme.palette.divider}`,
padding: theme.spacing(3, 4),
fontSize: theme.typography.h2.fontSize,
}));
Expand All @@ -43,7 +44,7 @@ const Footer = styled(Box)(({ theme }) => ({
}));

const Main = styled(Box)(({ theme }) => ({
borderBottom: `1px solid ${theme.palette.background.application}`,
borderBottom: `1px solid ${theme.palette.divider}`,
padding: theme.spacing(3, 4),
}));

Expand All @@ -68,7 +69,7 @@ export const Limit: FC<{
currentValue: number;
onClose?: () => void;
}> = ({ name, shortName, limit, currentValue, onClose }) => {
const percentageLimit = Math.round((currentValue / limit) * 100);
const percentageLimit = Math.floor((currentValue / limit) * 100);
const belowLimit = currentValue < limit;
const threshold = 80;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,20 @@ test('should set values', async () => {
expect(errors).toEqual(['']);
expect(values).toEqual(['1', '2', '3']);
});

test('should show limit reached indicator', async () => {
setupApi();
render(
<FreeTextInput
error=''
values={['1', '2', '3']}
setValues={(newValues) => {}}
setError={(newError: string) => {}}
removeValue={() => {}}
/>,
);

await screen.findByText(
'You have reached the limit for single constraint values',
);
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, Chip } from '@mui/material';
import { Box, Button, Chip, styled } from '@mui/material';
import { makeStyles } from 'tss-react/mui';
import Input from 'component/common/Input/Input';
import StringTruncator from 'component/common/StringTruncator/StringTruncator';
Expand All @@ -8,6 +8,8 @@ import { ConstraintFormHeader } from '../ConstraintFormHeader/ConstraintFormHead
import { parseParameterStrings } from 'utils/parseParameter';
import { useUiFlag } from 'hooks/useUiFlag';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { Limit } from 'component/common/Limit/Limit';

interface IFreeTextInputProps {
values: string[];
Expand Down Expand Up @@ -53,6 +55,12 @@ const useStyles = makeStyles()((theme) => ({
valuesContainer: { marginTop: '1rem' },
}));

const LimitContainer = styled(Box)(({ theme }) => ({
'&:has(*)': {
marginTop: theme.spacing(2),
},
}));

const ENTER = 'Enter';

export const FreeTextInput = ({
Expand All @@ -66,6 +74,7 @@ export const FreeTextInput = ({
const { classes: styles } = useStyles();
const resourceLimitsEnabled = useUiFlag('resourceLimits');
const { uiConfig, loading } = useUiConfig();
const constraintValuesLimit = uiConfig.resourceLimits.constraintValues;

const onKeyPress = (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === ENTER) {
Expand All @@ -80,13 +89,12 @@ export const FreeTextInput = ({
...parseParameterStrings(inputValues),
]);
const limitReached = Boolean(
resourceLimitsEnabled &&
newValues.length > uiConfig.resourceLimits.constraintValues,
resourceLimitsEnabled && newValues.length > constraintValuesLimit,
);

if (limitReached) {
setError(
`constraints cannot have more than ${uiConfig.resourceLimits.constraintValues} values`,
`constraints cannot have more than ${constraintValuesLimit} values`,
);
} else if (newValues.length === 0) {
setError('values cannot be empty');
Expand Down Expand Up @@ -139,6 +147,19 @@ export const FreeTextInput = ({
removeValue={removeValue}
/>
</div>
<LimitContainer>
<ConditionallyRender
condition={resourceLimitsEnabled}
show={
<Limit
name='single constraint values'
shortName='values'
currentValue={values.length}
limit={constraintValuesLimit}
/>
}
/>
</LimitContainer>
</>
);
};
Expand Down

0 comments on commit 46b1eed

Please sign in to comment.