Skip to content

Commit

Permalink
refactor: rename ReduxOrgUnit -> CoreOrgUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
superskip committed Oct 20, 2023
1 parent 978303c commit 9c20251
Show file tree
Hide file tree
Showing 23 changed files with 86 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<OwnProps> = ({
Expand All @@ -18,7 +18,7 @@ export const EnrollmentRegistrationEntry: ComponentType<OwnProps> = ({
...passOnProps
}) => {
const orgUnitId = useCurrentOrgUnitId();
const { orgUnit, error } = useReduxOrgUnit(orgUnitId);
const { orgUnit, error } = useCoreOrgUnit(orgUnitId);
const {
teiId,
ready,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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')),
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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)();
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => ({
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// @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';

export const OrgUnitFetcher = ({
orgUnitId,
...passOnProps
}: OrgUnitFetcherProps) => {
const { error, orgUnit } = useReduxOrgUnit(orgUnitId);
const { error, orgUnit } = useCoreOrgUnit(orgUnitId);

if (error) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrgUnitGroup>,
|};


type ActionCreator<T> = (payload: T) => ReduxAction<any, any>;

export type FetchOrgUnitPayload = {
orgUnitId: string,
onSuccess: ActionCreator<CoreOrgUnit>,
onError?: ActionCreator<any>,
};
Original file line number Diff line number Diff line change
@@ -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<ReduxOrgUnit> {
): Promise<CoreOrgUnit> {
return Promise.all([
querySingleResource({
resource: `organisationUnits/${orgUnitId}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))),
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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 {};
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
}),
Expand Down

This file was deleted.

Loading

0 comments on commit 9c20251

Please sign in to comment.