@lfai/egeria-js-commons
Version:
Common module for storing static data such as key value objects, SVGs, icon mappings, API urls.
71 lines (70 loc) • 3.01 kB
JavaScript
import { PAGE_SIZE_INCREASE_VALUE, QUERY_MIN_LENGTH } from '../../../commons/constants';
import { isStringLonger, isArrayEmpty } from '../../validators';
const formIsValid = (form) => {
var _a, _b;
return ((_a = form.q) === null || _a === void 0 ? void 0 : _a.isValid) && ((_b = form.types) === null || _b === void 0 ? void 0 : _b.isValid);
};
const getQueryParamsPath = (form) => {
return Object.keys(form).map((key) => {
var _a, _b;
switch (key) {
case 'q':
return `${key}=${(_a = form.q) === null || _a === void 0 ? void 0 : _a.value}`;
case 'types':
return `${key}=${(_b = form.types) === null || _b === void 0 ? void 0 : _b.value.join(',')}`;
default:
return `${key}=${form[key]}`;
}
}).join('&');
};
/**
*
* @param searchParams react-router-dom object that contains and object with all
* query params
* @returns a new object with validated query params input for Asset Catalog
*/
const getQueryParams = (searchParams) => {
let data = {};
const params = [];
searchParams.forEach((value, key) => {
params.push({ key: key, value: value });
});
params.forEach((param) => {
switch (param.key) {
case 'q':
data = Object.assign(Object.assign({}, data), { q: param.value });
break;
case 'types':
data = Object.assign(Object.assign({}, data), { types: param.value ? param.value.split(',') : [] });
break;
case 'exactMatch':
data = Object.assign(Object.assign({}, data), { exactMatch: param.value === 'true' });
break;
case 'caseSensitive':
data = Object.assign(Object.assign({}, data), { caseSensitive: param.value === 'true' });
break;
case 'pageSize':
data = Object.assign(Object.assign({}, data), { pageSize: param.value ? parseInt(param.value) : PAGE_SIZE_INCREASE_VALUE });
break;
default:
console.log('UNKOWN_QUERY_PARAM');
break;
}
});
return Object.assign({}, data);
};
const validateQueryAndTypes = (_queryParams, typesData) => {
const typesIntersection = _queryParams.types ?
typesData.map((t) => t.value).filter((value) => _queryParams.types.includes(value)) :
[];
return Object.assign(Object.assign({}, _queryParams), { q: {
value: _queryParams.q || '',
isPristine: _queryParams.q === undefined ? true : false,
isValid: _queryParams.q ? isStringLonger(_queryParams.q, QUERY_MIN_LENGTH) : false
}, types: {
value: typesIntersection,
isPristine: _queryParams.types === undefined ? true : false,
isValid: !isArrayEmpty(typesIntersection)
} });
};
export { formIsValid, getQueryParamsPath, getQueryParams, validateQueryAndTypes };