Skip to content

Commit

Permalink
refactor: simplify OrgUnitFetcher in WidgetEnrollmentEventNew
Browse files Browse the repository at this point in the history
  • Loading branch information
superskip committed Oct 17, 2023
1 parent 2f2096d commit 39cc34a
Showing 1 changed file with 4 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
// @flow
import * as React from 'react';
import i18n from '@dhis2/d2-i18n';
import { useOrgUnitGroups } from 'capture-core/hooks/useOrgUnitGroups';
import { useOrganisationUnit } from '../../../dataQueries';
import { useReduxOrgUnit } from '../../../redux/organisationUnits';
import { Validated } from '../Validated/Validated.container';
import type { OrgUnitFetcherProps } from './orgUnitFetcher.types';

// Produces a rules engine compatible OrgUnit object.

// The technical reason for introducing this component is that useOrganisationUnit and useOrgUnitGroups
// use async methods; the job of this component is to wait for these to complete.

export const OrgUnitFetcher = ({
orgUnitId,
...passOnProps
}: OrgUnitFetcherProps) => {
const { error, orgUnit } = useOrganisationUnit(orgUnitId, 'displayName,code');
const { orgUnitGroups, error: groupError } = useOrgUnitGroups(orgUnitId);
const { error, orgUnit } = useReduxOrgUnit(orgUnitId);

if (error || groupError) {
if (error) {
return (
<div>
{i18n.t('organisation unit could not be retrieved. Please try again later.')}
</div>
);
}

if (orgUnit && orgUnitGroups) {
orgUnit.groups = orgUnitGroups;

if (orgUnit) {
return (
<Validated
{...passOnProps}
Expand Down

0 comments on commit 39cc34a

Please sign in to comment.