Skip to content

Commit

Permalink
feat: extract first non empty property value
Browse files Browse the repository at this point in the history
  • Loading branch information
fboulnois committed Jan 8, 2024
1 parent 99fa68a commit 60d0aa9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/components/dac_dataset_table/DACDatasetTableCellData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' };

Expand Down Expand Up @@ -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: <div className={style['cell-data']}>{displayValue}</div>,
value: displayValue,
Expand Down
25 changes: 25 additions & 0 deletions src/utils/DatasetUtils.ts
Original file line number Diff line number Diff line change
@@ -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 '';
}

0 comments on commit 60d0aa9

Please sign in to comment.