-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [DHIS-11419] display assigned users on events in enrollment ove…
…rview page (#3453)
- Loading branch information
Showing
14 changed files
with
188 additions
and
35 deletions.
There are no files selected for viewing
113 changes: 92 additions & 21 deletions
113
...apture-core/components/Pages/Enrollment/EnrollmentPageDefault/hooks/useProgramMetadata.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,99 @@ | ||
// @flow | ||
import { useEffect } from 'react'; | ||
import { useDataQuery } from '@dhis2/app-runtime'; | ||
|
||
const query = { | ||
programData: { | ||
resource: 'programs', | ||
id: ({ id }) => id, | ||
params: { | ||
fields: | ||
['programStages[id,repeatable,hideDueDate,programStageDataElements[displayInReports,dataElement[id,valueType,displayName,displayFormName,optionSet[options[code,name]]]'], | ||
}, | ||
}, | ||
}; | ||
import { useMemo } from 'react'; | ||
import { useProgramFromIndexedDB } from '../../../../../utils/cachedDataHooks/useProgramFromIndexedDB'; | ||
import { useDataElementsFromIndexedDB } from '../../../../../utils/cachedDataHooks/useDataElementsFromIndexedDB'; | ||
import { useOptionSetsFromIndexedDB } from '../../../../../utils/cachedDataHooks/useOptionSetsFromIndexedDB'; | ||
|
||
const queryKey = 'useProgramMetadata'; | ||
|
||
export const useProgramMetadata = (programId: string) => { | ||
const { data, error, loading, refetch } = useDataQuery(query, { | ||
lazy: true, | ||
}); | ||
const { program, isLoading, isError } = useProgramFromIndexedDB(programId, { enabled: !!programId }); | ||
|
||
const dataElementIds = useMemo(() => | ||
(program ? program.programStages.reduce( | ||
(acc, stage) => stage.programStageDataElements.reduce( | ||
(accIds, dataElement) => { | ||
accIds.add(dataElement.dataElementId); | ||
return accIds; | ||
}, acc), | ||
new Set) : undefined), [program]); | ||
|
||
const { | ||
isLoading: loadingDataElements, | ||
dataElements, | ||
isError: dataElementsError, | ||
} = useDataElementsFromIndexedDB([queryKey, programId], dataElementIds); | ||
|
||
const derivedDataElementValues = useMemo(() => | ||
(dataElements ? ({ | ||
optionSetIds: dataElements.reduce((acc, dataElement) => { | ||
if (dataElement.optionSetValue) { | ||
acc.add(dataElement.optionSet.id); | ||
} | ||
return acc; | ||
}, new Set), | ||
dataElementDictionary: dataElements.reduce((acc, dataElement) => { | ||
acc[dataElement.id] = dataElement; | ||
return acc; | ||
}, {}), | ||
}) : undefined), [dataElements]); | ||
|
||
useEffect(() => { | ||
if (programId) { | ||
refetch({ id: programId }); | ||
const { | ||
isLoading: loadingOptionSets, | ||
optionSets, | ||
isError: optionSetsError, | ||
} = useOptionSetsFromIndexedDB([queryKey, programId], derivedDataElementValues && derivedDataElementValues.optionSetIds); | ||
|
||
const optionSetDictionary = useMemo( | ||
() => (optionSets ? optionSets.reduce( | ||
(acc, optionSet) => { | ||
acc[optionSet.id] = { | ||
optionSet: { | ||
options: optionSet.options.map(option => ({ | ||
name: option.displayName, | ||
code: option.code, | ||
})), | ||
}, | ||
}; | ||
return acc; | ||
}, {}) : undefined), | ||
[optionSets], | ||
); | ||
|
||
const programMetadata = useMemo(() => { | ||
if (!program || !derivedDataElementValues || !optionSetDictionary) { | ||
return undefined; | ||
} | ||
}, [refetch, programId]); | ||
|
||
return { error, programMetadata: !loading && data?.programData ? data.programData : undefined }; | ||
const dataElementDictionary = derivedDataElementValues.dataElementDictionary; | ||
|
||
return { | ||
programStages: program.programStages.map(stage => ({ | ||
id: stage.id, | ||
repeatable: stage.repeatable, | ||
hideDueDate: stage.hideDueDate, | ||
enableUserAssignment: stage.enableUserAssignment, | ||
programStageDataElements: stage.programStageDataElements | ||
.map((programStageDataElement) => { | ||
const dataElement = dataElementDictionary[programStageDataElement.dataElementId]; | ||
return { | ||
displayInReports: programStageDataElement.displayInReports, | ||
dataElement: { | ||
id: dataElement.id, | ||
valueType: dataElement.valueType, | ||
displayName: dataElement.displayName, | ||
displayFormName: dataElement.displayFormName, | ||
optionSet: dataElement.optionSetValue ? optionSetDictionary[dataElement.optionSet.id] : {}, | ||
}, | ||
}; | ||
}), | ||
})), | ||
}; | ||
}, [program, derivedDataElementValues, optionSetDictionary]); | ||
|
||
|
||
return { | ||
error: (isError || dataElementsError || optionSetsError) && { programId }, | ||
programMetadata: (isLoading || loadingDataElements || loadingOptionSets) ? undefined : programMetadata, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/core_modules/capture-core/utils/cachedDataHooks/useDataElementsFromIndexedDB.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// @flow | ||
import type { UseQueryOptions } from 'react-query'; | ||
import { userStores, getUserStorageController } from '../../storageControllers'; | ||
import { useIndexedDBQuery } from '../reactQueryHelpers'; | ||
import type { CachedDataElement } from '../../storageControllers/'; | ||
|
||
export const useDataElementsFromIndexedDB = (queryKey: Array<string | number>, dataElementIds: ?Set<string>, queryOptions?: UseQueryOptions<>): { | ||
dataElements: ?Array<CachedDataElement>, | ||
isLoading: boolean, | ||
isError: boolean, | ||
} => { | ||
const storageController = getUserStorageController(); | ||
const { enabled = !!dataElementIds } = queryOptions ?? {}; | ||
|
||
const { data, isLoading, isError } = useIndexedDBQuery( | ||
['dataElements', ...queryKey], | ||
() => storageController.getAll( | ||
userStores.DATA_ELEMENTS, { | ||
// $FlowIgnore - the enabled prop guarantees that dataElementIds will be defined | ||
predicate: dataElement => dataElementIds.has(dataElement.id), | ||
}, | ||
), { | ||
enabled, | ||
}, | ||
); | ||
|
||
return { | ||
dataElements: data, | ||
isLoading, | ||
isError, | ||
}; | ||
}; |
32 changes: 32 additions & 0 deletions
32
src/core_modules/capture-core/utils/cachedDataHooks/useOptionSetsFromIndexedDB.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// @flow | ||
import type { UseQueryOptions } from 'react-query'; | ||
import { userStores, getUserStorageController } from '../../storageControllers'; | ||
import { useIndexedDBQuery } from '../reactQueryHelpers'; | ||
import type { CachedOptionSet } from '../../storageControllers/'; | ||
|
||
export const useOptionSetsFromIndexedDB = (queryKey: Array<string | number>, optionSetIds: ?Set<string>, queryOptions?: UseQueryOptions<>): { | ||
optionSets: ?Array<CachedOptionSet>, | ||
isLoading: boolean, | ||
isError: boolean, | ||
} => { | ||
const storageController = getUserStorageController(); | ||
const { enabled = !!optionSetIds } = queryOptions ?? {}; | ||
|
||
const { data, isLoading, isError } = useIndexedDBQuery( | ||
['optionSets', ...queryKey], | ||
() => storageController.getAll( | ||
userStores.OPTION_SETS, { | ||
// $FlowIgnore - the enabled prop guarantees that optionSetIds will be defined | ||
predicate: optionSet => optionSetIds.has(optionSet.id), | ||
}, | ||
), { | ||
enabled, | ||
}, | ||
); | ||
|
||
return { | ||
optionSets: data, | ||
isLoading, | ||
isError, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/core_modules/capture-core/utils/reactQueryHelpers/reactQueryHelpers.const.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
|
||
// @flow | ||
|
||
export const ReactQueryAppNamespace = 'capture'; | ||
export const IndexedDBNamespace = 'indexedDB'; |