mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-31 13:47:02 +02:00
feat(1-3873)/warn you when adding existing values (#10310)
Makes it so that the constraint value input gives you an error if you try to add one or more values that **all** exist in the set of values already. E.g. if you have `a` and `b`, and try to add `a`, it'll tell you that "`a` has already been added". Likewise, if you try to add `a,b`, it'll tell you that all these values already exist. However, if at least one of the values does not exist, then it will allow you to submit the values (we already do deduplication before storing anyway). The background for this is that a user was confused thinking that just one specific value didn't get added to their constraints. As it turns out, they'd already added the value previously, so when it didn't show up at the end of the list, they thought it didn't work at all. <img width="863" alt="image" src="https://github.com/user-attachments/assets/12195e0a-04bc-4b41-bd44-432120c768a6" /> <img width="816" alt="image" src="https://github.com/user-attachments/assets/433a64d7-aec0-482d-8544-574656c266ce" /> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
parent
8e0e9c834e
commit
37aaf60aa5
@ -154,6 +154,27 @@ describe('validators', () => {
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
||||
test.each(multipleValueOperators)(
|
||||
'multi-value operator %s should reject fully duplicate inputs and accept new values',
|
||||
(operator) => {
|
||||
const initial: IConstraint = {
|
||||
contextName: 'context-field',
|
||||
operator: operator,
|
||||
values: ['a', 'b'],
|
||||
};
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useEditableConstraint(initial, () => {}),
|
||||
);
|
||||
|
||||
checkValidator(result.current.validator, [
|
||||
['a', false],
|
||||
[['a', 'c'], true],
|
||||
[['a', 'b'], false],
|
||||
]);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('legal values', () => {
|
||||
|
@ -4,6 +4,7 @@ import type { IConstraint } from 'interfaces/strategy';
|
||||
import {
|
||||
type EditableConstraint,
|
||||
fromIConstraint,
|
||||
isMultiValueConstraint,
|
||||
isSingleValueConstraint,
|
||||
toIConstraint,
|
||||
} from './editable-constraint-type.ts';
|
||||
@ -16,8 +17,8 @@ import {
|
||||
type ConstraintUpdateAction,
|
||||
} from './constraint-reducer.ts';
|
||||
import {
|
||||
type ConstraintValidationResult,
|
||||
constraintValidator,
|
||||
type ConstraintValidationResult,
|
||||
} from './constraint-validator.ts';
|
||||
import {
|
||||
getDeletedLegalValues,
|
||||
@ -76,7 +77,20 @@ export const useEditableConstraint = (
|
||||
[JSON.stringify(context), localConstraint.contextName],
|
||||
);
|
||||
|
||||
const validator = constraintValidator(localConstraint.operator);
|
||||
const baseValidator = constraintValidator(localConstraint.operator);
|
||||
|
||||
const validator = (...values: string[]) => {
|
||||
if (
|
||||
isMultiValueConstraint(localConstraint) &&
|
||||
values.every((value) => localConstraint.values.has(value))
|
||||
) {
|
||||
if (values.length === 1) {
|
||||
return [false, `${values[0]} is already added.`];
|
||||
}
|
||||
return [false, `All the values are already added`];
|
||||
}
|
||||
return baseValidator(...values);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
@ -104,7 +118,7 @@ export const useEditableConstraint = (
|
||||
isSingleValueConstraint(localConstraint)
|
||||
) {
|
||||
return getInvalidLegalValues(
|
||||
(value) => validator(value)[0],
|
||||
(value) => baseValidator(value)[0],
|
||||
contextDefinition.legalValues,
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user