From 155dc66e42a3f28ec68dd37b3c75c603ecf7fdae Mon Sep 17 00:00:00 2001 From: henrikmv Date: Tue, 24 Sep 2024 13:36:37 +0200 Subject: [PATCH] Revert "feat: merge hooks for auto select" This reverts commit 9b97d2e6499e9d3ba4b99068659495a355d83ddf. --- .../MetadataAutoSelectInitializer.js | 6 +-- .../hooks/useMetadataAutoSelect.js} | 49 +++++++++---------- .../WidgetRelatedStages.component.js | 13 +---- .../hooks/useAutoSelctOrgUnitRelatedStage.js | 29 +++++++++++ .../capture-core/dataQueries/index.js | 1 - 5 files changed, 57 insertions(+), 41 deletions(-) rename src/core_modules/capture-core/{dataQueries/useOrgUnitsForAutoSelect.js => components/MetadataAutoSelectInitializer/hooks/useMetadataAutoSelect.js} (62%) create mode 100644 src/core_modules/capture-core/components/WidgetRelatedStages/hooks/useAutoSelctOrgUnitRelatedStage.js diff --git a/src/core_modules/capture-core/components/MetadataAutoSelectInitializer/MetadataAutoSelectInitializer.js b/src/core_modules/capture-core/components/MetadataAutoSelectInitializer/MetadataAutoSelectInitializer.js index 317ba62919..a6161940c1 100644 --- a/src/core_modules/capture-core/components/MetadataAutoSelectInitializer/MetadataAutoSelectInitializer.js +++ b/src/core_modules/capture-core/components/MetadataAutoSelectInitializer/MetadataAutoSelectInitializer.js @@ -1,6 +1,6 @@ // @flow import * as React from 'react'; -import { useOrgUnitsForAutoSelect } from '../../dataQueries'; +import { useMetadataAutoSelect } from './hooks/useMetadataAutoSelect'; import { LoadingMaskForPage } from '../LoadingMasks'; type Props = {| @@ -8,9 +8,9 @@ type Props = {| |} export const MetadataAutoSelectInitializer = ({ children }: Props) => { - const { isLoading } = useOrgUnitsForAutoSelect(); + const { isReady } = useMetadataAutoSelect(); - if (isLoading) { + if (!isReady) { return ( ); diff --git a/src/core_modules/capture-core/dataQueries/useOrgUnitsForAutoSelect.js b/src/core_modules/capture-core/components/MetadataAutoSelectInitializer/hooks/useMetadataAutoSelect.js similarity index 62% rename from src/core_modules/capture-core/dataQueries/useOrgUnitsForAutoSelect.js rename to src/core_modules/capture-core/components/MetadataAutoSelectInitializer/hooks/useMetadataAutoSelect.js index 54297ef58c..90be846e65 100644 --- a/src/core_modules/capture-core/dataQueries/useOrgUnitsForAutoSelect.js +++ b/src/core_modules/capture-core/components/MetadataAutoSelectInitializer/hooks/useMetadataAutoSelect.js @@ -1,9 +1,9 @@ // @flow import { useCallback, useEffect, useState } from 'react'; import { useHistory } from 'react-router-dom'; -import { useApiMetadataQuery, useIndexedDBQuery } from '../utils/reactQueryHelpers'; -import { getUserStorageController, userStores } from '../storageControllers'; -import { buildUrlQueryString, useLocationQuery } from '../utils/routing'; +import { useApiMetadataQuery, useIndexedDBQuery } from '../../../utils/reactQueryHelpers'; +import { getUserStorageController, userStores } from '../../../storageControllers'; +import { buildUrlQueryString, useLocationQuery } from '../../../utils/routing'; const getAllPrograms = () => { const userStorageController = getUserStorageController(); @@ -13,7 +13,7 @@ const getAllPrograms = () => { }); }; -export const useOrgUnitsForAutoSelect = () => { +export const useMetadataAutoSelect = () => { const [mounted, setMounted] = useState(false); const history = useHistory(); const urlParams = useLocationQuery(); @@ -28,21 +28,21 @@ export const useOrgUnitsForAutoSelect = () => { }, ); - const queryKey = ['orgUnitsForAutoSelect']; - const queryFn = { - resource: 'organisationUnits', - params: { - fields: ['id, displayName~rename(name), path'], - withinUserHierarchy: true, - pageSize: 2, + const { data: searchOrgUnits, isLoading: loadingOrgUnits } = useApiMetadataQuery( + ['searchOrgUnitsForAutoSelect'], + { + resource: 'organisationUnits', + params: { + fields: 'id', + withinUserSearchHierarchy: true, + pageSize: 2, + }, }, - }; - const queryOptions = { - enabled: !mounted, - select: ({ organisationUnits }) => organisationUnits, - }; - - const { data: orgUnits, isLoading: loadingOrgUnits } = useApiMetadataQuery(queryKey, queryFn, queryOptions); + { + enabled: Object.keys(urlParams).length === 0 && !mounted, + select: ({ organisationUnits }) => organisationUnits, + }, + ); const updateUrlIfApplicable = useCallback(() => { const paramsToAdd = { @@ -52,14 +52,14 @@ export const useOrgUnitsForAutoSelect = () => { if (programs && programs.length === 1) { paramsToAdd.programId = programs[0].id; } - if (orgUnits && orgUnits.length === 1) { - paramsToAdd.orgUnitId = orgUnits[0].id; + if (searchOrgUnits && searchOrgUnits.length === 1) { + paramsToAdd.orgUnitId = searchOrgUnits[0].id; } if (Object.keys(paramsToAdd).length) { history.push(`?${buildUrlQueryString({ ...paramsToAdd })}`); } - }, [history, programs, orgUnits]); + }, [history, programs, searchOrgUnits]); useEffect(() => { if (mounted) return; @@ -79,14 +79,11 @@ export const useOrgUnitsForAutoSelect = () => { mounted, urlParams, loadingOrgUnits, - orgUnits, + searchOrgUnits, updateUrlIfApplicable, ]); - const isLoading = loadingPrograms || loadingOrgUnits; - return { - isLoading, - orgUnits, + isReady: mounted, }; }; diff --git a/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.component.js b/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.component.js index e6a974b3c5..b5d7087c34 100644 --- a/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.component.js +++ b/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.component.js @@ -1,7 +1,7 @@ // @flow import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useState } from 'react'; import { useRelatedStages } from './useRelatedStages'; -import { useOrgUnitsForAutoSelect } from '../../dataQueries'; +import { useOrgUnitAutoSelect } from './hooks/useAutoSelctOrgUnitRelatedStage'; import type { Props, RelatedStageDataValueStates } from './WidgetRelatedStages.types'; import { RelatedStagesActions } from './RelatedStagesActions'; import { relatedStageStatus } from './constants'; @@ -37,7 +37,7 @@ const WidgetRelatedStagesPlain = ({ orgUnit: undefined, linkedEventId: undefined, }); - const { isLoading: orgUnitLoading, orgUnits } = useOrgUnitsForAutoSelect(setRelatedStageDataValues); + const { isLoading: orgUnitLoading } = useOrgUnitAutoSelect(setRelatedStageDataValues); const addErrorMessage = (message: ErrorMessagesForRelatedStages) => { setErrorMessages((prevMessages: Object) => ({ @@ -46,15 +46,6 @@ const WidgetRelatedStagesPlain = ({ })); }; - useEffect(() => { - if (!orgUnitLoading && orgUnits?.length === 1) { - setRelatedStageDataValues(prev => ({ - ...prev, - orgUnit: orgUnits[0], - })); - } - }, [orgUnits, orgUnitLoading, setRelatedStageDataValues]); - const eventHasLinkableStageRelationship = () => currentRelatedStagesStatus === relatedStageStatus.LINKABLE; const formIsValidOnSave = () => { diff --git a/src/core_modules/capture-core/components/WidgetRelatedStages/hooks/useAutoSelctOrgUnitRelatedStage.js b/src/core_modules/capture-core/components/WidgetRelatedStages/hooks/useAutoSelctOrgUnitRelatedStage.js new file mode 100644 index 0000000000..00de49cee0 --- /dev/null +++ b/src/core_modules/capture-core/components/WidgetRelatedStages/hooks/useAutoSelctOrgUnitRelatedStage.js @@ -0,0 +1,29 @@ +// @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, displayName~rename(name), path'], + withinUserHierarchy: true, + pageSize: 2, + }, + }; + const { data, isLoading } = useApiMetadataQuery(queryKey, queryFn); + + useEffect(() => { + if (!isLoading && data?.organisationUnits?.length === 1) { + setRelatedStageDataValues(prev => ({ + ...prev, + orgUnit: data.organisationUnits[0], + })); + } + }, [data, isLoading, setRelatedStageDataValues]); + + return { + isLoading, + }; +}; diff --git a/src/core_modules/capture-core/dataQueries/index.js b/src/core_modules/capture-core/dataQueries/index.js index 9364e9ed19..913bca576a 100644 --- a/src/core_modules/capture-core/dataQueries/index.js +++ b/src/core_modules/capture-core/dataQueries/index.js @@ -1,2 +1 @@ export { useOrganisationUnit } from './useOrganisationUnit'; -export { useOrgUnitsForAutoSelect } from './useOrgUnitsForAutoSelect';