diff --git a/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/FeatureStrategyConstraints2.styles.ts b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/FeatureStrategyConstraints2.styles.ts new file mode 100644 index 0000000000..4793cb0452 --- /dev/null +++ b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/FeatureStrategyConstraints2.styles.ts @@ -0,0 +1,8 @@ +import { makeStyles } from '@material-ui/core/styles'; + +export const useStyles = makeStyles(theme => ({ + container: { + display: 'grid', + gap: '1rem', + }, +})); diff --git a/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/FeatureStrategyConstraints2.tsx b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/FeatureStrategyConstraints2.tsx index f480c107b2..512fdc591c 100644 --- a/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/FeatureStrategyConstraints2.tsx +++ b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/FeatureStrategyConstraints2.tsx @@ -10,6 +10,8 @@ import { } from 'component/providers/AccessProvider/permissions'; import { createEmptyConstraint } from 'component/feature/FeatureStrategy/FeatureStrategyConstraints2/createEmptyConstraint'; import { useWeakMap } from 'hooks/useWeakMap'; +import { objectId } from 'utils/object-id'; +import { useStyles } from 'component/feature/FeatureStrategy/FeatureStrategyConstraints2/FeatureStrategyConstraints2.styles'; interface IFeatureStrategyConstraints2Props { projectId: string; @@ -37,6 +39,7 @@ export const FeatureStrategyConstraints2 = ({ const state = useWeakMap(); const { context } = useUnleashContext(); const { constraints = [] } = strategy; + const styles = useStyles(); const onEdit = (constraint: IConstraint) => { state.set(constraint, { editing: true }); @@ -79,21 +82,12 @@ export const FeatureStrategyConstraints2 = ({ ); }; + if (context.length === 0) { + return null; + } + return ( - <> - {strategy.constraints?.map((constraint, index) => ( - - ))} +
Add constraints - + {strategy.constraints?.map((constraint, index) => ( + + ))} +
); }; diff --git a/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/createEmptyConstraint.ts b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/createEmptyConstraint.ts index e9b050b454..34290cc1b0 100644 --- a/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/createEmptyConstraint.ts +++ b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints2/createEmptyConstraint.ts @@ -4,6 +4,10 @@ import { IConstraint } from 'interfaces/strategy'; export const createEmptyConstraint = ( context: IUnleashContextDefinition[] ): IConstraint => { + if (context.length === 0) { + throw new Error('Expected at least one context definition'); + } + return { contextName: context[0].name, operator: 'IN', diff --git a/frontend/src/utils/object-id.test.ts b/frontend/src/utils/object-id.test.ts new file mode 100644 index 0000000000..25b0a3dc5f --- /dev/null +++ b/frontend/src/utils/object-id.test.ts @@ -0,0 +1,13 @@ +import { objectId } from 'utils/object-id'; + +test('objectId', () => { + const a = {}; + const b = {}; + const c = {}; + expect(objectId(a)).toBeGreaterThan(0); + expect(objectId(b)).toBeGreaterThan(0); + expect(objectId(c)).toBeGreaterThan(0); + expect(objectId(a)).toBe(objectId(a)); + expect(objectId(a)).not.toBe(objectId(b)); + expect(objectId(a)).not.toBe(objectId(c)); +}); diff --git a/frontend/src/utils/object-id.ts b/frontend/src/utils/object-id.ts new file mode 100644 index 0000000000..40c4ffa0d2 --- /dev/null +++ b/frontend/src/utils/object-id.ts @@ -0,0 +1,12 @@ +// Get a unique ID for an object instance. +export const objectId = (value: object): number => { + if (!ids.has(value)) { + id++; + ids.set(value, id); + } + + return ids.get(value)!; +}; + +const ids = new WeakMap(); +let id = 0;