@hisptz/react-ui
Version:
A collection of reusable complex DHIS2 react ui components.
158 lines (151 loc) • 3.68 kB
JavaScript
import { useState, useEffect } from "react";
import { dataSourceTypes } from "../../Models";
const dataElementsSearch = {
matches: {
resource: "dataElements",
params: _ref => {
let {
keyword
} = _ref;
return {
fields: ["id", "displayName", "href"],
pageSize: 10,
filter: ["id:ilike:".concat(keyword), "displayName:ilike:".concat(keyword)],
rootJunction: "OR"
};
}
}
};
const indicatorSearch = {
matches: {
resource: "indicators",
params: _ref2 => {
let {
keyword
} = _ref2;
return {
fields: ["id", "displayName", "href"],
pageSize: 10,
filter: ["id:ilike:".concat(keyword), "displayName:ilike:".concat(keyword)],
rootJunction: "OR"
};
}
}
};
const programIndicatorSearch = {
matches: {
resource: "programIndicators",
params: _ref3 => {
let {
keyword
} = _ref3;
return {
fields: ["id", "displayName", "href"],
pageSize: 10,
filter: ["id:ilike:".concat(keyword), "displayName:ilike:".concat(keyword)],
rootJunction: "OR"
};
}
}
};
const dataElementGroupsSearch = {
matches: {
resource: "dataElementGroups",
params: _ref4 => {
let {
keyword
} = _ref4;
return {
fields: ["id", "displayName", "href"],
pageSize: 10,
filter: ["id:ilike:".concat(keyword), "displayName:ilike:".concat(keyword)],
rootJunction: "OR"
};
}
}
};
const indicatorGroupsSearch = {
matches: {
resource: "indicatorGroups",
params: _ref5 => {
let {
keyword
} = _ref5;
return {
fields: ["id", "displayName", "href"],
pageSize: 10,
filter: ["id:ilike:".concat(keyword), "displayName:ilike:".concat(keyword)],
rootJunction: "OR"
};
}
}
};
export function useGetSearchResult(keyword, type, engine) {
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);
const [data, setData] = useState();
const result = [];
useEffect(() => {
async function fetch() {
if (keyword !== "") {
return await getResult(keyword, engine, type);
}
}
fetch().then(res => {
setLoading(false);
setData(res);
}).catch(error => {
setLoading(false);
setError(error);
});
}, [keyword, type]);
return {
loading,
error,
data
};
}
async function getResult(keyword, engine, type) {
if (type === dataSourceTypes.DATA_ELEMENT) {
const data = await engine.query(dataElementsSearch, {
variables: {
keyword
}
});
return data.matches.dataElements;
}
if (type === dataSourceTypes.INDICATOR) {
const data = await engine.query(indicatorSearch, {
variables: {
keyword
}
});
return data.matches.indicators;
}
if (type === dataSourceTypes.PROGRAM_INDICATOR) {
const data = await engine.query(programIndicatorSearch, {
variables: {
keyword
}
});
return data.matches.programIndicators;
}
if (type === dataSourceTypes.DATA_ELEMENT_GROUP) {
const data = await engine.query(dataElementGroupsSearch, {
variables: {
keyword
}
});
return data.matches.dataElementGroups;
}
if (type === dataSourceTypes.INDICATOR_GROUP) {
const data = await engine.query(indicatorGroupsSearch, {
variables: {
keyword
}
});
return data.matches.indicatorGroups;
} // if(type===dataSourceTypes.FUNCTION){
// const data = await engine.query(query6, {variables: {keyword}})
// }
}