diff --git a/src/core_modules/capture-core/components/WidgetEventSchedule/hooks/useEventsInOrgUnit.js b/src/core_modules/capture-core/components/WidgetEventSchedule/hooks/useEventsInOrgUnit.js index 15b3b09c01..c7227e8a81 100644 --- a/src/core_modules/capture-core/components/WidgetEventSchedule/hooks/useEventsInOrgUnit.js +++ b/src/core_modules/capture-core/components/WidgetEventSchedule/hooks/useEventsInOrgUnit.js @@ -1,6 +1,6 @@ // @flow import { useMemo, useEffect } from 'react'; -import { handleAPIResponse } from 'capture-core/utils/api'; +import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; import { useDataQuery } from '@dhis2/app-runtime'; export const useEventsInOrgUnit = (orgUnitId: string, selectedDate: string) => { @@ -32,6 +32,6 @@ export const useEventsInOrgUnit = (orgUnitId: string, selectedDate: string) => { } }, [refetch, orgUnitId, selectedDate]); - const apiEvents = handleAPIResponse('events', data?.events); + const apiEvents = handleAPIResponse(REQUESTED_ENTITIES.events, data?.events); return { error, events: !loading && data ? apiEvents : [] }; }; 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 559182f26e..bc6a9997b6 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 @@ -26,6 +26,7 @@ import { isNotValidOptionSet, escapeString, handleAPIResponse, + REQUESTED_ENTITIES, } from '../helpers'; import type { QuerySingleResource } from '../../../../utils/api/api.types'; @@ -88,7 +89,7 @@ const buildDataElementUnique = ( }); } return requestPromise.then((result) => { - const apiTrackedEntities = handleAPIResponse('trackedEntities', result); + const apiTrackedEntities = handleAPIResponse(REQUESTED_ENTITIES.trackedEntities, result); const otherTrackedEntityInstances = apiTrackedEntities.filter(item => item.trackedEntity !== contextProps.trackedEntityInstanceId); const trackedEntityInstance = (otherTrackedEntityInstances && otherTrackedEntityInstances[0]) || {}; 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 index 8ff870253e..e7fadf3307 100644 --- a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/helpers/handleAPIResponse.js +++ b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/helpers/handleAPIResponse.js @@ -1,5 +1,11 @@ // @flow +export const REQUESTED_ENTITIES = Object.freeze({ + events: 'events', + trackedEntities: 'trackedEntities', + relationships: 'relationships', +}); + export const handleAPIResponse = (resourceName: string, apiResponse: any) => { if (!apiResponse) { return []; 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 08be8b975c..f506f5c5d4 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,4 +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'; +export { handleAPIResponse, REQUESTED_ENTITIES } 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 e007b4184e..3342810e0b 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 @@ -3,7 +3,7 @@ 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'; +import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; type Props = { teiId: string; @@ -40,7 +40,7 @@ export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => { showSnackbar(); const apiRelationshipId = requestData.clientRelationship.relationship; const apiResponse = queryClient.getQueryData([ReactQueryAppNamespace, 'relationships', teiId]); - const apiRelationships = handleAPIResponse('relationships', apiResponse); + const apiRelationships = handleAPIResponse(REQUESTED_ENTITIES.relationships, apiResponse); if (apiRelationships.length === 0) return; @@ -63,7 +63,7 @@ export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => { if (!clientRelationship) return; queryClient.setQueryData([ReactQueryAppNamespace, 'relationships', teiId], (apiResponse) => { - const apiRelationships = handleAPIResponse('relationships', apiResponse); + const apiRelationships = handleAPIResponse(REQUESTED_ENTITIES.relationships, apiResponse); const updatedInstances = [clientRelationship, ...apiRelationships]; return { [queryKey]: updatedInstances }; }); @@ -71,7 +71,7 @@ export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => { onSuccess: async (apiResponse, requestData) => { const apiRelationshipId = apiResponse.bundleReport.typeReportMap.RELATIONSHIP.objectReports[0].uid; const currentRelationships = queryClient.getQueryData([ReactQueryAppNamespace, 'relationships', teiId]); - const apiRelationships = handleAPIResponse('relationships', currentRelationships); + const apiRelationships = handleAPIResponse(REQUESTED_ENTITIES.relationships, currentRelationships); if (apiRelationships.length === 0) return; const newRelationships = apiRelationships.map((relationship) => { diff --git a/src/core_modules/capture-core/components/WidgetsRelationship/common/useRelationships/useRelationships.js b/src/core_modules/capture-core/components/WidgetsRelationship/common/useRelationships/useRelationships.js index 9634a0a944..fdd63ffa2c 100644 --- a/src/core_modules/capture-core/components/WidgetsRelationship/common/useRelationships/useRelationships.js +++ b/src/core_modules/capture-core/components/WidgetsRelationship/common/useRelationships/useRelationships.js @@ -1,6 +1,6 @@ // @flow import { useMemo } from 'react'; -import { handleAPIResponse } from 'capture-core/utils/api'; +import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; import { useApiDataQuery } from '../../../../utils/reactQueryHelpers'; import type { InputRelationshipData, RelationshipTypes } from '../Types'; import { determineLinkedEntity } from '../RelationshipsWidget/useGroupedLinkedEntities'; @@ -35,7 +35,7 @@ export const useRelationships = ({ entityId, searchMode, relationshipTypes }: Pr { enabled: !!entityId, select: (apiResponse: any) => { - const apiRelationships = handleAPIResponse('relationships', apiResponse); + const apiRelationships = handleAPIResponse(REQUESTED_ENTITIES.relationships, apiResponse); if (!relationshipTypes?.length || !apiRelationships?.length) { return []; } diff --git a/src/core_modules/capture-core/components/WorkingLists/TeiWorkingLists/epics/teiViewEpics/helpers/getEventListData/getEventListData.js b/src/core_modules/capture-core/components/WorkingLists/TeiWorkingLists/epics/teiViewEpics/helpers/getEventListData/getEventListData.js index 43c52fa5e2..e12d2d43fb 100644 --- a/src/core_modules/capture-core/components/WorkingLists/TeiWorkingLists/epics/teiViewEpics/helpers/getEventListData/getEventListData.js +++ b/src/core_modules/capture-core/components/WorkingLists/TeiWorkingLists/epics/teiViewEpics/helpers/getEventListData/getEventListData.js @@ -1,5 +1,5 @@ // @flow -import { handleAPIResponse } from 'capture-core/utils/api'; +import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; import { convertToClientEvents } from './convertToClientEvents'; import { getSubvalues, @@ -68,7 +68,7 @@ export const getEventListData = async ( resource: resourceEvents, params: queryArgsEvents, }); - const apiEvents = handleAPIResponse('events', apiEventsResponse); + const apiEvents = handleAPIResponse(REQUESTED_ENTITIES.events, apiEventsResponse); const trackedEntityIds = apiEvents .reduce((acc, { trackedEntity }) => (acc.includes(trackedEntity) ? acc : [...acc, trackedEntity]), []) @@ -82,7 +82,7 @@ export const getEventListData = async ( resource: resourceTEIs, params: queryArgsTEIs, }); - const apiTrackedEntities = handleAPIResponse('trackedEntities', apiTEIResponse); + const apiTrackedEntities = handleAPIResponse(REQUESTED_ENTITIES.trackedEntities, apiTEIResponse); const columnsMetaForDataFetchingArray = getColumnsQueryArgs(columnsMetaForDataFetching); const clientEvents = convertToClientEvents(addTEIsData(apiEvents, apiTrackedEntities), columnsMetaForDataFetchingArray); diff --git a/src/core_modules/capture-core/components/WorkingLists/TeiWorkingLists/epics/teiViewEpics/helpers/getTeiListData/getTeiListData.js b/src/core_modules/capture-core/components/WorkingLists/TeiWorkingLists/epics/teiViewEpics/helpers/getTeiListData/getTeiListData.js index 7829289f32..f13196223e 100644 --- a/src/core_modules/capture-core/components/WorkingLists/TeiWorkingLists/epics/teiViewEpics/helpers/getTeiListData/getTeiListData.js +++ b/src/core_modules/capture-core/components/WorkingLists/TeiWorkingLists/epics/teiViewEpics/helpers/getTeiListData/getTeiListData.js @@ -1,5 +1,5 @@ // @flow -import { handleAPIResponse } from 'capture-core/utils/api'; +import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; import { convertToClientTeis } from './convertToClientTeis'; import { getSubvalues, getApiFilterQueryArgs, getMainApiFilterQueryArgs } from '../getListDataCommon'; import type { RawQueryArgs } from './types'; @@ -46,7 +46,7 @@ export const getTeiListData = async ( resource, params: queryArgs, }); - const apiTrackedEntities = handleAPIResponse('trackedEntities', apiResponse); + const apiTrackedEntities = handleAPIResponse(REQUESTED_ENTITIES.trackedEntities, apiResponse); const columnsMetaForDataFetchingArray = [...columnsMetaForDataFetching.values()]; const clientTeis = convertToClientTeis(apiTrackedEntities, columnsMetaForDataFetchingArray, rawQueryArgs.programId); const clientTeisWithSubvalues = await getSubvalues(querySingleResource, absoluteApiPath)(clientTeis, columnsMetaForDataFetchingArray); diff --git a/src/core_modules/capture-core/events/eventRequests.js b/src/core_modules/capture-core/events/eventRequests.js index 196a257ae5..c8e6ca1695 100644 --- a/src/core_modules/capture-core/events/eventRequests.js +++ b/src/core_modules/capture-core/events/eventRequests.js @@ -1,6 +1,6 @@ // @flow import log from 'loglevel'; -import { handleAPIResponse } from 'capture-core/utils/api'; +import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; import { errorCreator } from 'capture-core-utils'; import { programCollection } from '../metaDataMemoryStores/programCollection/programCollection'; import { convertValue } from '../converters/serverToClient'; @@ -181,7 +181,7 @@ export async function getEvents( params: queryParams, }); - const apiEvents = handleAPIResponse('events', apiResponse); + const apiEvents = handleAPIResponse(REQUESTED_ENTITIES.events, apiResponse); const eventContainers: Array = await apiEvents.reduce(async (accEventsPromise, apiEvent) => { const accEvents = await accEventsPromise; const eventContainer = await convertToClientEvent(apiEvent, absoluteApiPath, querySingleResource); diff --git a/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/DataElementFactory.js b/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/DataElementFactory.js index 09e8d29c0c..b8591e344d 100644 --- a/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/DataElementFactory.js +++ b/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/DataElementFactory.js @@ -1,7 +1,7 @@ // @flow /* eslint-disable no-underscore-dangle */ import log from 'loglevel'; -import { handleAPIResponse } from 'capture-core/utils/api'; +import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; import i18n from '@dhis2/d2-i18n'; import { pipe, errorCreator } from 'capture-core-utils'; @@ -107,7 +107,7 @@ export class DataElementFactory { } return requestPromise .then((result) => { - const apiTrackedEntities = handleAPIResponse('trackedEntities', result); + const apiTrackedEntities = handleAPIResponse(REQUESTED_ENTITIES.trackedEntities, result); const otherTrackedEntityInstances = apiTrackedEntities.filter(item => item.trackedEntity !== contextProps.trackedEntityInstanceId); const trackedEntityInstance = (otherTrackedEntityInstances && otherTrackedEntityInstances[0]) || {}; diff --git a/src/core_modules/capture-core/metaDataMemoryStoreBuilders/trackedEntityTypes/factory/TrackedEntityType/DataElementFactory.js b/src/core_modules/capture-core/metaDataMemoryStoreBuilders/trackedEntityTypes/factory/TrackedEntityType/DataElementFactory.js index 0ffecd6da5..c9dd2b7c69 100644 --- a/src/core_modules/capture-core/metaDataMemoryStoreBuilders/trackedEntityTypes/factory/TrackedEntityType/DataElementFactory.js +++ b/src/core_modules/capture-core/metaDataMemoryStoreBuilders/trackedEntityTypes/factory/TrackedEntityType/DataElementFactory.js @@ -1,7 +1,7 @@ // @flow /* eslint-disable no-underscore-dangle */ import log from 'loglevel'; -import { handleAPIResponse } from 'capture-core/utils/api'; +import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; import i18n from '@dhis2/d2-i18n'; import { pipe, errorCreator } from 'capture-core-utils'; import type { @@ -169,7 +169,7 @@ export class DataElementFactory { } return requestPromise .then((result) => { - const apiTrackedEntities = handleAPIResponse('trackedEntities', result); + const apiTrackedEntities = handleAPIResponse(REQUESTED_ENTITIES.trackedEntities, result); const trackedEntityInstance = apiTrackedEntities[0] || {}; const data = { id: trackedEntityInstance.trackedEntity, diff --git a/src/core_modules/capture-core/relationships/relationshipRequests.js b/src/core_modules/capture-core/relationships/relationshipRequests.js index e9d3bd1046..7507ac43a1 100644 --- a/src/core_modules/capture-core/relationships/relationshipRequests.js +++ b/src/core_modules/capture-core/relationships/relationshipRequests.js @@ -1,5 +1,5 @@ // @flow -import { handleAPIResponse } from 'capture-core/utils/api'; +import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; import { getProgramThrowIfNotFound, EventProgram } from '../metaData'; import type { RelationshipType } from '../metaData'; import type { QuerySingleResource } from '../utils/api/api.types'; @@ -15,7 +15,7 @@ async function getRelationships( resource: 'tracker/relationships', params: queryParams, }); - const apiRelationships = handleAPIResponse('relationships', apiResponse); + const apiRelationships = handleAPIResponse(REQUESTED_ENTITIES.relationships, apiResponse); // $FlowFixMe[missing-annot] return apiRelationships.map(rel => convertServerRelationshipToClient(rel, relationshipTypes)); } diff --git a/src/core_modules/capture-core/trackedEntityInstances/trackedEntityInstanceRequests.js b/src/core_modules/capture-core/trackedEntityInstances/trackedEntityInstanceRequests.js index 6c5b634419..01920797e7 100644 --- a/src/core_modules/capture-core/trackedEntityInstances/trackedEntityInstanceRequests.js +++ b/src/core_modules/capture-core/trackedEntityInstances/trackedEntityInstanceRequests.js @@ -1,5 +1,5 @@ // @flow -import { handleAPIResponse } from 'capture-core/utils/api'; +import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; import { type DataElement, convertDataElementsValues } from '../metaData'; import { convertValue } from '../converters/serverToClient'; import { getSubValues } from './getSubValues'; @@ -68,7 +68,7 @@ export async function getTrackedEntityInstances( resource: 'tracker/trackedEntities', params: queryParams, }); - const apiTrackedEntities = handleAPIResponse('trackedEntities', apiResponse); + const apiTrackedEntities = handleAPIResponse(REQUESTED_ENTITIES.trackedEntities, apiResponse); const trackedEntityInstanceContainers = await apiTrackedEntities.reduce(async (accTeiPromise, apiTei) => { const accTeis = await accTeiPromise; diff --git a/src/core_modules/capture-core/utils/api/handleAPIResponse.js b/src/core_modules/capture-core/utils/api/handleAPIResponse.js index 8ff870253e..e7fadf3307 100644 --- a/src/core_modules/capture-core/utils/api/handleAPIResponse.js +++ b/src/core_modules/capture-core/utils/api/handleAPIResponse.js @@ -1,5 +1,11 @@ // @flow +export const REQUESTED_ENTITIES = Object.freeze({ + events: 'events', + trackedEntities: 'trackedEntities', + relationships: 'relationships', +}); + export const handleAPIResponse = (resourceName: string, apiResponse: any) => { if (!apiResponse) { return []; diff --git a/src/core_modules/capture-core/utils/api/index.js b/src/core_modules/capture-core/utils/api/index.js index 1cdfaed41d..97df87a704 100644 --- a/src/core_modules/capture-core/utils/api/index.js +++ b/src/core_modules/capture-core/utils/api/index.js @@ -1,4 +1,4 @@ // @flow export { makeQuerySingleResource } from './makeQuerySingleResource'; -export { handleAPIResponse } from './handleAPIResponse'; +export { handleAPIResponse, REQUESTED_ENTITIES } from './handleAPIResponse'; export type * from './api.types';