Skip to content

Commit

Permalink
feat: compare with event data to find latest value
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikmv committed Nov 5, 2024
1 parent 8217a68 commit 292ff79
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow
import { useMemo } from 'react';
import moment from 'moment';
import { v4 as uuid } from 'uuid';
import log from 'loglevel';
import { useTimeZoneConversion, useConfig, useDataEngine } from '@dhis2/app-runtime';
Expand Down Expand Up @@ -42,16 +41,6 @@ const fetchFormattedValues = async ({
}) => {
if (!rawRecords) return [];

const mostRecentCreatedAtByFieldId = rawRecords.changeLogs.reduce((acc, record) => {
const elementKey = Object.keys(record.change)[0];
const fieldId = record.change[elementKey]?.dataElement ?? record.change[elementKey]?.attribute;

if (!acc[fieldId] || moment(record.createdAt).isAfter(acc[fieldId])) {
acc[fieldId] = record.createdAt;
}
return acc;
}, {});

const getMetadataItemDefinition = (
elementKey: string,
change: Change,
Expand Down Expand Up @@ -82,14 +71,9 @@ const fetchFormattedValues = async ({
return null;
}

console.log('change', change);
console.log('entityData', entityData);

const getSubValue =
subValueGetterByElementType[RECORD_TYPE[entityType]]?.[metadataElement.type];
const getSubValue = subValueGetterByElementType[RECORD_TYPE[entityType]]?.[metadataElement.type];

const isLatestValue =
moment(createdAt).isSameOrAfter(mostRecentCreatedAtByFieldId[fieldId]);
const isLatestValue = entityData?.[change.dataElement]?.value === change.currentValue;

const getValue = async (value, latestValue) => {
if (!getSubValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const buildTEAUrlByElementType: {|
const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` });

if (!latestValue) {
return name;
return 'name';
}

return {
Expand Down Expand Up @@ -67,7 +67,7 @@ const buildTEAUrlByElementType: {|
const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` });

if (!latestValue) {
return name;
return 'Image';
}

return {
Expand Down Expand Up @@ -96,7 +96,7 @@ const buildDataElementUrlByElementType: {|
const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` });

if (!latestValue) {
return name;
return 'File';
}

return {
Expand All @@ -119,7 +119,7 @@ const buildDataElementUrlByElementType: {|
const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` });

if (!latestValue) {
return name;
return 'Image';
}

return {
Expand Down

0 comments on commit 292ff79

Please sign in to comment.