1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-01 01:18:10 +02:00
unleash.unleash/frontend/src/hooks/api/getters/useConstraintsValidation/useConstraintsValidation.ts
2023-07-07 14:50:13 +02:00

48 lines
1.5 KiB
TypeScript

import useFeatureApi from 'hooks/api/actions/useFeatureApi/useFeatureApi';
import { useEffect, useState } from 'react';
import { IConstraint } from 'interfaces/strategy';
const isValid = (constraint: IConstraint) => {
const hasValues =
Array.isArray(constraint.values) && constraint.values.length > 0;
const hasValue = Boolean(constraint.value);
return hasValues || hasValue;
};
export const useConstraintsValidation = (
constraints?: IConstraint[]
): boolean => {
// An empty list of constraints is valid. An undefined list is not.
// A non-empty list has to be checked by calling the backend.
const isEmptyList = Boolean(constraints && constraints.length === 0);
const [valid, setValid] = useState(isEmptyList);
const { validateConstraint } = useFeatureApi();
useEffect(() => {
if (!constraints) {
return;
}
const invalidConstraints = constraints.find(item => !isValid(item));
if (invalidConstraints) {
setValid(false);
return;
}
const validationRequests = constraints
.filter(isValid)
.map(constraint => {
return validateConstraint(constraint);
});
Promise.all(validationRequests)
.then(() => setValid(true))
.catch(() => setValid(false));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [JSON.stringify(constraints)]);
return valid;
};