UNPKG

phx-react

Version:

PHX REACT

59 lines 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateTargetGroup = validateTargetGroup; const config_1 = require("../config"); const constant_1 = require("../constant"); const get_filter_error_message_1 = require("./get-filter-error-message"); 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 = config_1.TARGET_CONFIGS.find((cfg) => cfg.value === item.target); const targetIndex = config_1.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 === constant_1.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: (0, get_filter_error_message_1.getFilterErrorMessage)(radio.value), }; } }); }); return errors; } //# sourceMappingURL=target-group-resolver.js.map