Skip to content

Commit

Permalink
fix: convert changelog data in servertoclient
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikmv committed Nov 28, 2024
1 parent cfb870d commit 732f04c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { IconArrowRight16, IconChevronUp16, IconChevronDown16 } from '@dhis2/ui'
import { CHANGE_TYPES } from '../../../../Changelog/Changelog.constants';
import type { ChangelogValueCellProps } from './ChangelogValueCellComponents.types';

const ValueDisplay = ({ value, showMore }) => (
<span>{showMore ? value : value?.[0]}</span>
const ValueDisplay = ({ value, showMore, className }) => (
<span className={className}>{showMore ? value : value?.[0]}</span>
);

const ViewMoreButton = ({ showMore, onClick, classes }) => (
Expand Down Expand Up @@ -57,6 +57,7 @@ const Created = ({ currentValue, classes }: ChangelogValueCellProps) => {
<ValueDisplay
value={currentValue}
showMore={showMore}
className={classes.currentValue}
/>
</div>
<div className={classes.buttonContainer}>
Expand Down
26 changes: 21 additions & 5 deletions src/core_modules/capture-core/converters/serverToClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ export function convertOptionSetValue(value: any, type: $Keys<typeof dataElement
return optionSetConvertersForType[type] ? optionSetConvertersForType[type](value) : value;
}

function convertCoordinateToClient(value: any) {
if (typeof value === 'string') {
const coordinates = value.replace(/[()]/g, '').split(',').map(Number);
return { latitude: coordinates[1], longitude: coordinates[0] };
}
return { latitude: value[1], longitude: value[0] };
}

function convertPolygonToClient(value: any) {
if (typeof value === 'string') {
const coordinates = value.replace(/[()]/g, '').split(',').map(Number);
const coordinatesArray = [];
for (let i = 0; i < coordinates.length; i += 2) {
coordinatesArray.push([coordinates[i], coordinates[i + 1]]);
}
return coordinatesArray;
}
return value;
}

const valueConvertersForType = {
[dataElementTypes.NUMBER]: parseNumber,
Expand All @@ -55,11 +74,8 @@ const valueConvertersForType = {
[dataElementTypes.DATETIME]: (d2Value: string) => moment(d2Value).toISOString(),
[dataElementTypes.TRUE_ONLY]: (d2Value: string) => ((d2Value === 'true') || null),
[dataElementTypes.BOOLEAN]: (d2Value: string) => (d2Value === 'true'),
[dataElementTypes.COORDINATE]: (d2Value: string | Array<string>) => {
const arr = typeof d2Value === 'string' ? JSON.parse(d2Value) : d2Value;
return { latitude: arr[1], longitude: arr[0] };
},
[dataElementTypes.POLYGON]: (d2Value: Array<number>) => d2Value,
[dataElementTypes.COORDINATE]: (d2Value: any) => convertCoordinateToClient(d2Value),
[dataElementTypes.POLYGON]: (d2Value: any) => convertPolygonToClient(d2Value),
[dataElementTypes.ASSIGNEE]: convertAssignedUserToClient,
};

Expand Down

0 comments on commit 732f04c

Please sign in to comment.