UNPKG

@hisptz/react-ui

Version:

A collection of reusable complex DHIS2 react ui components.

48 lines (43 loc) 2.23 kB
import { useDataEngine } from "@dhis2/app-runtime"; import i18n from "@dhis2/d2-i18n"; import { reduce } from "lodash"; import PropTypes from "prop-types"; import React from "react"; import { useGetNumDenMatch } from "../../../Utils/Hooks"; import Error from "../Error/ErrorAPIResult"; import Loader from "../Loaders/Loader"; export default function IndicatorCount(_ref) { var _data$matches; let { dataElementsArray } = _ref; const engine = useDataEngine(); const onlyIds = dataElementsArray === null || dataElementsArray === void 0 ? void 0 : dataElementsArray.map(e => { return e === null || e === void 0 ? void 0 : e.id; }); const { loading, error, data } = useGetNumDenMatch(onlyIds, engine); const count = reduce((_data$matches = data === null || data === void 0 ? void 0 : data.matches) !== null && _data$matches !== void 0 ? _data$matches : [], function (prev, curr) { var _ref2, _ref3, _curr$numeratorMatch, _curr$numeratorMatch$, _curr$denominatorMatc, _curr$denominatorMatc2; return (_ref2 = (_ref3 = prev + (curr === null || curr === void 0 ? void 0 : (_curr$numeratorMatch = curr.numeratorMatch) === null || _curr$numeratorMatch === void 0 ? void 0 : (_curr$numeratorMatch$ = _curr$numeratorMatch.indicators) === null || _curr$numeratorMatch$ === void 0 ? void 0 : _curr$numeratorMatch$.length)) !== null && _ref3 !== void 0 ? _ref3 : 0 + (curr === null || curr === void 0 ? void 0 : (_curr$denominatorMatc = curr.denominatorMatch) === null || _curr$denominatorMatc === void 0 ? void 0 : (_curr$denominatorMatc2 = _curr$denominatorMatc.indicators) === null || _curr$denominatorMatc2 === void 0 ? void 0 : _curr$denominatorMatc2.length)) !== null && _ref2 !== void 0 ? _ref2 : 0; }, 0); if (loading) { return /*#__PURE__*/React.createElement(Loader, { text: "" }); } if (error) { return /*#__PURE__*/React.createElement(Error, { error: error }); } return /*#__PURE__*/React.createElement(React.Fragment, null, i18n.t("It\u2019s data elements belongs to {{variables}} indicators using it as numerator/denominator", { variables: count })); } IndicatorCount.propTypes = { dataElementsArray: PropTypes.array.isRequired };