Skip to content

Commit

Permalink
Revert "feat: merge hooks for auto select"
Browse files Browse the repository at this point in the history
This reverts commit 9b97d2e.
  • Loading branch information
henrikmv committed Sep 24, 2024
1 parent 9b97d2e commit 155dc66
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// @flow
import * as React from 'react';
import { useOrgUnitsForAutoSelect } from '../../dataQueries';
import { useMetadataAutoSelect } from './hooks/useMetadataAutoSelect';
import { LoadingMaskForPage } from '../LoadingMasks';

type Props = {|
children: React.Node,
|}

export const MetadataAutoSelectInitializer = ({ children }: Props) => {
const { isLoading } = useOrgUnitsForAutoSelect();
const { isReady } = useMetadataAutoSelect();

if (isLoading) {
if (!isReady) {
return (
<LoadingMaskForPage />
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -13,7 +13,7 @@ const getAllPrograms = () => {
});
};

export const useOrgUnitsForAutoSelect = () => {
export const useMetadataAutoSelect = () => {
const [mounted, setMounted] = useState(false);
const history = useHistory();
const urlParams = useLocationQuery();
Expand All @@ -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 = {
Expand All @@ -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;
Expand All @@ -79,14 +79,11 @@ export const useOrgUnitsForAutoSelect = () => {
mounted,
urlParams,
loadingOrgUnits,
orgUnits,
searchOrgUnits,
updateUrlIfApplicable,
]);

const isLoading = loadingPrograms || loadingOrgUnits;

return {
isLoading,
orgUnits,
isReady: mounted,
};
};
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 { 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';
Expand Down Expand Up @@ -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) => ({
Expand All @@ -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 = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
};
};
1 change: 0 additions & 1 deletion src/core_modules/capture-core/dataQueries/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { useOrganisationUnit } from './useOrganisationUnit';
export { useOrgUnitsForAutoSelect } from './useOrgUnitsForAutoSelect';

0 comments on commit 155dc66

Please sign in to comment.