Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [DHIS2-17171] preview images in versions prior to 41 #3694

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@ const getFileResourceSubvalue = async ({ attribute, querySingleResource }: SubVa
};
};

const getImageResourceSubvalue = async ({ attribute, querySingleResource, minorServerVersion }: SubValueFunctionParams) => {
const getImageResourceSubvalue = async ({ attribute, minorServerVersion }: SubValueFunctionParams) => {
const { id, value, teiId, programId, absoluteApiPath } = attribute;
if (!value) return null;

const { displayName } = await querySingleResource({ resource: 'fileResources', id: value });

const urls = hasAPISupportForFeature(minorServerVersion, FEATURES.trackerImageEndpoint) ? {
url: `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${id}/image?program=${programId}`,
previewUrl: `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${id}/image?program=${programId}&dimension=small`,
} : {
url: `${absoluteApiPath}/trackedEntityInstances/${teiId}/${id}/image`,
previewUrl: `${absoluteApiPath}/trackedEntityInstances/${teiId}/${id}/image`,
url: `${absoluteApiPath}/trackedEntityInstances/${teiId}/${id}/image?program=${programId}`,
previewUrl: `${absoluteApiPath}/trackedEntityInstances/${teiId}/${id}/image?program=${programId}&dimension=SMALL`,
};

return {
name: displayName,
value,
...urls,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,32 @@ const getFileResourceSubvalue = async (keys: Object, querySingleResource: QueryS
}, {});
};

const getImageSubvalue = async (keys: Object, querySingleResource: QuerySingleResource, eventId: string, absoluteApiPath: string) => {
const promises = Object.keys(keys)
.map(async (key) => {
const getImageSubvalue = (keys: Object, querySingleResource: QuerySingleResource, eventId: string, absoluteApiPath: string) => (
Object.keys(keys)
.map((key) => {
const value = keys[key];
if (value) {
const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` });
return {
id,
name,
value,
...(featureAvailable(FEATURES.trackerImageEndpoint) ?
{
url: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${key}/image`,
previewUrl: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${key}/image?dimension=small`,
} : {
url: `${absoluteApiPath}/events/files?dataElementUid=${key}&eventUid=${eventId}`,
previewUrl: `${absoluteApiPath}/events/files?dataElementUid=${key}&eventUid=${eventId}`,
previewUrl: `${absoluteApiPath}/events/files?dataElementUid=${key}&eventUid=${eventId}&dimension=SMALL`,
}
),
};
}
return {};
});

return (await Promise.all(promises))
.reduce((acc, { id, name, url, previewUrl }) => {
if (id) {
acc[id] = { value: id, name, url, previewUrl };
}).reduce((acc, { value, url, previewUrl }) => {
if (value) {
acc[value] = { value, url, previewUrl };
}
return acc;
}, {});
};

}, {})
);

const getOrganisationUnitSubvalue = async (keys: Object, querySingleResource: QuerySingleResource) => {
const ids = Object.values(keys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const buildTEIRecord = ({
imageUrl: `/tracker/trackedEntities/${trackedEntity}/attributes/${id}/image?program=${programId}`,
previewUrl: `/tracker/trackedEntities/${trackedEntity}/attributes/${id}/image?program=${programId}&dimension=small`,
} : {
imageUrl: `/trackedEntityInstances/${trackedEntity}/${id}/image`,
previewUrl: `/trackedEntityInstances/${trackedEntity}/${id}/image`,
imageUrl: `/trackedEntityInstances/${trackedEntity}/${id}/image?program=${programId}`,
previewUrl: `/trackedEntityInstances/${trackedEntity}/${id}/image?program=${programId}&dimension=SMALL`,
}
))() : {};

Expand Down Expand Up @@ -94,7 +94,7 @@ const buildEventRecord = ({
previewUrl: `/tracker/events/${apiEvent.event}/dataValues/${id}/image?dimension=small`,
} : {
imageUrl: `/events/files?dataElementUid=${id}&eventUid=${apiEvent.event}`,
previewUrl: `/events/files?dataElementUid=${id}&eventUid=${apiEvent.event}`,
previewUrl: `/events/files?dataElementUid=${id}&eventUid=${apiEvent.event}&dimension=SMALL`,
}
))() : {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { TeiColumnMetaForDataFetching } from '../../../../types';
import type { QuerySingleResource } from '../../../../../../../utils/api';

const getSubvaluesPlain = (querySingleResource: QuerySingleResource, absoluteApiPath: string) => {
const getImageOrFileResourceSubvalue = async (keys: Array<string>) => {
const getFileResourceSubvalue = async (keys: Array<string>) => {
const promises = keys
.map(async (key) => {
const { id, displayName: name } = await querySingleResource({ resource: 'fileResources', id: key });
Expand All @@ -31,27 +31,31 @@ const getSubvaluesPlain = (querySingleResource: QuerySingleResource, absoluteApi
}, {});
};

const getImageResourceSubvalue = (keys: Array<string>) =>
keys.reduce((acc, key) => {
acc[key] = key;
return acc;
}, {});

const getOrganisationUnitSubvalue = async (keys: Array<string>) =>
getOrgUnitNames(keys, querySingleResource);

const subvalueGetterByType: {|
[string]: any,
|} = {
[dataElementTypes.ORGANISATION_UNIT]: getOrganisationUnitSubvalue,
[dataElementTypes.IMAGE]: getImageOrFileResourceSubvalue,
[dataElementTypes.FILE_RESOURCE]: getImageOrFileResourceSubvalue,
[dataElementTypes.IMAGE]: getImageResourceSubvalue,
[dataElementTypes.FILE_RESOURCE]: getFileResourceSubvalue,
};

const subvaluePostProcessorByType: {|
[string]: any,
|} = {
[dataElementTypes.IMAGE]: ({
subvalueKey: value,
subvalue: name,
imageUrl,
previewUrl,
}) => ({
name,
value,
url: `${absoluteApiPath}${imageUrl}`,
previewUrl: `${absoluteApiPath}${previewUrl}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export const convertToClientTeis = (
imageUrl: `/tracker/trackedEntities/${tei.trackedEntity}/attributes/${id}/image?program=${programId}`,
previewUrl: `/tracker/trackedEntities/${tei.trackedEntity}/attributes/${id}/image?program=${programId}&dimension=small`,
} : {
imageUrl: `/trackedEntityInstances/${tei.trackedEntity}/${id}/image`,
previewUrl: `/trackedEntityInstances/${tei.trackedEntity}/${id}/image`,
imageUrl: `/trackedEntityInstances/${tei.trackedEntity}/${id}/image?program=${programId}`,
previewUrl: `/trackedEntityInstances/${tei.trackedEntity}/${id}/image?program=${programId}&dimension=SMALL`,
}
))() : {};

Expand Down
8 changes: 1 addition & 7 deletions src/core_modules/capture-core/converters/clientToList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import moment from 'moment';
import i18n from '@dhis2/d2-i18n';
import { Tag } from '@dhis2/ui';
import { PreviewImage } from 'capture-ui';
import { featureAvailable, FEATURES } from 'capture-core-utils';
import { dataElementTypes, type DataElement } from '../metaData';
import { convertMomentToDateFormatString } from '../utils/converters/date';
import { stringifyNumber } from './common/stringifyNumber';
Expand Down Expand Up @@ -60,12 +59,7 @@ function convertImageForDisplay(clientValue: ImageClientValue) {
if (typeof clientValue === 'string' || clientValue instanceof String) {
return clientValue;
}
return featureAvailable(FEATURES.trackerImageEndpoint) ? (
<PreviewImage
url={clientValue.url}
previewUrl={clientValue.previewUrl}
/>
) : convertFileForDisplay(clientValue);
return <PreviewImage url={clientValue.url} previewUrl={clientValue.previewUrl} />;
}

function convertRangeForDisplay(parser: any, clientValue: any) {
Expand Down
10 changes: 1 addition & 9 deletions src/core_modules/capture-core/converters/clientToView.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React from 'react';
import moment from 'moment';
import i18n from '@dhis2/d2-i18n';
import { PreviewImage } from 'capture-ui';
import { featureAvailable, FEATURES } from 'capture-core-utils';
import { dataElementTypes, type DataElement } from '../metaData';
import { convertMomentToDateFormatString } from '../utils/converters/date';
import { stringifyNumber } from './common/stringifyNumber';
Expand Down Expand Up @@ -52,16 +51,9 @@ function convertFileForDisplay(clientValue: FileClientValue) {
}

function convertImageForDisplay(clientValue: ImageClientValue) {
return featureAvailable(FEATURES.trackerImageEndpoint) ? (
<PreviewImage
url={clientValue.url}
previewUrl={clientValue.previewUrl}
alignLeft
/>
) : convertFileForDisplay(clientValue);
return <PreviewImage url={clientValue.url} previewUrl={clientValue.previewUrl} alignLeft />;
}


const valueConvertersForType = {
[dataElementTypes.NUMBER]: stringifyNumber,
[dataElementTypes.INTEGER]: stringifyNumber,
Expand Down
32 changes: 9 additions & 23 deletions src/core_modules/capture-core/events/getSubValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,23 @@ const subValueGetterByElementType = {
return null;
}),
[dataElementTypes.IMAGE]: ({
value,
eventId,
metaElementId,
absoluteApiPath,
querySingleResource,
}: {
value: any,
eventId: string,
metaElementId: string,
absoluteApiPath: string,
querySingleResource: QuerySingleResource,
}) =>
querySingleResource({ resource: `fileResources/${value}` })
.then(res =>
({
name: res.name,
value: res.id,
...(featureAvailable(FEATURES.trackerImageEndpoint) ?
{
url: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${metaElementId}/image`,
previewUrl: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${metaElementId}/image?dimension=small`,
} : {
url: `${absoluteApiPath}/events/files?dataElementUid=${metaElementId}&eventUid=${eventId}`,
previewUrl: `${absoluteApiPath}/events/files?dataElementUid=${metaElementId}&eventUid=${eventId}`,
}
),
}))
.catch((error) => {
log.warn(errorCreator(GET_SUBVALUE_ERROR)({ value, eventId, metaElementId, error }));
return null;
}),
(featureAvailable(FEATURES.trackerImageEndpoint) ?
{
url: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${metaElementId}/image`,
previewUrl: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${metaElementId}/image?dimension=small`,
} : {
url: `${absoluteApiPath}/events/files?dataElementUid=${metaElementId}&eventUid=${eventId}`,
previewUrl: `${absoluteApiPath}/events/files?dataElementUid=${metaElementId}&eventUid=${eventId}&dimension=SMALL`,
}
),
[dataElementTypes.ORGANISATION_UNIT]: ({
value,
eventId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@ const subValueGetterByElementType = {
absoluteApiPath: string,
programId: ?string,
}) => {
const buildUrl = () => {
if (featureAvailable(FEATURES.trackerImageEndpoint)) {
if (programId) {
return `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${attributeId}/image?program=${programId}&dimension=small`;
}
return `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${attributeId}/image?dimension=small`;
}
return `${absoluteApiPath}/trackedEntityInstances/${teiId}/${attributeId}/image`;
};
const previewUrl = buildUrl();
const url = featureAvailable(FEATURES.trackerImageEndpoint)
? `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${attributeId}/image?dimension=small`
: `${absoluteApiPath}/trackedEntityInstances/${teiId}/${attributeId}/image?dimension=SMALL`;
const previewUrl = programId ? `${url}&program=${programId}` : url;

return {
previewUrl,
Expand Down
Loading