From d2521312b85722168493461863bb8a8cf8b93ad4 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 10 Jun 2025 12:50:44 +0200 Subject: [PATCH] delete restricted legal values selector --- .../RestrictiveLegalValues.test.tsx | 119 -------- .../RestrictiveLegalValues.tsx | 256 ------------------ 2 files changed, 375 deletions(-) delete mode 100644 frontend/src/component/common/NewConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/RestrictiveLegalValues/RestrictiveLegalValues.test.tsx delete mode 100644 frontend/src/component/common/NewConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/RestrictiveLegalValues/RestrictiveLegalValues.tsx diff --git a/frontend/src/component/common/NewConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/RestrictiveLegalValues/RestrictiveLegalValues.test.tsx b/frontend/src/component/common/NewConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/RestrictiveLegalValues/RestrictiveLegalValues.test.tsx deleted file mode 100644 index c3af35c02c..0000000000 --- a/frontend/src/component/common/NewConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/RestrictiveLegalValues/RestrictiveLegalValues.test.tsx +++ /dev/null @@ -1,119 +0,0 @@ -import { render } from 'utils/testRenderer'; -import { fireEvent, screen } from '@testing-library/react'; -import { RestrictiveLegalValues } from './RestrictiveLegalValues.tsx'; -import { vi } from 'vitest'; - -vi.mock('../../../../../../hooks/useUiFlag', () => ({ - useUiFlag: vi.fn( - (flag: string) => flag !== 'disableShowContextFieldSelectionValues', - ), -})); - -test('should show alert when you have illegal legal values', async () => { - const contextDefinitionValues = [{ value: 'value1' }, { value: 'value2' }]; - const fixedValues = ['value1', 'value2']; - const localValues = ['value1', 'value2']; - const deletedLegalValues = [{ value: 'value1' }]; - - render( - {}} - setValuesWithRecord={() => {}} - error={''} - setError={() => {}} - />, - ); - - await screen.findByText( - 'This constraint is currently using values that were valid in the past but have since been deleted. If you save changes on this constraint and then save the strategy the following values will be removed:', - ); -}); - -test('Should remove illegal legal values from internal value state when mounting', () => { - const contextDefinitionValues = [{ value: 'value1' }, { value: 'value2' }]; - const fixedValues = ['value1', 'value2']; - let localValues = ['value1', 'value2']; - const deletedLegalValues = [{ value: 'value1' }]; - - const setValues = (values: string[]) => { - localValues = values; - }; - - render( - {}} - error={''} - setError={() => {}} - />, - ); - - expect(localValues).toEqual(['value2']); -}); - -test('Should select all', async () => { - const contextDefinitionValues = [{ value: 'value1' }, { value: 'value2' }]; - let localValues: string[] = []; - - const setValuesWithRecord = (values: string[]) => { - localValues = values; - }; - - render( - {}} - setValuesWithRecord={setValuesWithRecord} - error={''} - setError={() => {}} - />, - ); - - const selectedAllButton = await screen.findByText(/Select all/i); - - fireEvent.click(selectedAllButton); - expect(localValues).toEqual(['value1', 'value2']); -}); - -test('Should unselect all', async () => { - const contextDefinitionValues = [{ value: 'value1' }, { value: 'value2' }]; - let localValues: string[] = ['value1', 'value2']; - - const setValuesWithRecord = (values: string[]) => { - localValues = values; - }; - - render( - {}} - setValuesWithRecord={setValuesWithRecord} - error={''} - setError={() => {}} - />, - ); - - const selectedAllButton = await screen.findByText(/Unselect all/i); - - fireEvent.click(selectedAllButton); - expect(localValues).toEqual([]); -}); diff --git a/frontend/src/component/common/NewConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/RestrictiveLegalValues/RestrictiveLegalValues.tsx b/frontend/src/component/common/NewConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/RestrictiveLegalValues/RestrictiveLegalValues.tsx deleted file mode 100644 index 3ee8f3226b..0000000000 --- a/frontend/src/component/common/NewConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/RestrictiveLegalValues/RestrictiveLegalValues.tsx +++ /dev/null @@ -1,256 +0,0 @@ -import { useEffect, useState } from 'react'; -import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; -import { Alert, Button, Checkbox, Chip, Stack, styled } from '@mui/material'; -import { ConstraintValueSearch } from 'component/common/LegacyConstraintAccordion/ConstraintValueSearch/ConstraintValueSearch'; -import { ConstraintFormHeader } from '../ConstraintFormHeader/ConstraintFormHeader.tsx'; -import type { ILegalValue } from 'interfaces/context'; -import { - filterLegalValues, - LegalValueLabel, -} from '../LegalValueLabel/LegalValueLabel.tsx'; -import { useUiFlag } from 'hooks/useUiFlag'; - -interface IRestrictiveLegalValuesProps { - data: { - legalValues: ILegalValue[]; - deletedLegalValues: ILegalValue[]; - }; - constraintValues: string[]; - values: string[]; - setValues: (values: string[]) => void; - setValuesWithRecord: (values: string[]) => void; - beforeValues?: JSX.Element; - error: string; - setError: React.Dispatch>; -} - -interface IValuesMap { - [key: string]: boolean; -} - -const createValuesMap = (values: string[]): IValuesMap => { - return values.reduce((result: IValuesMap, currentValue: string) => { - if (!result[currentValue]) { - result[currentValue] = true; - } - return result; - }, {}); -}; - -export const getLegalValueSet = (values: ILegalValue[]) => { - return new Set(values.map(({ value }) => value)); -}; - -export const getIllegalValues = ( - constraintValues: string[], - deletedLegalValues: ILegalValue[], -) => { - const deletedValuesSet = getLegalValueSet(deletedLegalValues); - - return constraintValues.filter( - (value) => value !== '' && deletedValuesSet.has(value), - ); -}; - -const StyledValuesContainer = styled('div')(({ theme }) => ({ - display: 'grid', - gridTemplateColumns: 'repeat(auto-fit, minmax(120px, 1fr))', - gap: theme.spacing(1), - padding: theme.spacing(2), - border: `1px solid ${theme.palette.divider}`, - borderRadius: theme.shape.borderRadiusMedium, - maxHeight: '378px', - overflow: 'auto', -})); - -const StyledChipList = styled('ul')(({ theme }) => ({ - display: 'flex', - flexWrap: 'wrap', - listStyle: 'none', - gap: theme.spacing(1), - padding: theme.spacing(2), -})); - -const StyledStack = styled(Stack)(({ theme }) => ({ - marginTop: theme.spacing(2), - marginBottom: theme.spacing(0.5), - justifyContent: 'space-between', -})); - -const ErrorText = styled('p')(({ theme }) => ({ - fontSize: theme.fontSizes.smallBody, - color: theme.palette.error.main, -})); - -/** - * @deprecated use `/component/feature/FeatureStrategy/FeatureStrategyConstraints/LegalValuesSelector.tsx` - * Remove with flag `addEditStrategy` - */ -export const RestrictiveLegalValues = ({ - data, - values, - setValues, - setValuesWithRecord, - error, - setError, - constraintValues, -}: IRestrictiveLegalValuesProps) => { - const [filter, setFilter] = useState(''); - const { legalValues, deletedLegalValues } = data; - - const filteredValues = filterLegalValues(legalValues, filter); - - // Lazily initialise the values because there might be a lot of them. - const [valuesMap, setValuesMap] = useState(() => createValuesMap(values)); - - const disableShowContextFieldSelectionValues = useUiFlag( - 'disableShowContextFieldSelectionValues', - ); - - const cleanDeletedLegalValues = (constraintValues: string[]): string[] => { - const deletedValuesSet = getLegalValueSet(deletedLegalValues); - return ( - constraintValues?.filter((value) => !deletedValuesSet.has(value)) || - [] - ); - }; - - const illegalValues = getIllegalValues( - constraintValues, - deletedLegalValues, - ); - - useEffect(() => { - setValuesMap(createValuesMap(values)); - }, [values, setValuesMap, createValuesMap]); - - useEffect(() => { - if (illegalValues.length > 0) { - setValues(cleanDeletedLegalValues(values)); - } - }, []); - - const onChange = (legalValue: string) => { - setError(''); - - if (valuesMap[legalValue]) { - const index = values.findIndex((value) => value === legalValue); - const newValues = [...values]; - newValues.splice(index, 1); - setValuesWithRecord(newValues); - return; - } - - setValuesWithRecord([...cleanDeletedLegalValues(values), legalValue]); - }; - - const isAllSelected = legalValues.every((value) => - values.includes(value.value), - ); - - const onSelectAll = () => { - if (isAllSelected) { - return setValuesWithRecord([]); - } - setValuesWithRecord([ - ...legalValues.map((legalValue) => legalValue.value), - ]); - }; - - const handleSearchKeyDown = (event: React.KeyboardEvent) => { - if (event.key === 'Enter') { - event.preventDefault(); - if (filteredValues.length > 0) { - const firstValue = filteredValues[0].value; - onChange(firstValue); - } - } - }; - - return ( - <> - 0)} - show={ - - This constraint is currently using values that were - valid in the past but have since been deleted. If you - save changes on this constraint and then save the - strategy the following values will be removed: -
    - {illegalValues?.map((value) => ( -
  • {value}
  • - ))} -
-
- } - /> - - - Select values from a predefined set - - - - 100} - show={ - <> - - {values.map((value) => { - return ( -
  • - - onChange(value) - } - /> -
  • - ); - })} - - } - /> - - } - /> -
    - -
    - - {filteredValues.map((match) => ( - onChange(match.value)} - name={match.value} - color='primary' - disabled={deletedLegalValues - .map(({ value }) => value) - .includes(match.value)} - /> - } - /> - ))} - - {error}} - /> - - ); -};