From 274844076f5fda6ca4648d7fbbed7d868bdad3f1 Mon Sep 17 00:00:00 2001 From: Tony Valle Date: Tue, 17 Oct 2023 11:55:36 +0200 Subject: [PATCH] refactor: replace `useRulesEngineOrgUnit` with `useReduxOrgUnit` --- .../rules-engine/src/rulesEngine.types.js | 4 +-- .../EnrollmentRegistrationEntry.container.js | 4 +-- .../NewEventDataEntryWrapper.component.js | 4 +-- .../TeiRegistrationEntry.component.js | 11 ++++--- .../EnrollmentPageDefault.container.js | 4 +-- .../ProgramStageSelector.container.js | 4 +-- .../EventDetailsSection.component.js | 4 +-- .../WidgetEventEdit.container.js | 5 +-- src/core_modules/capture-core/hooks/index.js | 1 - .../hooks/useRulesEngineOrgUnit.js | 33 ------------------- 10 files changed, 22 insertions(+), 52 deletions(-) delete mode 100644 src/core_modules/capture-core/hooks/useRulesEngineOrgUnit.js diff --git a/packages/rules-engine/src/rulesEngine.types.js b/packages/rules-engine/src/rulesEngine.types.js index decb2154a0..3f1d422303 100644 --- a/packages/rules-engine/src/rulesEngine.types.js +++ b/packages/rules-engine/src/rulesEngine.types.js @@ -141,12 +141,12 @@ export type OrgUnitGroup = $ReadOnly<{| code: string, |}>; -export type OrgUnit = $ReadOnly<{| +export type OrgUnit = $ReadOnly<{ id: string, name: string, code: string, groups: Array, -|}>; +}>; export type RulesEngineInput = {| programRulesContainer: ProgramRulesContainer, 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 6d624541be..f3f7fbf3c0 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 { useCurrentOrgUnitInfo } from '../../../hooks/useCurrentOrgUnitInfo'; -import { useRulesEngineOrgUnit } from '../../../hooks'; +import { useReduxOrgUnit } from '../../../redux/organisationUnits'; import { dataEntryHasChanges } from '../../DataEntry/common/dataEntryHasChanges'; import { useMetadataForRegistrationForm } from '../common/TEIAndEnrollment/useMetadataForRegistrationForm'; @@ -18,7 +18,7 @@ export const EnrollmentRegistrationEntry: ComponentType = ({ ...passOnProps }) => { const orgUnitId = useCurrentOrgUnitInfo().id; - const { orgUnit, error } = useRulesEngineOrgUnit(orgUnitId); + const { orgUnit, error } = useReduxOrgUnit(orgUnitId); const { teiId, ready, skipDuplicateCheck } = useLifecycle(selectedScopeId, id, trackedEntityInstanceAttributes, orgUnit); const { formId, 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 97503017e5..e8279d39b1 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 { useRulesEngineOrgUnit } from '../../../../hooks/useRulesEngineOrgUnit'; +import { useReduxOrgUnit } from '../../../../redux/organisationUnits'; 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 } = useRulesEngineOrgUnit(orgUnitId); + const { orgUnit, error } = useReduxOrgUnit(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 1a8e11bd4c..5f4da76b0c 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,6 +9,7 @@ import { useScopeInfo } from '../../../hooks/useScopeInfo'; import { scopeTypes } from '../../../metaData'; import { TrackedEntityInstanceDataEntry } from '../TrackedEntityInstance'; import { useCurrentOrgUnitInfo } from '../../../hooks/useCurrentOrgUnitInfo'; +import { useReduxOrgUnit } from '../../../redux/organisationUnits'; import type { Props, PlainProps } from './TeiRegistrationEntry.types'; import { DiscardDialog } from '../../Dialogs/DiscardDialog.component'; import { withSaveHandler } from '../../DataEntry'; @@ -54,7 +55,9 @@ const TeiRegistrationEntryPlain = const [showWarning, setShowWarning] = useState(false); const { scopeType } = useScopeInfo(selectedScopeId); const { formId, formFoundation } = useMetadataForRegistrationForm({ selectedScopeId }); - const orgUnit = useCurrentOrgUnitInfo(); + const { id: orgUnitId } = useCurrentOrgUnitInfo(); + const { orgUnit } = useReduxOrgUnit(id); // [DHIS2-15814] Change this to new hook + const orgUnitName = orgUnit ? orgUnit.name : ''; const handleOnCancel = () => { if (!isUserInteractionInProgress) { @@ -68,9 +71,9 @@ const TeiRegistrationEntryPlain = const url = scopeType === scopeTypes.TRACKER_PROGRAM ? - buildUrlQueryString({ programId: selectedScopeId, orgUnitId: orgUnit.id }) + buildUrlQueryString({ programId: selectedScopeId, orgUnitId }) : - buildUrlQueryString({ orgUnitId: orgUnit.id }); + buildUrlQueryString({ orgUnitId }); return push(`/?${url}`); }; @@ -114,7 +117,7 @@ const TeiRegistrationEntryPlain = - {translatedTextWithStylesForTei(trackedEntityName.toLowerCase(), orgUnit.name)} + {translatedTextWithStylesForTei(trackedEntityName.toLowerCase(), orgUnitName)} { const history = useHistory(); const dispatch = useDispatch(); const { enrollmentId, programId, teiId, orgUnitId } = useLocationQuery(); - const { orgUnit, error } = useRulesEngineOrgUnit(orgUnitId); + const { orgUnit, error } = useReduxOrgUnit(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 1bd16b7a80..a4dbc54796 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 { useRulesEngineOrgUnit } from '../../../../hooks/useRulesEngineOrgUnit'; +import { useReduxOrgUnit } from '../../../../redux/organisationUnits'; import { useTrackerProgram } from '../../../../hooks/useTrackerProgram'; @@ -24,7 +24,7 @@ export const ProgramStageSelector = ({ programId, orgUnitId, teiId, enrollmentId isError: programError, } = useProgramFromIndexedDB(programId); - const { orgUnit } = useRulesEngineOrgUnit(orgUnitId); + const { orgUnit } = useReduxOrgUnit(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 4416248dcf..a925438658 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 { useRulesEngineOrgUnit } from '../../../../hooks/useRulesEngineOrgUnit'; +import { useReduxOrgUnit } from '../../../../redux/organisationUnits'; 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 } = useRulesEngineOrgUnit(orgUnitId); + const { orgUnit, error } = useReduxOrgUnit(orgUnitId); if (error) { return error.errorComponent; 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 4c4196fcfe..2c621fe99a 100644 --- a/src/core_modules/capture-core/components/WidgetEventEdit/WidgetEventEdit.container.js +++ b/src/core_modules/capture-core/components/WidgetEventEdit/WidgetEventEdit.container.js @@ -6,7 +6,8 @@ import { spacersNum, Button, colors, IconEdit24, IconArrowLeft24 } from '@dhis2/ import { withStyles } from '@material-ui/core'; import i18n from '@dhis2/d2-i18n'; import { ConditionalTooltip } from 'capture-core/components/ConditionalTooltip'; -import { useEnrollmentEditEventPageMode, useRulesEngineOrgUnit, useAvailableProgramStages } from 'capture-core/hooks'; +import { useEnrollmentEditEventPageMode, useAvailableProgramStages } from 'capture-core/hooks'; +import { useReduxOrgUnit } from 'capture-core/redux/organisationUnits'; import type { Props } from './widgetEventEdit.types'; import { startShowEditEventDataEntry } from './WidgetEventEdit.actions'; import { Widget } from '../Widget'; @@ -59,7 +60,7 @@ export const WidgetEventEditPlain = ({ }: Props) => { const dispatch = useDispatch(); const { currentPageMode } = useEnrollmentEditEventPageMode(eventStatus); - const { orgUnit, error } = useRulesEngineOrgUnit(orgUnitId); + const { orgUnit, error } = useReduxOrgUnit(orgUnitId); const eventAccess = getProgramEventAccess(programId, programStage.id); const availableProgramStages = useAvailableProgramStages(programStage, teiId, enrollmentId, programId); diff --git a/src/core_modules/capture-core/hooks/index.js b/src/core_modules/capture-core/hooks/index.js index 1330576491..bc172ac729 100644 --- a/src/core_modules/capture-core/hooks/index.js +++ b/src/core_modules/capture-core/hooks/index.js @@ -3,7 +3,6 @@ export { useSearchOptions } from './useSearchOptions'; export { useTrackedEntityTypesWithCorrelatedPrograms } from './useTrackedEntityTypesWithCorrelatedPrograms'; export { useCurrentTrackedEntityTypeId } from './useCurrentTrackedEntityTypeId'; export { useEnrollmentEditEventPageMode } from './useEnrollmentEditEventPageMode'; -export { useRulesEngineOrgUnit } from './useRulesEngineOrgUnit'; export { useAvailableProgramStages } from './useAvailableProgramStages'; export { useScopeInfo } from './useScopeInfo'; export { useScopeTitleText } from './useScopeTitleText'; diff --git a/src/core_modules/capture-core/hooks/useRulesEngineOrgUnit.js b/src/core_modules/capture-core/hooks/useRulesEngineOrgUnit.js deleted file mode 100644 index 94f9fece17..0000000000 --- a/src/core_modules/capture-core/hooks/useRulesEngineOrgUnit.js +++ /dev/null @@ -1,33 +0,0 @@ -// @flow -import React from 'react'; -import i18n from '@dhis2/d2-i18n'; -import type { OrgUnit } from '@dhis2/rules-engine-javascript'; -import { useOrgUnitGroups } from 'capture-core/hooks/useOrgUnitGroups'; -import { useOrganisationUnit } from '../dataQueries'; - -export function useRulesEngineOrgUnit(orgUnitId: string): { - orgUnit?: OrgUnit, - error?: any, -} { - const { orgUnit, error } = useOrganisationUnit(orgUnitId, 'displayName,code'); - const { orgUnitGroups, error: groupError } = useOrgUnitGroups(orgUnitId); - - if (error) { - return { error: { error, errorComponent } }; - } else if (groupError) { - return { error: { groupError, errorComponent } }; - } - - if (orgUnit && orgUnitGroups) { - orgUnit.groups = orgUnitGroups; - return { orgUnit }; - } - - return {}; -} - -const errorComponent = ( -
- {i18n.t('organisation unit could not be retrieved. Please try again later.')} -
-);