UNPKG

@hisptz/react-ui

Version:

A collection of reusable complex DHIS2 react ui components.

48 lines (43 loc) 2.25 kB
import { CustomDataProvider } from "@dhis2/app-runtime"; import { filter, find, flattenDeep, isEmpty, last, uniqBy } from "lodash"; import React from "react"; import orgUnits from "../data/orgUnit.json"; export default function OrgUnitDataProvider(_ref) { let { children } = _ref; const allOrgUnits = uniqBy(flattenDeep(orgUnits.organisationUnitsWithChildren.map(orgUnit => [orgUnit, ...orgUnit.children])), "id"); return /*#__PURE__*/React.createElement(CustomDataProvider, { data: { organisationUnits: async (type, query) => { var _query$params2, _query$params4; if (query.id) { var _query$params; return find(((_query$params = query.params) === null || _query$params === void 0 ? void 0 : _query$params.fields).includes("children::size") ? orgUnits.organisationUnitsChildrenCount : orgUnits.organisationUnitsWithChildren, { id: query.id }); } if ((_query$params2 = query.params) !== null && _query$params2 !== void 0 && _query$params2.filter) { var _last, _query$params3, _query$params3$filter; const keyword = (_last = last(query === null || query === void 0 ? void 0 : (_query$params3 = query.params) === null || _query$params3 === void 0 ? void 0 : (_query$params3$filter = _query$params3.filter) === null || _query$params3$filter === void 0 ? void 0 : _query$params3$filter.split(":"))) !== null && _last !== void 0 ? _last : ""; return { organisationUnits: filter(allOrgUnits, orgUnit => { return !isEmpty("".concat(orgUnit.id, " ").concat(orgUnit.displayName).match(RegExp(keyword.toLowerCase()))); }) }; } if (query !== null && query !== void 0 && (_query$params4 = query.params) !== null && _query$params4 !== void 0 && _query$params4.userDataViewFallback) { return { organisationUnits: orgUnits.rootOrganisationUnits }; } else { return { organisationUnits: [orgUnits.organisationUnitsChildrenCount] }; } }, organisationUnitLevels: orgUnits.organisationUnitLevels, organisationUnitGroups: orgUnits.organisationUnitGroups } }, children); }