Skip to content

Commit

Permalink
fix: add pteas
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikhaugstulen committed Nov 27, 2023
1 parent df3462e commit 03f0db9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ export const RegisterTei = ({
const error = useSelector(({ newRelationshipRegisterTei }) => (newRelationshipRegisterTei.error));
const selectedScopeId = suggestedProgramId || trackedEntityTypeId;
const { trackedEntityName } = useScopeInfo(selectedScopeId);
const {
inheritedAttributes,
isLoading: isLoadingAttributes,
} = useInheritedAttributeValues({
const { inheritedAttributes, isLoading: isLoadingAttributes } = useInheritedAttributeValues({
teiId,
trackedEntityTypeId,
programId: suggestedProgramId,
});

if (isLoadingAttributes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// @flow
import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import type { InputAttribute } from '../../../DataEntries/EnrollmentRegistrationEntry/hooks/useFormValues';
import { useApiDataQuery } from '../../../../utils/reactQueryHelpers';
import { getTrackedEntityTypeThrowIfNotFound } from '../../../../metaData';
import {
getProgramFromProgramIdThrowIfNotFound,
getTrackedEntityTypeThrowIfNotFound,
TrackerProgram,
} from '../../../../metaData';

type Props = {
teiId: string,
Expand All @@ -13,21 +19,40 @@ type Return = {
isLoading: boolean,
};
export const useInheritedAttributeValues = ({ teiId, trackedEntityTypeId }: Props): Return => {
const trackedEntityType = getTrackedEntityTypeThrowIfNotFound(trackedEntityTypeId);
const inheritedAttributeIds = trackedEntityType.attributes?.reduce((acc, attribute) => {
if (attribute.inherit) {
acc.add(attribute.id);
const programId = useSelector(({ newRelationshipRegisterTei }) => newRelationshipRegisterTei.programId);
const inheritedAttributeIds = useMemo(() => {
const attributeIds = new Set();

if (programId) {
const program = getProgramFromProgramIdThrowIfNotFound(programId);
if (program instanceof TrackerProgram) {
program.attributes.forEach((attribute) => {
if (attribute.inherit) {
attributeIds.add(attribute.id);
}
});
}
return attributeIds;
}
return acc;
}, new Set());

const trackedEntityType = getTrackedEntityTypeThrowIfNotFound(trackedEntityTypeId);
trackedEntityType.attributes.forEach((attribute) => {
if (attribute.inherit) {
attributeIds.add(attribute.id);
}
});
return attributeIds;
}, [programId, trackedEntityTypeId]);


const { data, isLoading } = useApiDataQuery(
['inheritedAttributeValues', teiId],
['inheritedAttributeValues', teiId, programId],
{
resource: 'tracker/trackedEntities',
id: teiId,
params: {
fields: ['attributes'],
program: programId,
},
}, {
enabled: !!teiId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Result } from './useMetadataQuery.types';
import { ReactQueryAppNamespace } from '../reactQueryHelpers.const';

export const useApiDataQuery = <TResultData>(
queryKey: Array<string | number>,
queryKey: Array<string | number | null | void>,
queryObject: ResourceQuery,
queryOptions: UseQueryOptions<TResultData>,
): Result<TResultData> => {
Expand Down

0 comments on commit 03f0db9

Please sign in to comment.