phx-react
Version:
PHX REACT
56 lines • 2.59 kB
JavaScript
import { TARGET_CONFIGS } from '../config';
import { FILTER_BY } from '../constant';
import { getFilterErrorMessage } from './get-filter-error-message';
export function validateTargetGroup({ options, targets, }) {
const { required = true } = options || {};
const errors = {};
if (!required && (!targets || targets.length === 0)) {
return errors;
}
if (required && (!targets || targets.length === 0)) {
errors.target = {
type: 'manual',
message: 'Vui lòng chọn ít nhất 1 loại đối tượng',
};
return errors;
}
targets.forEach((item) => {
var _a, _b, _c;
var _d;
const config = TARGET_CONFIGS.find((cfg) => cfg.value === item.target);
const targetIndex = TARGET_CONFIGS.findIndex((t) => t.value === item.target);
if (!config || targetIndex === -1)
return;
const selectedFilters = (_a = item.filter) !== null && _a !== void 0 ? _a : [];
if (selectedFilters.length === 0) {
(_b = errors.target) !== null && _b !== void 0 ? _b : (errors.target = []);
(_c = (_d = errors.target)[targetIndex]) !== null && _c !== void 0 ? _c : (_d[targetIndex] = {});
errors.target[targetIndex].filter = {
type: 'manual',
message: 'Vui lòng chọn ít nhất 1 bộ lọc',
};
return;
}
config.listRadio.forEach((radio, radioIndex) => {
var _a, _b, _c, _d;
var _e, _f, _g;
const matched = selectedFilters.find((f) => f.filterBy === radio.value);
if (!matched)
return;
if (matched.filterBy === FILTER_BY.ALL)
return;
if (!matched.filterValues || matched.filterValues.length === 0) {
(_a = errors.target) !== null && _a !== void 0 ? _a : (errors.target = []);
(_b = (_e = errors.target)[targetIndex]) !== null && _b !== void 0 ? _b : (_e[targetIndex] = {});
(_c = (_f = errors.target[targetIndex]).filter) !== null && _c !== void 0 ? _c : (_f.filter = []);
(_d = (_g = errors.target[targetIndex].filter)[radioIndex]) !== null && _d !== void 0 ? _d : (_g[radioIndex] = {});
errors.target[targetIndex].filter[radioIndex].filterValues = {
type: 'manual',
message: getFilterErrorMessage(radio.value),
};
}
});
});
return errors;
}
//# sourceMappingURL=target-group-resolver.js.map