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,9 +1,10 @@
// @flow
import { useCallback, useEffect, useState } from 'react';
import { useHistory } from 'react-router-dom';
import { useApiMetadataQuery, useIndexedDBQuery } from '../../../utils/reactQueryHelpers';
import { useIndexedDBQuery } from '../../../utils/reactQueryHelpers';
import { getUserStorageController, userStores } from '../../../storageControllers';
import { buildUrlQueryString, useLocationQuery } from '../../../utils/routing';
import { useOrgUnitAutoSelect } from '../../../dataQueries';

const getAllPrograms = () => {
const userStorageController = getUserStorageController();
Expand All @@ -28,21 +29,8 @@ export const useMetadataAutoSelect = () => {
},
);

const { data: searchOrgUnits, isLoading: loadingOrgUnits } = useApiMetadataQuery(
['searchOrgUnitsForAutoSelect'],
{
resource: 'organisationUnits',
params: {
fields: 'id',
withinUserSearchHierarchy: true,
pageSize: 2,
},
},
{
enabled: Object.keys(urlParams).length === 0 && !mounted,
select: ({ organisationUnits }) => organisationUnits,
},
);
const queryOptions = { enabled: Object.keys(urlParams).length === 0 && !mounted };
const { isLoading: loadingOrgUnits, data: searchOrgUnits } = useOrgUnitAutoSelect(queryOptions);

const updateUrlIfApplicable = useCallback(() => {
const paramsToAdd = {
Expand Down
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 '../../dataQueries';
import type { Props, RelatedStageDataValueStates } from './WidgetRelatedStages.types';
import { RelatedStagesActions } from './RelatedStagesActions';
import { relatedStageStatus } from './constants';
Expand Down Expand Up @@ -36,6 +37,15 @@ const WidgetRelatedStagesPlain = ({
orgUnit: undefined,
linkedEventId: undefined,
});
const { isLoading: orgUnitLoading, data } = useOrgUnitAutoSelect();
useEffect(() => {
if (!orgUnitLoading && data?.length === 1) {
setRelatedStageDataValues(prev => ({
...prev,
orgUnit: data[0],
}));
}
}, [data, orgUnitLoading, setRelatedStageDataValues]);

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

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

Expand All @@ -103,8 +113,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);
1 change: 1 addition & 0 deletions src/core_modules/capture-core/dataQueries/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { useOrganisationUnit } from './useOrganisationUnit';
export { useOrgUnitAutoSelect } from './useOrgUnitsForAutoSelect';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @flow
import { useApiMetadataQuery } from '../utils/reactQueryHelpers';

export const useOrgUnitAutoSelect = (customQueryOptions: Object) => {
const queryKey = ['organisationUnits'];
const queryFn = {
resource: 'organisationUnits',
params: {
fields: ['id, displayName~rename(name), path'],
withinUserHierarchy: true,
pageSize: 2,
},
};
const defaultQueryOptions = {
select: ({ organisationUnits }) => organisationUnits,
};

const queryOptions = { ...defaultQueryOptions, ...customQueryOptions };

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

return {
isLoading,
data,
};
};
Loading