-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: [DHIS2-17970] Auto-select orgUnit if there is only one available #3798
Changes from 4 commits
381bce4
e8756e3
7dedc13
dbe4461
2b6b0a7
fb2c654
9b97d2e
155dc66
f0f8c72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// @flow | ||
import { useEffect } from 'react'; | ||
import { useApiMetadataQuery } from '../../../utils/reactQueryHelpers'; | ||
|
||
export const useOrgUnitAutoSelect = (setRelatedStageDataValues: any) => { | ||
const queryKey = ['organisationUnits']; | ||
const queryFn = { | ||
resource: 'organisationUnits', | ||
params: { | ||
fields: 'id,path,displayName', | ||
withinUserSearchHierarchy: true, | ||
page: 2, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @henrikmv what is the reason for adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am sorry, it should have been |
||
}, | ||
}; | ||
const { data, isLoading } = useApiMetadataQuery(queryKey, queryFn); | ||
|
||
useEffect(() => { | ||
if (!isLoading && data) { | ||
const orgUnits = data.organisationUnits; | ||
if (!orgUnits || orgUnits.length !== 1) return; | ||
const [orgUnit] = orgUnits; | ||
const { displayName, ...rest } = orgUnit; | ||
setRelatedStageDataValues(prev => ({ | ||
...prev, | ||
orgUnit: { ...rest, name: displayName }, | ||
})); | ||
} | ||
}, [data, isLoading, setRelatedStageDataValues]); | ||
|
||
return { | ||
isLoading, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this hook is basically the same as the on in
MetadataAutoSelectInitializer
. Could we perhaps combine them and reuse it? I feel pretty certain that this will be used in multiple places. Make a new hook calleduseOrgUnitsForAutoSelect
which returnsisLoading
and an array of max 2 orgunits calledorgUnits
.Keep the useEffect outside of the hook, so that the behavior can be customized for whichever use-case needed. (In this case, move the useEffect into the
WidgetRelatedStages.component.js
-file). Put the hook in thesrc/core_modules/capture-core/dataQueries
folder.