diff --git a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/FormFoundation/DataElement.js b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/FormFoundation/DataElement.js index a94775628e..559182f26e 100644 --- a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/FormFoundation/DataElement.js +++ b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/FormFoundation/DataElement.js @@ -1,7 +1,6 @@ // @flow /* eslint-disable no-underscore-dangle */ import log from 'loglevel'; -import { handleAPIResponse } from 'capture-core/utils/api'; import i18n from '@dhis2/d2-i18n'; import { pipe, errorCreator } from 'capture-core-utils'; @@ -20,7 +19,14 @@ import { convertFormToClient, convertClientToServer } from '../../../../converte import { convertOptionSetValue } from '../../../../converters/serverToClient'; import { buildIcon } from '../../../../metaDataMemoryStoreBuilders/common/helpers'; import { OptionGroup } from '../../../../metaData/OptionSet/OptionGroup'; -import { getFeatureType, getDataElement, getLabel, isNotValidOptionSet, escapeString } from '../helpers'; +import { + getFeatureType, + getDataElement, + getLabel, + isNotValidOptionSet, + escapeString, + handleAPIResponse, +} from '../helpers'; import type { QuerySingleResource } from '../../../../utils/api/api.types'; const OPTION_SET_NOT_FOUND = 'Optionset not found'; diff --git a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/helpers/handleAPIResponse.js b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/helpers/handleAPIResponse.js new file mode 100644 index 0000000000..8ff870253e --- /dev/null +++ b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/helpers/handleAPIResponse.js @@ -0,0 +1,8 @@ +// @flow + +export const handleAPIResponse = (resourceName: string, apiResponse: any) => { + if (!apiResponse) { + return []; + } + return apiResponse[resourceName] || apiResponse.instances || []; +}; diff --git a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/helpers/index.js b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/helpers/index.js index 9a9b94144a..08be8b975c 100644 --- a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/helpers/index.js +++ b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/helpers/index.js @@ -16,3 +16,4 @@ export { GEOMETRY, getFeatureType, getDataElement, getLabel } from './geometry'; export { convertClientToView } from './convertClientToView'; export { isNotValidOptionSet } from './isNotValidOptionSet'; export { escapeString } from './escapeString'; +export { handleAPIResponse } from './handleAPIResponse'; diff --git a/src/core_modules/capture-core/components/WidgetsRelationship/WidgetTrackedEntityRelationship/NewTrackedEntityRelationship/hooks/useAddRelationship.js b/src/core_modules/capture-core/components/WidgetsRelationship/WidgetTrackedEntityRelationship/NewTrackedEntityRelationship/hooks/useAddRelationship.js index 9bdfd4b14d..e007b4184e 100644 --- a/src/core_modules/capture-core/components/WidgetsRelationship/WidgetTrackedEntityRelationship/NewTrackedEntityRelationship/hooks/useAddRelationship.js +++ b/src/core_modules/capture-core/components/WidgetsRelationship/WidgetTrackedEntityRelationship/NewTrackedEntityRelationship/hooks/useAddRelationship.js @@ -1,7 +1,9 @@ // @flow import i18n from '@dhis2/d2-i18n'; +import { FEATURES, useFeature } from 'capture-core-utils'; import { useDataEngine, useAlert } from '@dhis2/app-runtime'; import { useMutation, useQueryClient } from 'react-query'; +import { handleAPIResponse } from 'capture-core/utils/api'; type Props = { teiId: string; @@ -19,6 +21,7 @@ const addRelationshipMutation = { export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => { const queryClient = useQueryClient(); + const queryKey: string = useFeature(FEATURES.exportablePayload) ? 'relationships' : 'instances'; const dataEngine = useDataEngine(); const { show: showSnackbar } = useAlert( i18n.t('An error occurred while adding the relationship'), @@ -36,11 +39,12 @@ export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => { onError: (_, requestData) => { showSnackbar(); const apiRelationshipId = requestData.clientRelationship.relationship; - const currentRelationships = queryClient.getQueryData([ReactQueryAppNamespace, 'relationships', teiId]); + const apiResponse = queryClient.getQueryData([ReactQueryAppNamespace, 'relationships', teiId]); + const apiRelationships = handleAPIResponse('relationships', apiResponse); - if (!currentRelationships?.instances) return; + if (apiRelationships.length === 0) return; - const newRelationships = currentRelationships.instances.reduce((acc, relationship) => { + const newRelationships = apiRelationships.reduce((acc, relationship) => { if (relationship.relationship === apiRelationshipId) { return acc; } @@ -50,7 +54,7 @@ export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => { queryClient.setQueryData( [ReactQueryAppNamespace, 'relationships', teiId], - { instances: newRelationships }, + { [queryKey]: newRelationships }, ); }, onMutate: (...props) => { @@ -58,18 +62,19 @@ export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => { const { clientRelationship } = props[0]; if (!clientRelationship) return; - queryClient.setQueryData([ReactQueryAppNamespace, 'relationships', teiId], (oldData) => { - const instances = oldData?.instances || []; - const updatedInstances = [clientRelationship, ...instances]; - return { instances: updatedInstances }; + queryClient.setQueryData([ReactQueryAppNamespace, 'relationships', teiId], (apiResponse) => { + const apiRelationships = handleAPIResponse('relationships', apiResponse); + const updatedInstances = [clientRelationship, ...apiRelationships]; + return { [queryKey]: updatedInstances }; }); }, onSuccess: async (apiResponse, requestData) => { const apiRelationshipId = apiResponse.bundleReport.typeReportMap.RELATIONSHIP.objectReports[0].uid; const currentRelationships = queryClient.getQueryData([ReactQueryAppNamespace, 'relationships', teiId]); - if (!currentRelationships?.instances) return; + const apiRelationships = handleAPIResponse('relationships', currentRelationships); + if (apiRelationships.length === 0) return; - const newRelationships = currentRelationships.instances.map((relationship) => { + const newRelationships = apiRelationships.map((relationship) => { if (relationship.relationship === apiRelationshipId) { return { ...relationship, @@ -81,7 +86,7 @@ export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => { queryClient.setQueryData( [ReactQueryAppNamespace, 'relationships', teiId], - { instances: newRelationships }, + { [queryKey]: newRelationships }, ); onSuccess && onSuccess(apiResponse, requestData); }, diff --git a/src/core_modules/capture-core/components/WorkingLists/EventWorkingLists/epics/getEventListData.js b/src/core_modules/capture-core/components/WorkingLists/EventWorkingLists/epics/getEventListData.js index 7884ce257b..8a2a1a7e5c 100644 --- a/src/core_modules/capture-core/components/WorkingLists/EventWorkingLists/epics/getEventListData.js +++ b/src/core_modules/capture-core/components/WorkingLists/EventWorkingLists/epics/getEventListData.js @@ -182,7 +182,6 @@ export const getEventListData = async ({ querySingleResource: QuerySingleResource, }) => { const mainColumns = getMainColumns(columnsMetaForDataFetching); - // $FlowFixMe const { eventContainers, pagingData, request } = await getEvents( createApiQueryArgs(queryArgs, mainColumns, categoryCombinationId), absoluteApiPath, diff --git a/src/core_modules/capture-core/events/eventRequests.js b/src/core_modules/capture-core/events/eventRequests.js index 4e400072ca..7e27b20463 100644 --- a/src/core_modules/capture-core/events/eventRequests.js +++ b/src/core_modules/capture-core/events/eventRequests.js @@ -182,7 +182,7 @@ export async function getEvents( }); const apiEvents = handleAPIResponse('events', apiResponse); - const eventContainers = await apiEvents.reduce(async (accEventsPromise, apiEvent) => { + const eventContainers: Array = await apiEvents.reduce(async (accEventsPromise, apiEvent) => { const accEvents = await accEventsPromise; const eventContainer = await convertToClientEvent(apiEvent, absoluteApiPath, querySingleResource); if (eventContainer) { diff --git a/src/core_modules/capture-core/relationships/relationshipRequests.js b/src/core_modules/capture-core/relationships/relationshipRequests.js index 774f0ec487..e9d3bd1046 100644 --- a/src/core_modules/capture-core/relationships/relationshipRequests.js +++ b/src/core_modules/capture-core/relationships/relationshipRequests.js @@ -16,7 +16,7 @@ async function getRelationships( params: queryParams, }); const apiRelationships = handleAPIResponse('relationships', apiResponse); - // $FlowFixMe + // $FlowFixMe[missing-annot] return apiRelationships.map(rel => convertServerRelationshipToClient(rel, relationshipTypes)); } diff --git a/src/core_modules/capture-core/utils/api/handleAPIResponse.js b/src/core_modules/capture-core/utils/api/handleAPIResponse.js index afc29763e0..8ff870253e 100644 --- a/src/core_modules/capture-core/utils/api/handleAPIResponse.js +++ b/src/core_modules/capture-core/utils/api/handleAPIResponse.js @@ -1,18 +1,4 @@ // @flow -import { FEATURES, hasAPISupportForFeature } from 'capture-core-utils'; - -export const handleAPIResponseByMinorVersion = ( - resourceName: string, - apiResponse: any, - minorVersion: string | number, -) => { - if (!apiResponse) { - return []; - } - return hasAPISupportForFeature(minorVersion, FEATURES.exportablePayload) - ? apiResponse[resourceName] || [] - : apiResponse.instances; -}; export const handleAPIResponse = (resourceName: string, apiResponse: any) => { if (!apiResponse) {