Skip to content

Commit

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

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

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

if (!isReady) {
if (isLoading) {
return (
<LoadingMaskForPage />
);
Expand Down
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 { useOrgUnitAutoSelect } from './hooks/useAutoSelctOrgUnitRelatedStage';
import { useOrgUnitsForAutoSelect } from '../../dataQueries';
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 } = useOrgUnitAutoSelect(setRelatedStageDataValues);
const { isLoading: orgUnitLoading, orgUnits } = useOrgUnitsForAutoSelect(setRelatedStageDataValues);

const addErrorMessage = (message: ErrorMessagesForRelatedStages) => {
setErrorMessages((prevMessages: Object) => ({
Expand All @@ -46,6 +46,15 @@ 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

This file was deleted.

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 { useOrgUnitsForAutoSelect } from './useOrgUnitsForAutoSelect';
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 useMetadataAutoSelect = () => {
export const useOrgUnitsForAutoSelect = () => {
const [mounted, setMounted] = useState(false);
const history = useHistory();
const urlParams = useLocationQuery();
Expand All @@ -28,21 +28,21 @@ export const useMetadataAutoSelect = () => {
},
);

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

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

const updateUrlIfApplicable = useCallback(() => {
const paramsToAdd = {
Expand All @@ -52,14 +52,14 @@ export const useMetadataAutoSelect = () => {
if (programs && programs.length === 1) {
paramsToAdd.programId = programs[0].id;
}
if (searchOrgUnits && searchOrgUnits.length === 1) {
paramsToAdd.orgUnitId = searchOrgUnits[0].id;
if (orgUnits && orgUnits.length === 1) {
paramsToAdd.orgUnitId = orgUnits[0].id;
}

if (Object.keys(paramsToAdd).length) {
history.push(`?${buildUrlQueryString({ ...paramsToAdd })}`);
}
}, [history, programs, searchOrgUnits]);
}, [history, programs, orgUnits]);

useEffect(() => {
if (mounted) return;
Expand All @@ -79,11 +79,14 @@ export const useMetadataAutoSelect = () => {
mounted,
urlParams,
loadingOrgUnits,
searchOrgUnits,
orgUnits,
updateUrlIfApplicable,
]);

const isLoading = loadingPrograms || loadingOrgUnits;

return {
isReady: mounted,
isLoading,
orgUnits,
};
};

0 comments on commit 9b97d2e

Please sign in to comment.