-
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: interface for accessing
organisationUnits
- Loading branch information
Showing
8 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
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
26 changes: 26 additions & 0 deletions
26
src/core_modules/capture-core/redux/organisationUnits/fetchReduxOrgUnit.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,26 @@ | ||
// @flow | ||
import { getAssociatedOrgUnitGroups } from 'capture-core/MetaDataStoreUtils/getAssociatedOrgUnitGroups'; | ||
import type { ReduxOrgUnit } from './organisationUnits.types'; | ||
import type { QuerySingleResource } from '../../utils/api/api.types'; | ||
|
||
// Builds new ReduxOrgUnit by fetching data from the api and index db | ||
export async function fetchReduxOrgUnit( | ||
orgUnitId: string, | ||
querySingleResource: QuerySingleResource, | ||
): Promise<ReduxOrgUnit> { | ||
return Promise.all([ | ||
querySingleResource({ | ||
resource: `organisationUnits/${orgUnitId}`, | ||
params: { | ||
fields: 'displayName,code,path', | ||
}, | ||
}), | ||
getAssociatedOrgUnitGroups(orgUnitId), | ||
]).then(([orgUnit, groups]) => ({ | ||
id: orgUnitId, | ||
name: orgUnit.displayName, | ||
code: orgUnit.code, | ||
path: orgUnit.path, | ||
groups, | ||
})); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/core_modules/capture-core/redux/organisationUnits/getReduxOrgUnit.epics.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,28 @@ | ||
// @flow | ||
import { ofType } from 'redux-observable'; | ||
import { catchError, mergeMap, concatMap } from 'rxjs/operators'; | ||
import { from, of } from 'rxjs'; | ||
import { actionTypes, orgUnitFetched, type FetchOrgUnitPayload } from './organisationUnits.actions'; | ||
import { fetchReduxOrgUnit } from './fetchReduxOrgUnit'; | ||
|
||
export const getReduxOrgUnitEpic = ( | ||
action$: InputObservable, | ||
store: ReduxStore, | ||
{ querySingleResource }: ApiUtils, | ||
) => action$.pipe( | ||
ofType(actionTypes.GET_ORGUNIT), | ||
concatMap((action: ReduxAction<FetchOrgUnitPayload, void>) => { | ||
const { organisationUnits } = store.value; | ||
const payload = action.payload; | ||
if (organisationUnits[payload.orgUnitId]) { | ||
return of(payload.onSuccess(organisationUnits[payload.orgUnitId])); | ||
} | ||
return from(fetchReduxOrgUnit(payload.orgUnitId, querySingleResource)) | ||
.pipe( | ||
mergeMap(orgUnit => | ||
of(orgUnitFetched(orgUnit), payload.onSuccess(orgUnit))), | ||
catchError(error => | ||
(payload.onError ? of(payload.onError(error)) : of({}))), | ||
); | ||
}), | ||
); |
5 changes: 5 additions & 0 deletions
5
src/core_modules/capture-core/redux/organisationUnits/index.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,5 @@ | ||
// @flow | ||
export { useReduxOrgUnit } from './useReduxOrgUnit'; | ||
export { getOrgUnit } from './organisationUnits.actions'; | ||
export { getReduxOrgUnitEpic } from './getReduxOrgUnit.epics'; | ||
export type { ReduxOrgUnit } from './organisationUnits.types'; |
21 changes: 21 additions & 0 deletions
21
src/core_modules/capture-core/redux/organisationUnits/organisationUnits.actions.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,21 @@ | ||
// @flow | ||
import { actionCreator } from '../../actions/actions.utils'; | ||
import type { ReduxOrgUnit } from './organisationUnits.types'; | ||
|
||
export const actionTypes = { | ||
GET_ORGUNIT: 'organisationUnits.GetOrgUnit', | ||
ORG_UNIT_FETCHED: 'organisationUnits.OrgUnitFetched', | ||
}; | ||
|
||
type ActionCreator<T> = (payload: T) => ReduxAction<any, any>; | ||
|
||
// Public | ||
export type FetchOrgUnitPayload = { | ||
orgUnitId: string, | ||
onSuccess: ActionCreator<ReduxOrgUnit>, | ||
onError?: ActionCreator<any>, | ||
} | ||
export const getOrgUnit = (payload: FetchOrgUnitPayload) => actionCreator(actionTypes.GET_ORGUNIT)(payload); | ||
|
||
// Private | ||
export const orgUnitFetched = (orgUnit: ReduxOrgUnit) => actionCreator(actionTypes.ORG_UNIT_FETCHED)(orgUnit); |
11 changes: 11 additions & 0 deletions
11
src/core_modules/capture-core/redux/organisationUnits/organisationUnits.types.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,11 @@ | ||
// @flow | ||
import type { OrgUnitGroup } from '@dhis2/rules-engine-javascript'; | ||
|
||
// Make sure rules engine OrgUnit is a subset of this! | ||
export type ReduxOrgUnit = {| | ||
id: string, | ||
name: string, // this is the translated name (displayName) | ||
code: string, | ||
path: string, | ||
groups: Array<OrgUnitGroup>, | ||
|}; |
46 changes: 46 additions & 0 deletions
46
src/core_modules/capture-core/redux/organisationUnits/useReduxOrgUnit.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,46 @@ | ||
// @flow | ||
import React from 'react'; | ||
import i18n from '@dhis2/d2-i18n'; | ||
import { useSelector, useDispatch } from 'react-redux'; | ||
import { useOrgUnitGroups } from 'capture-core/hooks/useOrgUnitGroups'; | ||
import { useOrganisationUnit } from '../../dataQueries'; | ||
import { orgUnitFetched } from './organisationUnits.actions'; | ||
import { type ReduxOrgUnit } from './organisationUnits.types'; | ||
|
||
export function useReduxOrgUnit(orgUnitId: string): { | ||
orgUnit?: ReduxOrgUnit, | ||
error?: any, | ||
} { | ||
const dispatch = useDispatch(); | ||
const reduxOrgUnit = useSelector(({ organisationUnits }) => organisationUnits && organisationUnits[orgUnitId]); | ||
const id = reduxOrgUnit ? undefined : orgUnitId; | ||
// These hooks do no work when id is undefined | ||
const { orgUnit, error } = useOrganisationUnit(id, 'displayName,code,path'); | ||
const { orgUnitGroups, error: groupError } = useOrgUnitGroups(id); | ||
|
||
if (reduxOrgUnit) { | ||
return { orgUnit: reduxOrgUnit }; | ||
} | ||
|
||
if (error) { | ||
return { error: { error, errorComponent } }; | ||
} else if (groupError) { | ||
return { error: { groupError, errorComponent } }; | ||
} | ||
|
||
if (orgUnit && orgUnitGroups) { | ||
orgUnit.name = orgUnit.displayName; | ||
orgUnit.groups = orgUnitGroups; | ||
delete orgUnit.displayName; | ||
dispatch(orgUnitFetched(orgUnit)); | ||
return { orgUnit }; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
const errorComponent = ( | ||
<div> | ||
{i18n.t('organisation unit could not be retrieved. Please try again later.')} | ||
</div> | ||
); |
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