Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [DHIS2-17970] Auto-select orgUnit if there is only one available #3798

Merged
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useState } from 'react';
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 @@ -36,6 +37,7 @@ const WidgetRelatedStagesPlain = ({
orgUnit: undefined,
linkedEventId: undefined,
});
const { isLoading: orgUnitLoading } = useOrgUnitAutoSelect(setRelatedStageDataValues);

const addErrorMessage = (message: ErrorMessagesForRelatedStages) => {
setErrorMessages((prevMessages: Object) => ({
Expand Down Expand Up @@ -81,7 +83,7 @@ const WidgetRelatedStagesPlain = ({
}
}, [formIsValid, relatedStageDataValues]);

if (!currentRelatedStagesStatus || !selectedRelationshipType || isLoadingEvents) {
if (!currentRelatedStagesStatus || !selectedRelationshipType || isLoadingEvents || orgUnitLoading) {
return null;
}

Expand All @@ -103,8 +105,8 @@ const WidgetRelatedStagesPlain = ({
);
};

export const WidgetRelatedStages = forwardRef<Props, {|
export const WidgetRelatedStages = forwardRef < Props, {|
eventHasLinkableStageRelationship: Function,
formIsValidOnSave: Function,
getLinkedStageValues: Function
|}>(WidgetRelatedStagesPlain);
formIsValidOnSave: Function,
getLinkedStageValues: Function
|}>(WidgetRelatedStagesPlain);
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @flow
import { useEffect } from 'react';
import { useApiMetadataQuery } from '../../../utils/reactQueryHelpers';

export const useOrgUnitAutoSelect = (setRelatedStageDataValues: any) => {
const queryKey = ['organisationUnits'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this hook is basically the same as the on in MetadataAutoSelectInitializer. Could we perhaps combine them and reuse it? I feel pretty certain that this will be used in multiple places. Make a new hook called useOrgUnitsForAutoSelect which returns isLoading and an array of max 2 orgunits called orgUnits.

Keep the useEffect outside of the hook, so that the behavior can be customized for whichever use-case needed. (In this case, move the useEffect into the WidgetRelatedStages.component.js-file). Put the hook in the src/core_modules/capture-core/dataQueries folder.

const queryFn = {
resource: 'organisationUnits',
params: {
fields: 'id,path,displayName',
withinUserSearchHierarchy: true,
page: 2,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@henrikmv what is the reason for adding page: 2? I am not able to get the only available orgUnit with these parameters combination. It works if I remove it or use page:1 instead. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry, it should have been pageSize. Thanks!

},
};
const { data, isLoading } = useApiMetadataQuery(queryKey, queryFn);

useEffect(() => {
if (!isLoading && data) {
const orgUnits = data.organisationUnits;
if (!orgUnits || orgUnits.length !== 1) return;
const [orgUnit] = orgUnits;
const { displayName, ...rest } = orgUnit;
setRelatedStageDataValues(prev => ({
...prev,
orgUnit: { ...rest, name: displayName },
}));
}
}, [data, isLoading, setRelatedStageDataValues]);

return {
isLoading,
};
};
Loading