Skip to content

Commit

Permalink
feat: [DHIS2-17970] Auto-select orgUnit if there is only one available (
Browse files Browse the repository at this point in the history
#3798)

* feat: pre select

* feat: review changes

* fix: remove children check

* fix: review change

* feat: merge hooks for auto select

* Revert "feat: merge hooks for auto select"

This reverts commit 9b97d2e.

* fix: review changes
  • Loading branch information
henrikmv authored Oct 14, 2024
1 parent cd63f87 commit de06f8b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
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 type { ErrorMessagesForRelatedStages } from './RelatedStagesActions';
import { RelatedStagesActions } from './RelatedStagesActions';
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 @@ -104,8 +114,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,
};
};

0 comments on commit de06f8b

Please sign in to comment.