UNPKG

@hisptz/react-ui

Version:

A collection of reusable complex DHIS2 react ui components.

157 lines (147 loc) 3.43 kB
import { cloneDeep, compact, filter, find, flattenDeep, isEmpty, remove, take, uniq } from "lodash"; export function isOrgUnitSelected(selectedOrgUnits, orgUnit) { return !isEmpty(find(selectedOrgUnits, ["id", orgUnit === null || orgUnit === void 0 ? void 0 : orgUnit.id])); } export 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 }] }); } }; export const onDeselectOrgUnit = (orgUnit, selectedOrgUnits, _ref2) => { let { onUpdate, value } = _ref2; const updateValue = cloneDeep(selectedOrgUnits); remove(updateValue, ["id", orgUnit.id]); if (onUpdate) { onUpdate({ ...value, orgUnits: updateValue }); } }; export const onLevelSelect = _ref3 => { let { onUpdate, value } = _ref3; return _ref4 => { let { selected } = _ref4; if (onUpdate) { onUpdate({ ...value, levels: selected }); } }; }; export const onGroupSelect = _ref5 => { let { onUpdate, value } = _ref5; return _ref6 => { let { selected } = _ref6; if (onUpdate) { onUpdate({ ...value, groups: selected }); } }; }; export const onUserOrUnitChange = _ref7 => { let { onUpdate, value } = _ref7; return _ref8 => { let { checked } = _ref8; if (onUpdate) { onUpdate({ ...value, userOrgUnit: checked, orgUnits: [], levels: [], groups: [] }); } }; }; export const onUserSubUnitsChange = _ref9 => { let { onUpdate, value } = _ref9; return _ref10 => { let { checked } = _ref10; if (onUpdate) { onUpdate({ ...value, userSubUnit: checked, orgUnits: [], levels: [], groups: [] }); } }; }; export const onUserSubX2Units = _ref11 => { let { onUpdate, value } = _ref11; return _ref12 => { let { checked } = _ref12; if (onUpdate) { onUpdate({ ...value, userSubX2Unit: checked, orgUnits: [], levels: [], groups: [] }); } }; }; export const sanitizeFilters = filters => { const sanitizedFilters = filters.map(_ref13 => { let { path } = _ref13; const newFilter = []; const splitFilter = 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(take(splitFilter, i).join("/"))); } return remove(newFilter, filter => filter !== "/"); }); return uniq(flattenDeep(compact(sanitizedFilters))).filter(filter => filter !== ""); }; export const sanitizeExpansionPaths = orgUnitPaths => { return uniq(orgUnitPaths.map(path => { const orgUnits = path.split("/").filter(value => !isEmpty(value)); return orgUnits.map((orgUnit, index) => { return "/".concat(orgUnits.slice(0, -(index + 1)).join("/")); }).filter(value => value !== "/"); }).flat()); };