Skip to content

Commit

Permalink
chore: use const
Browse files Browse the repository at this point in the history
  • Loading branch information
simonadomnisoru committed Jan 30, 2024
1 parent 50648b9 commit ffe6ab0
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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 : [] };
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
isNotValidOptionSet,
escapeString,
handleAPIResponse,
REQUESTED_ENTITIES,
} from '../helpers';
import type { QuerySingleResource } from '../../../../utils/api/api.types';

Expand Down Expand Up @@ -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]) || {};

Expand Down
Original file line number Diff line number Diff line change
@@ -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 [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -63,15 +63,15 @@ 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 };
});
},
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) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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]), [])
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/core_modules/capture-core/events/eventRequests.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<Object> = await apiEvents.reduce(async (accEventsPromise, apiEvent) => {
const accEvents = await accEventsPromise;
const eventContainer = await convertToClientEvent(apiEvent, absoluteApiPath, querySingleResource);
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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]) || {};

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/core_modules/capture-core/utils/api/handleAPIResponse.js
Original file line number Diff line number Diff line change
@@ -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 [];
Expand Down
2 changes: 1 addition & 1 deletion src/core_modules/capture-core/utils/api/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow
export { makeQuerySingleResource } from './makeQuerySingleResource';
export { handleAPIResponse } from './handleAPIResponse';
export { handleAPIResponse, REQUESTED_ENTITIES } from './handleAPIResponse';
export type * from './api.types';

0 comments on commit ffe6ab0

Please sign in to comment.