Skip to content

Commit

Permalink
Include dataSets elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccxzhang committed Aug 2, 2024
1 parent 5d9e9bf commit de09953
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
21 changes: 14 additions & 7 deletions src/renderer/src/pages/MainPage/DataElements.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@ const DataElementsSelector = () => {
const elements = useLiveQuery(() => dictionaryDb.dataElements.toArray(), [])
const indicators = useLiveQuery(() => dictionaryDb.indicators.toArray(), [])
const programIndicators = useLiveQuery(() => dictionaryDb.programIndicators.toArray(), [])
const dataSets = useLiveQuery(() => dictionaryDb.dataSets.toArray(), [])

useEffect(() => {
if (elements && indicators && programIndicators) {
const allData = [...elements, ...indicators, ...programIndicators]
if (elements && indicators && programIndicators && dataSets) {
const allData = [...elements, ...indicators, ...programIndicators, ...dataSets]
dispatch(setData(allData))
}
}, [elements, indicators, programIndicators, dispatch])
}, [elements, indicators, programIndicators, dataSets, dispatch])

useEffect(() => {
const filtered = data
.filter((element) =>
selectedDataType !== 'All' ? element.category === selectedDataType : true
)
.filter((element) => element.displayName.toLowerCase().includes(searchQuery.toLowerCase()))
.filter(
(element) =>
searchQuery === '' ||
element.displayName.toLowerCase().includes(searchQuery.toLowerCase())
)
dispatch(setFilteredElements(filtered))
}, [data, selectedDataType, searchQuery, dispatch])

Expand All @@ -60,10 +65,12 @@ const DataElementsSelector = () => {
<option value="DataElement">Data Element</option>
<option value="Indicator">Indicator</option>
<option value="ProgramIndicator">Program Indicator</option>
<option value="dataSets">DataSet</option>
</select>
<input
type="text"
placeholder="Search"
value={searchQuery}
onChange={(e) => dispatch(setSearchQuery(e.target.value))}
className="mb-2 w-full px-4 py-2 border border-gray-700 rounded"
/>
Expand All @@ -73,7 +80,7 @@ const DataElementsSelector = () => {
className="w-full px-4 py-2 overflow-y border-gray-700 rounded"
style={{ minHeight: '200px', maxHeight: '200px' }}
>
{filteredElements.map((element) => (
{filteredElements?.map((element) => (
<option key={element.id} value={element.id} className="whitespace-normal">
- {element.displayName}
</option>
Expand All @@ -91,7 +98,7 @@ const DataElementsSelector = () => {

const SelectedDataElementsDisplay = () => {
const dispatch = useDispatch()
const addedDataElements = useSelector((state) => state.dataElements.addedElements)
const addedDataElements = useSelector((state) => state.dataElements?.addedElements)

const handleRemoveElement = (id) => {
dispatch(removeElement(id))
Expand All @@ -101,7 +108,7 @@ const SelectedDataElementsDisplay = () => {
<div className="mb-4">
<h3 className="text-xl font-bold mb-2">Selected Items</h3>
<ul>
{addedDataElements.map((element) => (
{addedDataElements?.map((element) => (
<li key={element.id} className="text-sm">
{element.displayName}
<button onClick={() => handleRemoveElement(element.id)} className="ml-2 text-red-500">
Expand Down
37 changes: 29 additions & 8 deletions src/renderer/src/reducers/authReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
getDataElements,
getIndicators,
getProgramIndicators,
getCategoryOptionCombos
getCategoryOptionCombos,
getDataSets
} from '../service/useApi'
import { dictionaryDb, servicesDb } from '../service/db'
import { setLoading, setError, setNotification } from '../reducers/statusReducer'
Expand Down Expand Up @@ -78,12 +79,31 @@ export const connect = (dhis2Url, username, password) => async (dispatch) => {
localStorage.setItem('password', password)
localStorage.setItem('dhis2Url', dhis2Url)

const [elements, indicators, programIndicators, catOptionCombos] = await Promise.all([
getDataElements(dhis2Url, username, password),
getIndicators(dhis2Url, username, password),
getProgramIndicators(dhis2Url, username, password),
getCategoryOptionCombos(dhis2Url, username, password)
])
const [elements, indicators, programIndicators, catOptionCombos, dataSetsRaw] =
await Promise.all([
getDataElements(dhis2Url, username, password),
getIndicators(dhis2Url, username, password),
getProgramIndicators(dhis2Url, username, password),
getCategoryOptionCombos(dhis2Url, username, password),
getDataSets(dhis2Url, username, password)
])

const dataSetMetrics = [
'REPORTING_RATE',
'REPORTING_RATE_ON_TIME',
'ACTUAL_REPORTS',
'ACTUAL_REPORTS_ON_TIME',
'EXPECTED_REPORTS'
]

const dataSets = dataSetsRaw.dataSets.flatMap((ds) =>
dataSetMetrics.map((metric) => ({
...ds,
id: `${ds.id}.${metric}`,
displayName: `${ds.displayName} (${metric})`,
category: 'dataSets'
}))
)

await Promise.all([
dictionaryDb.dataElements.bulkAdd(
Expand All @@ -98,7 +118,8 @@ export const connect = (dhis2Url, username, password) => async (dispatch) => {
category: 'ProgramIndicator'
}))
),
dictionaryDb.catOptionCombos.bulkAdd(catOptionCombos.categoryOptionCombos)
dictionaryDb.catOptionCombos.bulkAdd(catOptionCombos.categoryOptionCombos),
dictionaryDb.dataSets.bulkAdd(dataSets)
])

dispatch(setNotification({ message: 'Connected successfully', type: 'success' }))
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/service/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dictionaryDb.version(1).stores({
indicators: '++id, category, displayName, displayDescription, numerator, denominator',
programIndicators: '++id, category, displayName, displayDescription',
catOptionCombos: '++id, displayName',
dataSets: '++id, displayName',
query: '++id, ou, pe, dx, co, url'
})

Expand Down
5 changes: 5 additions & 0 deletions src/renderer/src/service/useApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,8 @@ export const getOrganizationUnitGroupSets = (dhis2Url, username, password) => {
const orgUnitGroupSetsUrl = `${dhis2Url}/api/organisationUnitGroupSets?fields=id,displayName&paging=false`
return fetchData(orgUnitGroupSetsUrl, username, password)
}

export const getDataSets = (dhis2Url, username, password) => {
const dataSetsUrl = `${dhis2Url}/api/dataSets?fields=id,displayName&paging=false`
return fetchData(dataSetsUrl, username, password)
}

0 comments on commit de09953

Please sign in to comment.