@hisptz/react-ui
Version:
A collection of reusable complex DHIS2 react ui components.
494 lines (452 loc) • 16.7 kB
JavaScript
import { dataTypes, dataTypesInitials } from "../Models";
const identifiableQuery1 = {
identifiableObjects: {
resource: "identifiableObjects",
id: _ref => {
let {
id
} = _ref;
return id;
},
params: {
fields: ["id", "displayName", "href"]
}
}
};
const queryIdentifiable2 = {
identifiableObjects: {
resource: "identifiableObjects",
id: _ref2 => {
let {
id
} = _ref2;
return id;
},
params: {
fields: ["id", "displayName", "href"]
}
},
identifiableObjects2: {
resource: "identifiableObjects",
id: _ref3 => {
let {
id2
} = _ref3;
return id2;
},
params: {
fields: ["id", "displayName", "href"]
}
}
};
const dtEleSourceQuery = {
dataElementSource: {
resource: "dataElements",
id: _ref4 => {
let {
id
} = _ref4;
return id;
},
params: {
fields: ["id", "displayName", "dataSetElements[dataSet[id,displayName]]"]
}
}
};
const querySource = {
dataElementSource: {
resource: "dataElements",
id: _ref5 => {
let {
id
} = _ref5;
return id;
},
params: {
fields: ["id", "displayName", "dataSetElements[dataSet[id,displayName]]"]
}
},
identifiableObjects: {
resource: "identifiableObjects",
id: _ref6 => {
let {
idCombo
} = _ref6;
return idCombo;
},
params: {
fields: ["id", "displayName"]
}
}
};
const programIndicatorQuery = {
programIndicators: {
resource: "programIndicators",
id: _ref7 => {
let {
id
} = _ref7;
return id;
},
params: {
fields: ["id", "displayName", "program[id,displayName]"]
}
}
};
const identifiableObjectsQuery = {
identifiableObjectsProgram: {
resource: "identifiableObjects",
id: _ref8 => {
let {
idProgram
} = _ref8;
return idProgram;
},
params: {
fields: ["id", "displayName"]
}
},
identifiableObjectsDtEle: {
resource: "identifiableObjects",
id: _ref9 => {
let {
idDataElement
} = _ref9;
return idDataElement;
},
params: {
fields: ["id", "displayName"]
}
}
};
const functionQuery = {
functions: {
resource: "dataStore/functions",
id: _ref10 => {
let {
idFunction
} = _ref10;
return idFunction;
}
}
};
function loopGetSources(formula, sourceInitial) {
let ind1 = 0;
let ind2 = 0;
const arr = [];
const initialLength = sourceInitial === null || sourceInitial === void 0 ? void 0 : sourceInitial.length; //since we have case for initials like ORG{
while (((_formula = formula) === null || _formula === void 0 ? void 0 : _formula.search(sourceInitial)) >= 0) {
var _formula;
//there is still a dataElement
ind1 = formula.indexOf(sourceInitial) + initialLength - 2; //first occourance
const subStr = formula.substr(ind1);
ind2 = subStr === null || subStr === void 0 ? void 0 : subStr.indexOf("}");
ind2 = ind2 + ind1;
const datEl = formula.substring(ind1 + 2, ind2);
arr.push(datEl);
formula = setCharAt(formula, ind1, ""); //remove {
formula = setCharAt(formula, ind1 - 1, ""); //removes #
formula = setCharAt(formula, ind2 - 2, ""); //removes }
}
return arr;
}
export function getFormulaSources(formula, sourceInitial) {
let arr = loopGetSources(formula, sourceInitial);
if (sourceInitial === dataTypesInitials.DATASET_REPORTING_RATES) {
const resultedArr = [];
arr.filter(ele => {
resultedArr.push(ele.split(".")[0]); //elements comes as BfMAe6Itzgt.REPORTING_RATE or OsPTWNqq26W.EXPECTED_REPORTS so we do this to just take the id
});
arr = resultedArr;
}
return arr;
}
export function setCharAt(str, index, chr) {
if (index > str.length - 1) {
return str;
}
return str.substring(0, index) + chr + str.substring(index + 1);
}
async function getValueIdentifiableObjects2(engine, id, id2) {
var _data$identifiableObj;
const data = await engine.query(queryIdentifiable2, {
variables: {
id,
id2
}
});
return [data === null || data === void 0 ? void 0 : (_data$identifiableObj = data.identifiableObjects) === null || _data$identifiableObj === void 0 ? void 0 : _data$identifiableObj.displayName, data === null || data === void 0 ? void 0 : data.identifiableObjects2.displayName];
}
async function getValueIdentifiableObjects(engine, id) {
var _data$identifiableObj2;
const data = await engine.query(identifiableQuery1, {
variables: {
id
}
});
return [data === null || data === void 0 ? void 0 : (_data$identifiableObj2 = data.identifiableObjects) === null || _data$identifiableObj2 === void 0 ? void 0 : _data$identifiableObj2.displayName];
}
async function getValueDataElementSource(engine, id) {
const data = await engine.query(dtEleSourceQuery, {
variables: {
id
}
});
return [data === null || data === void 0 ? void 0 : data.dataElementSource];
}
async function getValueProgramIndicator(engine, id) {
const data = await engine.query(programIndicatorQuery, {
variables: {
id
}
});
return [data === null || data === void 0 ? void 0 : data.programIndicators];
}
async function getValueDataElementSourceWithCombo(engine, id, idCombo) {
const data = await engine.query(querySource, {
variables: {
id,
idCombo
}
});
return [data === null || data === void 0 ? void 0 : data.dataElementSource, data === null || data === void 0 ? void 0 : data.identifiableObjects.displayName];
}
async function getValueProgramDataElementWithSource(engine, idProgram, idDataElement) {
var _data$identifiableObj3, _data$identifiableObj4;
const data = await engine.query(identifiableObjectsQuery, {
variables: {
idProgram,
idDataElement
}
});
return [data === null || data === void 0 ? void 0 : (_data$identifiableObj3 = data.identifiableObjectsProgram) === null || _data$identifiableObj3 === void 0 ? void 0 : _data$identifiableObj3.displayName, data === null || data === void 0 ? void 0 : (_data$identifiableObj4 = data.identifiableObjectsDtEle) === null || _data$identifiableObj4 === void 0 ? void 0 : _data$identifiableObj4.displayName];
}
async function getValueDataSource(engine, id) {
if (isPureDataElement(id)) {
//its a function
const data = await engine.query(identifiableQuery1, {
variables: {
id
}
});
return [data === null || data === void 0 ? void 0 : data.identifiableObjects];
} else {
const idFunction = id.split(".")[0];
const data = await engine.query(functionQuery, {
variables: {
idFunction
}
});
return [data === null || data === void 0 ? void 0 : data.functions];
}
}
export function getFormulaInWordsFromFullSources(formula, arrOfSources) {
for (let i = 0; i < (arrOfSources === null || arrOfSources === void 0 ? void 0 : arrOfSources.length); i++) {
var _formula2, _arrOfSources$i;
if (((_formula2 = formula) === null || _formula2 === void 0 ? void 0 : _formula2.search((_arrOfSources$i = arrOfSources[i]) === null || _arrOfSources$i === void 0 ? void 0 : _arrOfSources$i.id)) >= 0) {
var _formula3, _arrOfSources$i2, _arrOfSources$i3;
formula = (_formula3 = formula) === null || _formula3 === void 0 ? void 0 : _formula3.replace((_arrOfSources$i2 = arrOfSources[i]) === null || _arrOfSources$i2 === void 0 ? void 0 : _arrOfSources$i2.id, (_arrOfSources$i3 = arrOfSources[i]) === null || _arrOfSources$i3 === void 0 ? void 0 : _arrOfSources$i3.val);
}
}
return formula;
}
export function getFinalWordFormula(formula, dataElementsArray, programIndicatorArray, dataSetReportingRatesArray, attributes, constants, programDtElement, orgUnitCount) {
var _final, _final2, _final3, _final4, _final5, _final6, _final7, _final8, _final14, _final15;
//need to be reduced to a loop
let final = getFormulaInWordsFromFullSources(formula, dataElementsArray);
final = getFormulaInWordsFromFullSources(final, programIndicatorArray);
final = getFormulaInWordsFromFullSources(final, dataSetReportingRatesArray);
final = getFormulaInWordsFromFullSources(final, attributes);
final = getFormulaInWordsFromFullSources(final, constants);
final = getFormulaInWordsFromFullSources(final, orgUnitCount);
final = getFormulaInWordsFromFullSources(final, programDtElement); //replacing all occurrence of the following globally
final = (_final = final) === null || _final === void 0 ? void 0 : _final.replace(/#{/g, "{");
final = (_final2 = final) === null || _final2 === void 0 ? void 0 : _final2.replace(/I{/g, "{");
final = (_final3 = final) === null || _final3 === void 0 ? void 0 : _final3.replace(/D{/g, "{");
final = (_final4 = final) === null || _final4 === void 0 ? void 0 : _final4.replace(/V{/g, "{");
final = (_final5 = final) === null || _final5 === void 0 ? void 0 : _final5.replace(/C{/g, "{");
final = (_final6 = final) === null || _final6 === void 0 ? void 0 : _final6.replace(/A{/g, "{");
final = (_final7 = final) === null || _final7 === void 0 ? void 0 : _final7.replace(/R{/g, "{");
final = (_final8 = final) === null || _final8 === void 0 ? void 0 : _final8.replace(/OUG{/g, "{");
if ((dataSetReportingRatesArray === null || dataSetReportingRatesArray === void 0 ? void 0 : dataSetReportingRatesArray.length) !== 0) {
var _final9, _final10, _final11, _final12, _final13;
//replace those caps
//has to be fixed later
final = (_final9 = final) === null || _final9 === void 0 ? void 0 : _final9.replace(/ACTUAL_REPORTS/g, "Actual_Reports");
final = (_final10 = final) === null || _final10 === void 0 ? void 0 : _final10.replace(/REPORTING_RATE_ON_TIME/g, "Reporting_on_Time");
final = (_final11 = final) === null || _final11 === void 0 ? void 0 : _final11.replace(/EXPECTED_REPORTS/g, "Expected_Reports");
final = (_final12 = final) === null || _final12 === void 0 ? void 0 : _final12.replace(/REPORTING_RATE/g, "Reporting_Rate");
final = (_final13 = final) === null || _final13 === void 0 ? void 0 : _final13.replace(/ACTUAL_REPORTS_ON_TIME/g, "Actual_Reports_on_Time");
}
final = (_final14 = final) === null || _final14 === void 0 ? void 0 : _final14.replace(/_/g, " ");
final = (_final15 = final) === null || _final15 === void 0 ? void 0 : _final15.replace(/\./g, " ");
return cleanBrackets(final);
}
export function lowerCaseAllWordsExceptFirstLetters(string) {
return string === null || string === void 0 ? void 0 : string.replace(/\S*/g, function (word) {
return (word === null || word === void 0 ? void 0 : word.charAt(0)) + (word === null || word === void 0 ? void 0 : word.slice(1).toLowerCase());
});
}
export function getSummaryValueFromApi(engine, id) {
if (isPureDataElement(id)) {
//fetch value normally
return new Promise((resolve, reject) => {
resolve(getValueIdentifiableObjects(engine, id));
});
} else {
//break to array and just take first element
return new Promise((resolve, reject) => {
const arr = id.split(".");
resolve(getValueIdentifiableObjects2(engine, arr[0], arr[1]));
});
}
}
export function getDetailedValueFromApi(engine, id, type) {
if (type === dataTypes.DATA_ELEMENT) {
if (isPureDataElement(id)) {
//fetch value normally
return new Promise((resolve, reject) => {
resolve(getValueDataElementSource(engine, id));
});
} else {
//break to array and just take first element
return new Promise((resolve, reject) => {
const arr = id.split(".");
resolve(getValueDataElementSourceWithCombo(engine, arr[0], arr[1]));
});
}
}
if (type === dataTypes.PROGRAM_DATA_ELEMENT || type === dataTypes.ATTRIBUTES) {
return new Promise(resolve => {
const arr = id.split(".");
resolve(getValueProgramDataElementWithSource(engine, arr[0], arr[1]));
});
}
if (type === dataTypes.PROGRAM_INDICATOR) {
return new Promise(resolve => {
resolve(getValueProgramIndicator(engine, id));
});
} else {
return new Promise(resolve => {
resolve(getValueIdentifiableObjects(engine, id));
});
}
}
export function getValueDataSourcePromise(engine, id) {
return getValueDataSource(engine, id); //its automatically a promise since it is await
}
export async function getWordData(engine, arr, type, loc) {
//arr for array of id of datas to get their values, type indicates the data type of data eg dataElement=0 program indicator=1, reporting rates=2
if (arr.length > 0) {
const allPromises = arr === null || arr === void 0 ? void 0 : arr.map(id => {
return getDetailedValueFromApi(engine, id === null || id === void 0 ? void 0 : id.replace(/ /g, ""), type);
});
return await Promise.all(allPromises).then(value => {
if (type === dataTypes.DATA_ELEMENT) {
return value.map((val, index) => {
//We always return array just for uniformity
if (val.length === 2) {
//array of two elements first element is dataElement second element of array is category option combo
return {
id: arr[index],
val: val[0].displayName + " " + val[1],
location: loc,
sources: val[0].dataSetElements
}; // wordDtEl.push({id:arr[i],val:val[0].displayName+" "+val[1],location:loc,sources:val[0].dataSetElements})
}
if (val.length === 1) {
//this is array of one element for data element that are just pure no category options
return {
id: arr[index],
val: val[0].displayName,
location: loc,
sources: val[0].dataSetElements
}; // wordDtEl.push({id:arr[i],val:val[0].displayName,"location":loc,sources:val[0].dataSetElements})
}
});
}
if (type === dataTypes.PROGRAM_INDICATOR) {
return value.map((val, index) => {
//We always return array just for uniformity
return {
id: arr[index],
val: val[0].displayName,
location: loc,
sources: val[0].program
};
});
}
if (type === dataTypes.DATASET_REPORTING_RATES) {
return value.map((val, index) => {
//We always return array just for uniformity
return {
id: arr[index],
val: val[0],
location: loc
};
});
} else {
return value.map((val, index) => {
//We always return array just for uniformity
return {
id: arr[index],
val: val[0],
location: loc
};
});
}
});
}
}
export async function getWordDataForAll(engine, arr, loc) {
if (arr.length > 0) {
const allPromises = arr === null || arr === void 0 ? void 0 : arr.map(id => {
return getSummaryValueFromApi(engine, id === null || id === void 0 ? void 0 : id.replace(/ /g, ""));
});
return await Promise.all(allPromises).then(value => {
return value.map((val, index) => {
//We always return array just for uniformity
if (val.length === 2) {
//array of two elements first element is dataElement second element of array is category option combo or program stage then data element
return {
id: arr[index],
val: val[0] + " " + val[1],
location: loc
};
}
if (val.length === 1) {
//this is array of one element for data element that are just pure no category options
return {
id: arr[index],
val: val[0],
location: loc
};
}
});
});
}
}
function cleanBrackets(formula) {
if (typeof formula !== dataTypes.UNDEFINED) {
let arr = formula.split("{");
arr = arr.join("");
arr = arr.split("}"); //string = array.join("")
arr = arr.join(" ");
return arr;
}
return formula;
}
export function isPureDataElement(str) {
return (str === null || str === void 0 ? void 0 : str.indexOf(".")) === -1;
}
export function formatBytes(bytes) {
let decimals = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
if (bytes === 0) {
return "0 Bytes";
}
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}