@hisptz/react-ui
Version:
A collection of reusable complex DHIS2 react ui components.
168 lines (156 loc) • 3.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useGetSearchResult = useGetSearchResult;
var _react = require("react");
var _Models = require("../../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"
};
}
}
};
function useGetSearchResult(keyword, type, engine) {
const [loading, setLoading] = (0, _react.useState)(true);
const [error, setError] = (0, _react.useState)(false);
const [data, setData] = (0, _react.useState)();
const result = [];
(0, _react.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 === _Models.dataSourceTypes.DATA_ELEMENT) {
const data = await engine.query(dataElementsSearch, {
variables: {
keyword
}
});
return data.matches.dataElements;
}
if (type === _Models.dataSourceTypes.INDICATOR) {
const data = await engine.query(indicatorSearch, {
variables: {
keyword
}
});
return data.matches.indicators;
}
if (type === _Models.dataSourceTypes.PROGRAM_INDICATOR) {
const data = await engine.query(programIndicatorSearch, {
variables: {
keyword
}
});
return data.matches.programIndicators;
}
if (type === _Models.dataSourceTypes.DATA_ELEMENT_GROUP) {
const data = await engine.query(dataElementGroupsSearch, {
variables: {
keyword
}
});
return data.matches.dataElementGroups;
}
if (type === _Models.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}})
// }
}