diff --git a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/hooks/useClientDataValues.js b/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/hooks/useClientDataValues.js index 3ca91ba898..7e85ede527 100644 --- a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/hooks/useClientDataValues.js +++ b/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/hooks/useClientDataValues.js @@ -8,7 +8,7 @@ import { subValueGetterByElementType } from '../utils/getSubValueForDataValue'; import { makeQuerySingleResource } from '../../../utils/api'; type Props = {| - linkedEventId: string, + linkedEventId: ?string, dataValues: Array<{| dataElement: string, value: any |}>, formFoundation: ?RenderFoundation, |} @@ -75,6 +75,7 @@ export const useClientDataValues = ({ // $FlowFixMe formFoundation, querySingleResource, + // $FlowFixMe linkedEventId, buildUrl(baseUrl, `api/${apiVersion}`), ), diff --git a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/hooks/useLinkedEventByOriginId.js b/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/hooks/useLinkedEventByOriginId.js index b8f1ec6055..fa876139d7 100644 --- a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/hooks/useLinkedEventByOriginId.js +++ b/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/hooks/useLinkedEventByOriginId.js @@ -80,12 +80,31 @@ export const useLinkedEventByOriginId = ({ originEventId }: Props) => { }; }, [data]); + // Add fallback query if relationship is missing eventData + const { + data: fallbackDataValues, + isLoading: isLoadingFallback, + isError: isErrorFallback, + } = useApiDataQuery( + ['linkedEventDataValuesFallback', linkedEvent?.event], + { + resource: 'tracker/events', + id: linkedEvent?.event, + params: { + fields: 'event,dataValues,occurredAt,scheduledAt,status,orgUnit,programStage,program', + }, + }, + { + enabled: !!linkedEvent?.event && !dataValues, + }, + ); + return { - linkedEvent, + linkedEvent: dataValues ? linkedEvent : fallbackDataValues, relationshipType, - dataValues, - isLoading, - isError, + dataValues: dataValues || fallbackDataValues?.dataValues, + isLoading: isLoading || isLoadingFallback, + isError: isError || isErrorFallback, error, }; };