Skip to content

Commit

Permalink
feat: review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikmv committed Sep 11, 2024
1 parent 381bce4 commit e8756e3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useState } from 'react';
import { useDataQuery } from '@dhis2/app-runtime';
import { useRelatedStages } from './useRelatedStages';
import { useOrgUnitAutoSelect } from './hooks/useAutoSelctOrgUnitRelatedStage';
import type { Props, RelatedStageDataValueStates } from './WidgetRelatedStages.types';
import { RelatedStagesActions } from './RelatedStagesActions';
import { relatedStageStatus } from './constants';
Expand Down Expand Up @@ -29,16 +29,6 @@ const WidgetRelatedStagesPlain = ({
occurredLabel,
enrollmentId,
});

const { loading: orgUnitLoading, data: orgUnitData } = useDataQuery({
orgUnits: {
resource: 'me',
params: {
fields: ['organisationUnits[id,path,displayName,children::isNotEmpty]'],
},
},
});

const [saveAttempted, setSaveAttempted] = useState(false);
const [errorMessages, setErrorMessages] = useState({});
const [relatedStageDataValues, setRelatedStageDataValues] = useState<RelatedStageDataValueStates>({
Expand All @@ -47,17 +37,7 @@ const WidgetRelatedStagesPlain = ({
orgUnit: undefined,
linkedEventId: undefined,
});

useEffect(() => {
const orgUnits = orgUnitData?.orgUnits?.organisationUnits || [];
if (orgUnits.length === 1 && !orgUnits[0]?.children) {
const { displayName, ...rest } = orgUnits[0];
setRelatedStageDataValues(prev => ({
...prev,
orgUnit: { ...rest, name: displayName },
}));
}
}, [orgUnitData]);
const { isLoading: orgUnitLoading } = useOrgUnitAutoSelect(setRelatedStageDataValues);

const addErrorMessage = (message: ErrorMessagesForRelatedStages) => {
setErrorMessages((prevMessages: Object) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// @flow
import { useEffect } from 'react';
import { useApiMetadataQuery } from '../../../utils/reactQueryHelpers';

export const useOrgUnitAutoSelect = (setRelatedStageDataValues: any) => {
const queryKey = ['organisationUnits'];
const queryFn = {
resource: 'organisationUnits',
params: {
fields: 'id,path,displayName,children::isNotEmpty',
withinUserSearchHierarchy: true,
paging: 2,
},
};
const { data, isLoading } = useApiMetadataQuery(queryKey, queryFn);

useEffect(() => {
const orgUnits = data?.organisationUnits;
if (isLoading || !orgUnits || orgUnits.length !== 1) return;

const [orgUnit] = orgUnits;
if (orgUnit && !orgUnit.children) {
const { displayName, ...rest } = orgUnit;
setRelatedStageDataValues(prev => ({
...prev,
orgUnit: { ...rest, name: displayName },
}));
}
}, [data, isLoading, setRelatedStageDataValues]);


return {
isLoading,
};
};

0 comments on commit e8756e3

Please sign in to comment.