From 60d0aa90a8b2fc3f8f8167fa2cb8413b8e6da5a3 Mon Sep 17 00:00:00 2001 From: Florian Boulnois Date: Mon, 8 Jan 2024 14:54:30 -0500 Subject: [PATCH] feat: extract first non empty property value --- .../DACDatasetTableCellData.js | 6 ++--- src/utils/DatasetUtils.ts | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/utils/DatasetUtils.ts diff --git a/src/components/dac_dataset_table/DACDatasetTableCellData.js b/src/components/dac_dataset_table/DACDatasetTableCellData.js index 53279771be..7468693cae 100644 --- a/src/components/dac_dataset_table/DACDatasetTableCellData.js +++ b/src/components/dac_dataset_table/DACDatasetTableCellData.js @@ -5,6 +5,7 @@ import {DatasetService} from '../../utils/DatasetService'; import DACDatasetApprovalStatus from './DACDatasetApprovalStatus'; import {isEmpty, map} from 'lodash/fp'; import ReactTooltip from 'react-tooltip'; +import { firstNonEmptyPropertyValue } from '../../utils/DatasetUtils'; export const consoleTypes = { CHAIR: 'chair' }; @@ -43,10 +44,7 @@ export function datasetNameCellData({dataset, label = 'datasetNameCellData'}) { export function dataCustodianCellData({dataset, label = 'dataCustodianCellData'}) { // Newer datasets have a list of data custodian emails. // Older datasets may or may not have a data depositor - const datasetCustodians = DatasetService.findDatasetPropertyValueList(dataset, 'Data Custodian Email')?.join(', '); - const dataDepositor = DatasetService.findDatasetPropertyValue(dataset, 'Data Depositor'); - const studyCustodians = DatasetService.findDatasetPropertyValueList(dataset.study, 'dataCustodianEmail')?.join(', '); - const displayValue = isEmpty(datasetCustodians) ? (isEmpty(dataDepositor) ? studyCustodians : dataDepositor) : datasetCustodians; + const displayValue = firstNonEmptyPropertyValue(dataset, [ 'Data Custodian Email', 'Data Depositor', 'dataCustodianEmail' ]); return { data:
{displayValue}
, value: displayValue, diff --git a/src/utils/DatasetUtils.ts b/src/utils/DatasetUtils.ts new file mode 100644 index 0000000000..f0c731d018 --- /dev/null +++ b/src/utils/DatasetUtils.ts @@ -0,0 +1,25 @@ +export const firstNonEmptyPropertyValue = (dataset: any, propertyNames: string[]): string => { + for (const propertyName of propertyNames) { + let propertyValue: string[] = []; + if ('study' in dataset && 'properties' in dataset.study) { + const property = dataset.study.properties.find((property: any) => property.key === propertyName); + const value = property?.value; + if (value !== undefined) { + const valueAsIterable = typeof value === 'string' ? [value] : value; + propertyValue.push(...valueAsIterable); + } + } + if ('properties' in dataset && propertyValue.length === 0) { + const property = dataset.properties.find((property: any) => property.propertyName === propertyName); + const value = property?.propertyValue; + if (property !== undefined) { + const valueAsIterable = typeof value === 'string' ? [value] : value; + propertyValue.push(...valueAsIterable); + } + } + if (propertyValue.length > 0) { + return propertyValue.join(', '); + } + } + return ''; +}