Skip to content

Commit

Permalink
Add TDR specific rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
nmalfroy authored and fboulnois committed Jan 29, 2024
1 parent 843658f commit dd24448
Showing 1 changed file with 66 additions and 6 deletions.
72 changes: 66 additions & 6 deletions src/components/data_search/DatasetSearchTable.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as React from 'react';
import { Button, Link } from '@mui/material';
import _ from 'lodash';
import { Box, Button, Link } from '@mui/material';
import { useEffect, useState } from 'react';
import { groupBy, isEmpty } from 'lodash';
import CollapsibleTable from '../CollapsibleTable';
import TableHeaderSection from '../TableHeaderSection';
import { DAR } from '../../libs/ajax';
import DatasetExportButton from './DatasetExportButton';
import { DAR, TerraDataRepo } from '../../libs/ajax';
import { Config } from '../../libs/config';
import DatasetFilterList from './DatasetFilterList';
import { Box } from '@mui/material';

const studyTableHeader = [
'Study Name',
Expand All @@ -26,6 +28,7 @@ const datasetTableHeader = [
'Participants',
'Data Location',
'DAC',
'Use in Terra',
];

export const DatasetSearchTable = (props) => {
Expand All @@ -34,6 +37,8 @@ export const DatasetSearchTable = (props) => {
const [filtered, setFiltered] = useState([]);
const [tableData, setTableData] = useState({});
const [selected, setSelected] = useState([]);
const [exportableDatasets, setExportableDatasets] = useState({}); // datasetId -> snapshot
const [tdrApiUrl, setTdrApiUrl] = useState('');

const isFiltered = (filter) => filters.indexOf(filter) > -1;

Expand Down Expand Up @@ -96,6 +101,30 @@ export const DatasetSearchTable = (props) => {
setSelected(newSelected);
};

const expandHandler = async (event, data) => {
// This method adds the export to Terra button to the subtable rows that have an associated TDR snapshot that the user
// has access to.

setTdrApiUrl(await Config.getTdrApiUrl());

// Note the dataset id is the first column in subrows.
// If columns are ever reordereable, this will need to be updated to be not hardcoded to look for the first column.
const datasetIds = data.subtable.rows.map((row) => row.data[0].value);
const snapshots = await TerraDataRepo.listSnapshotsByDatasetIds(datasetIds);
if (snapshots.filteredTotal > 0) {
const datasetIdToSnapshot = _.chain(snapshots.items)
// Ignore any snapshots that a user does not have export (steward or reader) to
.filter((snapshot) => _.intersection(snapshots.roleMap[snapshot.id], ['steward', 'reader']).length > 0)
.groupBy('duosId')
.value();
setExportableDatasets(datasetIdToSnapshot);
}
};

const collapseHandler = () => {
setExportableDatasets({});
};

const applyForAccess = async () => {
const draftDatasets = selected.map((id) => parseInt(id.replace('dataset-', '')));
const darDraft = await DAR.postDarDraft({ datasetId: draftDatasets });
Expand Down Expand Up @@ -172,11 +201,35 @@ export const DatasetSearchTable = (props) => {
value: dataset.participantCount,
},
{
value: dataset.url ? <Link href={dataset.url}>{dataset.dataLocation}</Link> : dataset.dataLocation,
value: () => {
const exportableSnapshots = exportableDatasets[dataset.datasetIdentifier] || [];
if (exportableSnapshots.length === 0) {
return dataset.dataLocation;
}
return exportableSnapshots.map((snapshot, i) =>
<Link
key={`${i}`}
href={`${tdrApiUrl}/snapshots/${snapshot.id}`}
target="_blank"
>
{snapshot.name}
</Link>);
}
},
{
value: dataset.dac?.dacName,
},
{
value: () => {
const exportableSnapshots = exportableDatasets[dataset.datasetIdentifier] || [];
return exportableSnapshots
.map((snapshot, i) =>
<DatasetExportButton
key={`${i}`}
snapshot={snapshot}
title={`Export snapshot ${snapshot.name}`} />);
}
},
],
};
}),
Expand All @@ -186,7 +239,7 @@ export const DatasetSearchTable = (props) => {
};

setTableData(table);
}, [filtered]);
}, [filtered, exportableDatasets, tdrApiUrl]);

useEffect(() => {
setFiltered(datasets);
Expand All @@ -206,7 +259,14 @@ export const DatasetSearchTable = (props) => {
<h1>No datasets registered for this library.</h1>
</Box>
:
<CollapsibleTable data={tableData} selected={selected} selectHandler={selectHandler} summary='faceted study search table' />
<CollapsibleTable
data={tableData}
selected={selected}
selectHandler={selectHandler}
expandHandler={expandHandler}
collapseHandler={collapseHandler}
summary='faceted study search table'
/>
}
</Box>
</Box>
Expand Down

0 comments on commit dd24448

Please sign in to comment.