diff --git a/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/EnrollmentRegistrationEntry.container.js b/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/EnrollmentRegistrationEntry.container.js index 67482a7521..55106542d8 100644 --- a/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/EnrollmentRegistrationEntry.container.js +++ b/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/EnrollmentRegistrationEntry.container.js @@ -6,7 +6,7 @@ import { EnrollmentRegistrationEntryComponent } from './EnrollmentRegistrationEn import type { OwnProps } from './EnrollmentRegistrationEntry.types'; import { useLifecycle } from './hooks'; import { useCurrentOrgUnitId } from '../../../hooks/useCurrentOrgUnitId'; -import { useReduxOrgUnit } from '../../../redux/organisationUnits'; +import { useCoreOrgUnit } from '../../../metadataRetrieval/coreOrgUnit'; import { dataEntryHasChanges } from '../../DataEntry/common/dataEntryHasChanges'; export const EnrollmentRegistrationEntry: ComponentType = ({ @@ -18,7 +18,7 @@ export const EnrollmentRegistrationEntry: ComponentType = ({ ...passOnProps }) => { const orgUnitId = useCurrentOrgUnitId(); - const { orgUnit, error } = useReduxOrgUnit(orgUnitId); + const { orgUnit, error } = useCoreOrgUnit(orgUnitId); const { teiId, ready, diff --git a/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/NewEventDataEntryWrapper.component.js b/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/NewEventDataEntryWrapper.component.js index e8279d39b1..14e74ad609 100644 --- a/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/NewEventDataEntryWrapper.component.js +++ b/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/NewEventDataEntryWrapper.component.js @@ -7,7 +7,7 @@ import { DataEntry } from './DataEntry/DataEntry.container'; import { EventsList } from './RecentlyAddedEventsList/RecentlyAddedEventsList.container'; import { useScopeTitleText } from '../../../../hooks/useScopeTitleText'; import { useCurrentProgramInfo } from '../../../../hooks/useCurrentProgramInfo'; -import { useReduxOrgUnit } from '../../../../redux/organisationUnits'; +import { useCoreOrgUnit } from '../../../../metadataRetrieval/coreOrgUnit'; import { useLocationQuery } from '../../../../utils/routing'; import { useRulesEngine } from './useRulesEngine'; import type { PlainProps } from './NewEventDataEntryWrapper.types'; @@ -48,7 +48,7 @@ const NewEventDataEntryWrapperPlain = ({ }: PlainProps) => { const { id: programId } = useCurrentProgramInfo(); const orgUnitId = useLocationQuery().orgUnitId; - const { orgUnit, error } = useReduxOrgUnit(orgUnitId); + const { orgUnit, error } = useCoreOrgUnit(orgUnitId); const rulesReady = useRulesEngine({ programId, orgUnit, formFoundation }); const titleText = useScopeTitleText(programId); diff --git a/src/core_modules/capture-core/components/DataEntries/TeiRegistrationEntry/TeiRegistrationEntry.component.js b/src/core_modules/capture-core/components/DataEntries/TeiRegistrationEntry/TeiRegistrationEntry.component.js index 95c8ba6886..7b61054364 100644 --- a/src/core_modules/capture-core/components/DataEntries/TeiRegistrationEntry/TeiRegistrationEntry.component.js +++ b/src/core_modules/capture-core/components/DataEntries/TeiRegistrationEntry/TeiRegistrationEntry.component.js @@ -9,7 +9,7 @@ import { useScopeInfo } from '../../../hooks/useScopeInfo'; import { scopeTypes } from '../../../metaData'; import { TrackedEntityInstanceDataEntry } from '../TrackedEntityInstance'; import { useCurrentOrgUnitId } from '../../../hooks/useCurrentOrgUnitId'; -import { useReduxOrgUnit } from '../../../redux/organisationUnits'; +import { useCoreOrgUnit } from '../../../metadataRetrieval/coreOrgUnit'; import type { Props, PlainProps } from './TeiRegistrationEntry.types'; import { DiscardDialog } from '../../Dialogs/DiscardDialog.component'; import { withSaveHandler } from '../../DataEntry'; @@ -56,7 +56,7 @@ const TeiRegistrationEntryPlain = const { scopeType } = useScopeInfo(selectedScopeId); const { formId, formFoundation } = useMetadataForRegistrationForm({ selectedScopeId }); const orgUnitId = useCurrentOrgUnitId(); - const { orgUnit } = useReduxOrgUnit(orgUnitId); // Tony: [DHIS2-15814] Change this to new hook + const { orgUnit } = useCoreOrgUnit(orgUnitId); // Tony: [DHIS2-15814] Change this to new hook const orgUnitName = orgUnit ? orgUnit.name : ''; const handleOnCancel = () => { diff --git a/src/core_modules/capture-core/components/LockedSelector/LockedSelector.actions.js b/src/core_modules/capture-core/components/LockedSelector/LockedSelector.actions.js index 5426d31572..d7e395e289 100644 --- a/src/core_modules/capture-core/components/LockedSelector/LockedSelector.actions.js +++ b/src/core_modules/capture-core/components/LockedSelector/LockedSelector.actions.js @@ -1,6 +1,6 @@ // @flow import { actionCreator } from '../../actions/actions.utils'; -import type { ReduxOrgUnit } from '../../redux/organisationUnits'; +import type { CoreOrgUnit } from '../../metadataRetrieval/coreOrgUnit'; export const lockedSelectorActionTypes = { LOADING_START: 'LockedSelector.Loading', @@ -18,7 +18,7 @@ export const lockedSelectorActionTypes = { export const updateSelectionsFromUrl = (data: Object) => actionCreator(lockedSelectorActionTypes.FROM_URL_UPDATE)(data); export const validSelectionsFromUrl = () => actionCreator(lockedSelectorActionTypes.FROM_URL_CURRENT_SELECTIONS_VALID)(); export const invalidSelectionsFromUrl = (error: string) => actionCreator(lockedSelectorActionTypes.FROM_URL_CURRENT_SELECTIONS_INVALID)({ error }); -export const setCurrentOrgUnitBasedOnUrl = (orgUnit: ReduxOrgUnit) => actionCreator(lockedSelectorActionTypes.FETCH_ORG_UNIT_SUCCESS)(orgUnit); +export const setCurrentOrgUnitBasedOnUrl = (orgUnit: CoreOrgUnit) => actionCreator(lockedSelectorActionTypes.FETCH_ORG_UNIT_SUCCESS)(orgUnit); export const startLoading = () => actionCreator(lockedSelectorActionTypes.LOADING_START)(); export const completeUrlUpdate = () => actionCreator(lockedSelectorActionTypes.FROM_URL_UPDATE_COMPLETE)(); export const errorRetrievingOrgUnitBasedOnUrl = (error: string) => actionCreator(lockedSelectorActionTypes.FETCH_ORG_UNIT_ERROR)({ error }); diff --git a/src/core_modules/capture-core/components/LockedSelector/LockedSelector.epics.js b/src/core_modules/capture-core/components/LockedSelector/LockedSelector.epics.js index 360cbd6508..ad4329db43 100644 --- a/src/core_modules/capture-core/components/LockedSelector/LockedSelector.epics.js +++ b/src/core_modules/capture-core/components/LockedSelector/LockedSelector.epics.js @@ -16,7 +16,7 @@ import { import { programCollection } from '../../metaDataMemoryStores'; import { getLocationPathname, pageFetchesOrgUnitUsingTheOldWay } from '../../utils/url'; import { getLocationQuery } from '../../utils/routing'; -import { getOrgUnit } from '../../redux/organisationUnits'; +import { getCoreOrgUnit } from '../../metadataRetrieval/coreOrgUnit'; export const getOrgUnitDataBasedOnUrlUpdateEpic = (action$: InputObservable, store: ReduxStore) => action$.pipe( @@ -28,7 +28,7 @@ export const getOrgUnitDataBasedOnUrlUpdateEpic = (action$: InputObservable, sto if (organisationUnits[orgUnitId]) { return of(completeUrlUpdate()); } - return of(startLoading(), getOrgUnit({ + return of(startLoading(), getCoreOrgUnit({ orgUnitId, onSuccess: setCurrentOrgUnitBasedOnUrl, onError: () => errorRetrievingOrgUnitBasedOnUrl(i18n.t('Could not get organisation unit')), diff --git a/src/core_modules/capture-core/components/OrgUnitFetcher/OrgUnitFetcher.actions.js b/src/core_modules/capture-core/components/OrgUnitFetcher/OrgUnitFetcher.actions.js index 4c36fd473d..806b384742 100644 --- a/src/core_modules/capture-core/components/OrgUnitFetcher/OrgUnitFetcher.actions.js +++ b/src/core_modules/capture-core/components/OrgUnitFetcher/OrgUnitFetcher.actions.js @@ -1,7 +1,7 @@ // @flow import { actionCreator } from '../../actions/actions.utils'; -import type { ReduxOrgUnit } from '../../redux/organisationUnits'; +import type { CoreOrgUnit } from '../../metadataRetrieval/coreOrgUnit'; export const actionTypes = { FETCH_ORG_UNIT: 'OrgUnitFetcher.FetchOrgUnit', @@ -12,6 +12,6 @@ export const actionTypes = { export const fetchOrgUnit = (orgUnitId: string) => actionCreator(actionTypes.FETCH_ORG_UNIT)({ orgUnitId }); -export const setCurrentOrgUnit = (orgUnit: ReduxOrgUnit) => actionCreator(actionTypes.FETCH_ORG_UNIT_SUCCESS)(orgUnit); +export const setCurrentOrgUnit = (orgUnit: CoreOrgUnit) => actionCreator(actionTypes.FETCH_ORG_UNIT_SUCCESS)(orgUnit); export const errorRetrievingOrgUnit = () => actionCreator(actionTypes.FETCH_ORG_UNIT_FAILURE)(); diff --git a/src/core_modules/capture-core/components/OrgUnitFetcher/OrgUnitFetcher.epics.js b/src/core_modules/capture-core/components/OrgUnitFetcher/OrgUnitFetcher.epics.js index d50ed49f73..f82ba69dc0 100644 --- a/src/core_modules/capture-core/components/OrgUnitFetcher/OrgUnitFetcher.epics.js +++ b/src/core_modules/capture-core/components/OrgUnitFetcher/OrgUnitFetcher.epics.js @@ -1,12 +1,12 @@ // @flow import { ofType } from 'redux-observable'; import { map } from 'rxjs/operators'; -import { getOrgUnit } from '../../redux/organisationUnits'; +import { getCoreOrgUnit } from '../../metadataRetrieval/coreOrgUnit'; import { actionTypes, setCurrentOrgUnit, errorRetrievingOrgUnit } from './OrgUnitFetcher.actions'; export const orgUnitFetcherEpic = (action$: InputObservable) => action$.pipe( ofType(actionTypes.FETCH_ORG_UNIT), - map(({ payload: { orgUnitId } }) => getOrgUnit({ + map(({ payload: { orgUnitId } }) => getCoreOrgUnit({ orgUnitId, onSuccess: orgUnit => setCurrentOrgUnit(orgUnit), onError: errorRetrievingOrgUnit, diff --git a/src/core_modules/capture-core/components/Pages/Enrollment/EnrollmentPageDefault/EnrollmentPageDefault.container.js b/src/core_modules/capture-core/components/Pages/Enrollment/EnrollmentPageDefault/EnrollmentPageDefault.container.js index be98fa707d..71e75fa2b0 100644 --- a/src/core_modules/capture-core/components/Pages/Enrollment/EnrollmentPageDefault/EnrollmentPageDefault.container.js +++ b/src/core_modules/capture-core/components/Pages/Enrollment/EnrollmentPageDefault/EnrollmentPageDefault.container.js @@ -19,7 +19,7 @@ import { updateTeiDisplayName, } from '../EnrollmentPage.actions'; import { useTrackerProgram } from '../../../../hooks/useTrackerProgram'; -import { useReduxOrgUnit } from '../../../../redux/organisationUnits'; +import { useCoreOrgUnit } from '../../../../metadataRetrieval/coreOrgUnit'; import { EnrollmentPageDefaultComponent } from './EnrollmentPageDefault.component'; import { useProgramMetadata, @@ -33,7 +33,7 @@ export const EnrollmentPageDefault = () => { const history = useHistory(); const dispatch = useDispatch(); const { enrollmentId, programId, teiId, orgUnitId } = useLocationQuery(); - const { orgUnit, error } = useReduxOrgUnit(orgUnitId); + const { orgUnit, error } = useCoreOrgUnit(orgUnitId); const program = useTrackerProgram(programId); const { diff --git a/src/core_modules/capture-core/components/Pages/EnrollmentAddEvent/ProgramStageSelector/ProgramStageSelector.container.js b/src/core_modules/capture-core/components/Pages/EnrollmentAddEvent/ProgramStageSelector/ProgramStageSelector.container.js index a4dbc54796..d0356732ac 100644 --- a/src/core_modules/capture-core/components/Pages/EnrollmentAddEvent/ProgramStageSelector/ProgramStageSelector.container.js +++ b/src/core_modules/capture-core/components/Pages/EnrollmentAddEvent/ProgramStageSelector/ProgramStageSelector.container.js @@ -10,7 +10,7 @@ import { useCommonEnrollmentDomainData, useRuleEffects } from '../../common/Enro import type { Props } from './ProgramStageSelector.types'; import { useProgramFromIndexedDB } from '../../../../utils/cachedDataHooks/useProgramFromIndexedDB'; import { useLocationQuery, buildUrlQueryString } from '../../../../utils/routing'; -import { useReduxOrgUnit } from '../../../../redux/organisationUnits'; +import { useCoreOrgUnit } from '../../../../metadataRetrieval/coreOrgUnit'; import { useTrackerProgram } from '../../../../hooks/useTrackerProgram'; @@ -24,7 +24,7 @@ export const ProgramStageSelector = ({ programId, orgUnitId, teiId, enrollmentId isError: programError, } = useProgramFromIndexedDB(programId); - const { orgUnit } = useReduxOrgUnit(orgUnitId); + const { orgUnit } = useCoreOrgUnit(orgUnitId); const programRules = useTrackerProgram(programId); const ruleEffects = useRuleEffects({ diff --git a/src/core_modules/capture-core/components/Pages/ViewEvent/EventDetailsSection/EventDetailsSection.component.js b/src/core_modules/capture-core/components/Pages/ViewEvent/EventDetailsSection/EventDetailsSection.component.js index a925438658..31da7bdbd4 100644 --- a/src/core_modules/capture-core/components/Pages/ViewEvent/EventDetailsSection/EventDetailsSection.component.js +++ b/src/core_modules/capture-core/components/Pages/ViewEvent/EventDetailsSection/EventDetailsSection.component.js @@ -11,7 +11,7 @@ import { ViewEventSectionHeader } from '../Section/ViewEventSectionHeader.compon import { EditEventDataEntry } from '../../../WidgetEventEdit/EditEventDataEntry/EditEventDataEntry.container'; import { ViewEventDataEntry } from '../../../WidgetEventEdit/ViewEventDataEntry/ViewEventDataEntry.container'; import type { ProgramStage } from '../../../../metaData'; -import { useReduxOrgUnit } from '../../../../redux/organisationUnits'; +import { useCoreOrgUnit } from '../../../../metadataRetrieval/coreOrgUnit'; import { NoticeBox } from '../../../NoticeBox'; const getStyles = () => ({ @@ -60,7 +60,7 @@ const EventDetailsSectionPlain = (props: Props) => { eventAccess, ...passOnProps } = props; const orgUnitId = useSelector(({ viewEventPage }) => viewEventPage.loadedValues?.orgUnit?.id); - const { orgUnit, error } = useReduxOrgUnit(orgUnitId); + const { orgUnit, error } = useCoreOrgUnit(orgUnitId); if (error) { return error.errorComponent; diff --git a/src/core_modules/capture-core/components/Pages/ViewEvent/epics/viewEvent.epics.js b/src/core_modules/capture-core/components/Pages/ViewEvent/epics/viewEvent.epics.js index 254a6cf3bc..b39483080b 100644 --- a/src/core_modules/capture-core/components/Pages/ViewEvent/epics/viewEvent.epics.js +++ b/src/core_modules/capture-core/components/Pages/ViewEvent/epics/viewEvent.epics.js @@ -4,7 +4,7 @@ import { ofType } from 'redux-observable'; import { map, switchMap } from 'rxjs/operators'; import i18n from '@dhis2/d2-i18n'; import { errorCreator } from 'capture-core-utils'; -import { getOrgUnit } from 'capture-core/redux/organisationUnits'; +import { getCoreOrgUnit } from 'capture-core/metadataRetrieval/coreOrgUnit'; import { isSelectionsEqual } from '../../../App/isSelectionsEqual'; import { getErrorMessageAndDetails } from '../../../../utils/errors/getErrorMessageAndDetails'; @@ -43,7 +43,7 @@ export const getEventOpeningFromEventListEpic = ( return openViewEventPageFailed( i18n.t('Event could not be loaded. Are you sure it exists?')); } - return getOrgUnit({ + return getCoreOrgUnit({ orgUnitId: eventContainer.event.orgUnitId, onSuccess: orgUnit => startOpenEventForView(eventContainer, orgUnit), onError: (error) => { @@ -105,7 +105,7 @@ export const getOrgUnitOnUrlUpdateEpic = (action$: InputObservable) => ofType(viewEventActionTypes.EVENT_FROM_URL_RETRIEVED), map((action) => { const eventContainer = action.payload.eventContainer; - return getOrgUnit({ + return getCoreOrgUnit({ orgUnitId: eventContainer.event.orgUnitId, onSuccess: orgUnit => orgUnitRetrievedOnUrlUpdate(orgUnit, eventContainer), onError: (error) => { diff --git a/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/OrgUnitFetcher/OrgUnitFetcher.component.js b/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/OrgUnitFetcher/OrgUnitFetcher.component.js index b2ccb9d18e..f92ecbfa41 100644 --- a/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/OrgUnitFetcher/OrgUnitFetcher.component.js +++ b/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/OrgUnitFetcher/OrgUnitFetcher.component.js @@ -1,7 +1,7 @@ // @flow import * as React from 'react'; import i18n from '@dhis2/d2-i18n'; -import { useReduxOrgUnit } from '../../../redux/organisationUnits'; +import { useCoreOrgUnit } from '../../../metadataRetrieval/coreOrgUnit'; import { Validated } from '../Validated/Validated.container'; import type { OrgUnitFetcherProps } from './orgUnitFetcher.types'; @@ -9,7 +9,7 @@ export const OrgUnitFetcher = ({ orgUnitId, ...passOnProps }: OrgUnitFetcherProps) => { - const { error, orgUnit } = useReduxOrgUnit(orgUnitId); + const { error, orgUnit } = useCoreOrgUnit(orgUnitId); if (error) { return ( diff --git a/src/core_modules/capture-core/components/WidgetEventEdit/WidgetEventEdit.container.js b/src/core_modules/capture-core/components/WidgetEventEdit/WidgetEventEdit.container.js index 85c76f538c..e2096656ab 100644 --- a/src/core_modules/capture-core/components/WidgetEventEdit/WidgetEventEdit.container.js +++ b/src/core_modules/capture-core/components/WidgetEventEdit/WidgetEventEdit.container.js @@ -7,7 +7,7 @@ import { withStyles } from '@material-ui/core'; import i18n from '@dhis2/d2-i18n'; import { ConditionalTooltip } from 'capture-core/components/ConditionalTooltip'; import { useEnrollmentEditEventPageMode, useAvailableProgramStages } from 'capture-core/hooks'; -import { useReduxOrgUnit } from 'capture-core/redux/organisationUnits'; +import { useCoreOrgUnit } from 'capture-core/metadataRetrieval/coreOrgUnit'; import type { Props } from './widgetEventEdit.types'; import { startShowEditEventDataEntry } from './WidgetEventEdit.actions'; import { Widget } from '../Widget'; @@ -61,7 +61,7 @@ export const WidgetEventEditPlain = ({ }: Props) => { const dispatch = useDispatch(); const { currentPageMode } = useEnrollmentEditEventPageMode(eventStatus); - const { orgUnit, error } = useReduxOrgUnit(orgUnitId); + const { orgUnit, error } = useCoreOrgUnit(orgUnitId); // "Edit event"-button depends on loadedValues. Delay rendering component until loadedValues has been initialized. const loadedValues = useSelector(({ viewEventPage }) => viewEventPage.loadedValues); diff --git a/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/coreOrgUnit.actions.js b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/coreOrgUnit.actions.js new file mode 100644 index 0000000000..b8c9b760d2 --- /dev/null +++ b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/coreOrgUnit.actions.js @@ -0,0 +1,14 @@ +// @flow +import { actionCreator } from '../../actions/actions.utils'; +import type { CoreOrgUnit, FetchOrgUnitPayload } from './coreOrgUnit.types'; + +export const actionTypes = { + GET_ORGUNIT: 'organisationUnits.GetOrgUnit', + ORG_UNIT_FETCHED: 'organisationUnits.OrgUnitFetched', +}; + +// Public +export const getCoreOrgUnit = (payload: FetchOrgUnitPayload) => actionCreator(actionTypes.GET_ORGUNIT)(payload); + +// Private +export const orgUnitFetched = (orgUnit: CoreOrgUnit) => actionCreator(actionTypes.ORG_UNIT_FETCHED)(orgUnit); diff --git a/src/core_modules/capture-core/redux/organisationUnits/organisationUnits.types.js b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/coreOrgUnit.types.js similarity index 55% rename from src/core_modules/capture-core/redux/organisationUnits/organisationUnits.types.js rename to src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/coreOrgUnit.types.js index f08257f9f6..49b9e4f57f 100644 --- a/src/core_modules/capture-core/redux/organisationUnits/organisationUnits.types.js +++ b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/coreOrgUnit.types.js @@ -2,10 +2,19 @@ import type { OrgUnitGroup } from '@dhis2/rules-engine-javascript'; // Make sure rules engine OrgUnit is a subset of this! -export type ReduxOrgUnit = {| +export type CoreOrgUnit = {| id: string, name: string, // this is the translated name (displayName) code: string, path: string, groups: Array, |}; + + +type ActionCreator = (payload: T) => ReduxAction; + +export type FetchOrgUnitPayload = { + orgUnitId: string, + onSuccess: ActionCreator, + onError?: ActionCreator, +}; diff --git a/src/core_modules/capture-core/redux/organisationUnits/fetchReduxOrgUnit.js b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/fetchCoreOrgUnit.js similarity index 77% rename from src/core_modules/capture-core/redux/organisationUnits/fetchReduxOrgUnit.js rename to src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/fetchCoreOrgUnit.js index 51ab968d41..580855fee8 100644 --- a/src/core_modules/capture-core/redux/organisationUnits/fetchReduxOrgUnit.js +++ b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/fetchCoreOrgUnit.js @@ -1,13 +1,13 @@ // @flow import { getAssociatedOrgUnitGroups } from 'capture-core/MetaDataStoreUtils/getAssociatedOrgUnitGroups'; -import type { ReduxOrgUnit } from './organisationUnits.types'; +import type { CoreOrgUnit } from './coreOrgUnit.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( +// Builds new CoreOrgUnit by fetching data from the api and index db +export async function fetchCoreOrgUnit( orgUnitId: string, querySingleResource: QuerySingleResource, -): Promise { +): Promise { return Promise.all([ querySingleResource({ resource: `organisationUnits/${orgUnitId}`, diff --git a/src/core_modules/capture-core/redux/organisationUnits/getReduxOrgUnit.epics.js b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/getCoreOrgUnit.epics.js similarity index 74% rename from src/core_modules/capture-core/redux/organisationUnits/getReduxOrgUnit.epics.js rename to src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/getCoreOrgUnit.epics.js index c37d75e8fb..0eee20e5e3 100644 --- a/src/core_modules/capture-core/redux/organisationUnits/getReduxOrgUnit.epics.js +++ b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/getCoreOrgUnit.epics.js @@ -2,10 +2,11 @@ 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'; +import { actionTypes, orgUnitFetched } from './coreOrgUnit.actions'; +import { fetchCoreOrgUnit } from './fetchCoreOrgUnit'; +import type { FetchOrgUnitPayload } from './coreOrgUnit.types'; -export const getReduxOrgUnitEpic = ( +export const getCoreOrgUnitEpic = ( action$: InputObservable, store: ReduxStore, { querySingleResource }: ApiUtils, @@ -17,7 +18,7 @@ export const getReduxOrgUnitEpic = ( if (organisationUnits[payload.orgUnitId]) { return of(payload.onSuccess(organisationUnits[payload.orgUnitId])); } - return from(fetchReduxOrgUnit(payload.orgUnitId, querySingleResource)) + return from(fetchCoreOrgUnit(payload.orgUnitId, querySingleResource)) .pipe( mergeMap(orgUnit => of(orgUnitFetched(orgUnit), payload.onSuccess(orgUnit))), diff --git a/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/index.js b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/index.js new file mode 100644 index 0000000000..ee09984e67 --- /dev/null +++ b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/index.js @@ -0,0 +1,5 @@ +// @flow +export { useCoreOrgUnit } from './useCoreOrgUnit'; +export { getCoreOrgUnit } from './coreOrgUnit.actions'; +export { getCoreOrgUnitEpic } from './getCoreOrgUnit.epics'; +export type { CoreOrgUnit } from './coreOrgUnit.types'; diff --git a/src/core_modules/capture-core/redux/organisationUnits/useReduxOrgUnit.js b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/useCoreOrgUnit.js similarity index 60% rename from src/core_modules/capture-core/redux/organisationUnits/useReduxOrgUnit.js rename to src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/useCoreOrgUnit.js index ba0b439d79..b30c3a36c3 100644 --- a/src/core_modules/capture-core/redux/organisationUnits/useReduxOrgUnit.js +++ b/src/core_modules/capture-core/metadataRetrieval/coreOrgUnit/useCoreOrgUnit.js @@ -4,19 +4,19 @@ 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'; +import { orgUnitFetched } from './coreOrgUnit.actions'; +import { type CoreOrgUnit } from './coreOrgUnit.types'; -export function useReduxOrgUnit(orgUnitId: string): { - orgUnit?: ReduxOrgUnit, +export function useCoreOrgUnit(orgUnitId: string): { + orgUnit?: CoreOrgUnit, error?: any, } { const dispatch = useDispatch(); const reduxOrgUnit = useSelector(({ organisationUnits }) => organisationUnits && organisationUnits[orgUnitId]); - const id = reduxOrgUnit ? undefined : orgUnitId; + const fetchId = 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); + const { orgUnit, error } = useOrganisationUnit(fetchId, 'displayName,code,path'); + const { orgUnitGroups, error: groupError } = useOrgUnitGroups(fetchId); if (reduxOrgUnit) { return { orgUnit: reduxOrgUnit }; @@ -29,11 +29,14 @@ export function useReduxOrgUnit(orgUnitId: string): { } if (orgUnit && orgUnitGroups) { - orgUnit.name = orgUnit.displayName; - orgUnit.groups = orgUnitGroups; - delete orgUnit.displayName; - dispatch(orgUnitFetched(orgUnit)); - return { orgUnit }; + const { displayName, ...restOrgUnit } = orgUnit; + const coreOrgUnit = { + ...restOrgUnit, + name: displayName, + groups: orgUnitGroups, + }; + dispatch(orgUnitFetched(coreOrgUnit)); + return { orgUnit: coreOrgUnit }; } return {}; diff --git a/src/core_modules/capture-core/reducers/descriptions/organisationUnits.reducerDescription.js b/src/core_modules/capture-core/reducers/descriptions/organisationUnits.reducerDescription.js index 33b4677d52..24d880f226 100644 --- a/src/core_modules/capture-core/reducers/descriptions/organisationUnits.reducerDescription.js +++ b/src/core_modules/capture-core/reducers/descriptions/organisationUnits.reducerDescription.js @@ -1,11 +1,11 @@ // @flow import { createReducerDescription } from '../../trackerRedux/trackerReducer'; import { actionTypes as initActionTypes } from '../../init/init.actions'; -import { actionTypes as reduxOrgunitActionTypes } from '../../redux/organisationUnits/organisationUnits.actions'; -import type { ReduxOrgUnit } from '../../redux/organisationUnits'; +import { actionTypes as coreOrgunitActionTypes } from '../../metadataRetrieval/coreOrgUnit/coreOrgUnit.actions'; +import type { CoreOrgUnit } from '../../metadataRetrieval/coreOrgUnit'; export const organisationUnitDesc = createReducerDescription({ - [reduxOrgunitActionTypes.ORG_UNIT_FETCHED]: (state: ReduxState, action: { payload: ReduxOrgUnit }) => ({ + [coreOrgunitActionTypes.ORG_UNIT_FETCHED]: (state: ReduxState, action: { payload: CoreOrgUnit }) => ({ ...state, [action.payload.id]: action.payload, }), diff --git a/src/core_modules/capture-core/redux/organisationUnits/index.js b/src/core_modules/capture-core/redux/organisationUnits/index.js deleted file mode 100644 index ed880346dc..0000000000 --- a/src/core_modules/capture-core/redux/organisationUnits/index.js +++ /dev/null @@ -1,5 +0,0 @@ -// @flow -export { useReduxOrgUnit } from './useReduxOrgUnit'; -export { getOrgUnit } from './organisationUnits.actions'; -export { getReduxOrgUnitEpic } from './getReduxOrgUnit.epics'; -export type { ReduxOrgUnit } from './organisationUnits.types'; diff --git a/src/core_modules/capture-core/redux/organisationUnits/organisationUnits.actions.js b/src/core_modules/capture-core/redux/organisationUnits/organisationUnits.actions.js deleted file mode 100644 index 5d7dc72165..0000000000 --- a/src/core_modules/capture-core/redux/organisationUnits/organisationUnits.actions.js +++ /dev/null @@ -1,21 +0,0 @@ -// @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 = (payload: T) => ReduxAction; - -// Public -export type FetchOrgUnitPayload = { - orgUnitId: string, - onSuccess: ActionCreator, - onError?: ActionCreator, -} -export const getOrgUnit = (payload: FetchOrgUnitPayload) => actionCreator(actionTypes.GET_ORGUNIT)(payload); - -// Private -export const orgUnitFetched = (orgUnit: ReduxOrgUnit) => actionCreator(actionTypes.ORG_UNIT_FETCHED)(orgUnit); diff --git a/src/epics/trackerCapture.epics.js b/src/epics/trackerCapture.epics.js index 19be4f8c00..eb790ff6e8 100644 --- a/src/epics/trackerCapture.epics.js +++ b/src/epics/trackerCapture.epics.js @@ -215,8 +215,8 @@ import { orgUnitFetcherEpic, } from '../core_modules/capture-core/components/OrgUnitFetcher'; import { - getReduxOrgUnitEpic, -} from '../core_modules/capture-core/redux/organisationUnits'; + getCoreOrgUnitEpic, +} from '../core_modules/capture-core/metadataRetrieval/coreOrgUnit'; export const epics = combineEpics( resetProgramAfterSettingOrgUnitIfApplicableEpic, @@ -342,7 +342,7 @@ export const epics = combineEpics( navigateToEnrollmentOverviewEpic, scheduleEnrollmentEventEpic, orgUnitFetcherEpic, - getReduxOrgUnitEpic, + getCoreOrgUnitEpic, updateTeiEpic, updateTeiSucceededEpic, updateTeiFailedEpic,