@hisptz/react-ui
Version:
A collection of reusable complex DHIS2 react ui components.
193 lines (162 loc) • 4.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isOrgUnitSelected = isOrgUnitSelected;
exports.sanitizeFilters = exports.sanitizeExpansionPaths = exports.onUserSubX2Units = exports.onUserSubUnitsChange = exports.onUserOrUnitChange = exports.onSelectOrgUnit = exports.onLevelSelect = exports.onGroupSelect = exports.onDeselectOrgUnit = void 0;
var _lodash = require("lodash");
function isOrgUnitSelected(selectedOrgUnits, orgUnit) {
return !(0, _lodash.isEmpty)((0, _lodash.find)(selectedOrgUnits, ["id", orgUnit === null || orgUnit === void 0 ? void 0 : orgUnit.id]));
}
const onSelectOrgUnit = (orgUnit, selectedOrgUnits, _ref) => {
let {
onUpdate,
value
} = _ref;
if (onUpdate) {
onUpdate({ ...value,
orgUnits: [...selectedOrgUnits, {
id: orgUnit === null || orgUnit === void 0 ? void 0 : orgUnit.id,
displayName: orgUnit === null || orgUnit === void 0 ? void 0 : orgUnit.displayName,
path: orgUnit === null || orgUnit === void 0 ? void 0 : orgUnit.path
}]
});
}
};
exports.onSelectOrgUnit = onSelectOrgUnit;
const onDeselectOrgUnit = (orgUnit, selectedOrgUnits, _ref2) => {
let {
onUpdate,
value
} = _ref2;
const updateValue = (0, _lodash.cloneDeep)(selectedOrgUnits);
(0, _lodash.remove)(updateValue, ["id", orgUnit.id]);
if (onUpdate) {
onUpdate({ ...value,
orgUnits: updateValue
});
}
};
exports.onDeselectOrgUnit = onDeselectOrgUnit;
const onLevelSelect = _ref3 => {
let {
onUpdate,
value
} = _ref3;
return _ref4 => {
let {
selected
} = _ref4;
if (onUpdate) {
onUpdate({ ...value,
levels: selected
});
}
};
};
exports.onLevelSelect = onLevelSelect;
const onGroupSelect = _ref5 => {
let {
onUpdate,
value
} = _ref5;
return _ref6 => {
let {
selected
} = _ref6;
if (onUpdate) {
onUpdate({ ...value,
groups: selected
});
}
};
};
exports.onGroupSelect = onGroupSelect;
const onUserOrUnitChange = _ref7 => {
let {
onUpdate,
value
} = _ref7;
return _ref8 => {
let {
checked
} = _ref8;
if (onUpdate) {
onUpdate({ ...value,
userOrgUnit: checked,
orgUnits: [],
levels: [],
groups: []
});
}
};
};
exports.onUserOrUnitChange = onUserOrUnitChange;
const onUserSubUnitsChange = _ref9 => {
let {
onUpdate,
value
} = _ref9;
return _ref10 => {
let {
checked
} = _ref10;
if (onUpdate) {
onUpdate({ ...value,
userSubUnit: checked,
orgUnits: [],
levels: [],
groups: []
});
}
};
};
exports.onUserSubUnitsChange = onUserSubUnitsChange;
const onUserSubX2Units = _ref11 => {
let {
onUpdate,
value
} = _ref11;
return _ref12 => {
let {
checked
} = _ref12;
if (onUpdate) {
onUpdate({ ...value,
userSubX2Unit: checked,
orgUnits: [],
levels: [],
groups: []
});
}
};
};
exports.onUserSubX2Units = onUserSubX2Units;
const sanitizeFilters = filters => {
const sanitizedFilters = filters.map(_ref13 => {
let {
path
} = _ref13;
const newFilter = [];
const splitFilter = (0, _lodash.filter)(path === null || path === void 0 ? void 0 : path.split("/"), path => path !== "");
const count = splitFilter.length;
if (count === 1) {
return path;
}
for (let i = 0; i <= count; i++) {
newFilter.push("/".concat((0, _lodash.take)(splitFilter, i).join("/")));
}
return (0, _lodash.remove)(newFilter, filter => filter !== "/");
});
return (0, _lodash.uniq)((0, _lodash.flattenDeep)((0, _lodash.compact)(sanitizedFilters))).filter(filter => filter !== "");
};
exports.sanitizeFilters = sanitizeFilters;
const sanitizeExpansionPaths = orgUnitPaths => {
return (0, _lodash.uniq)(orgUnitPaths.map(path => {
const orgUnits = path.split("/").filter(value => !(0, _lodash.isEmpty)(value));
return orgUnits.map((orgUnit, index) => {
return "/".concat(orgUnits.slice(0, -(index + 1)).join("/"));
}).filter(value => value !== "/");
}).flat());
};
exports.sanitizeExpansionPaths = sanitizeExpansionPaths;