Skip to content

Commit

Permalink
CDE-38 feat: Add callback example to example application
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsobspinto committed Mar 6, 2024
1 parent 0f15dad commit 7784d67
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion demo/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function mapAndInit(datasetMappingFile, additionalDatasetMappingsFiles, d
collections: getCollections(),
config: {width: '60%', height: '80%'},
name: 'TestLabName',
callback: (cdeFileMapping) => console.log(cdeFileMapping),
callback: (datasetMapping, datasetMappingHeader) => downloadDatasetMappingAsCSV(datasetMapping, datasetMappingHeader),
headerIndexes: {
variableName: 0,
preciseAbbreviation: 1,
Expand Down Expand Up @@ -132,3 +132,32 @@ async function fetchElasticSearchData(queryString) {
const data = await response.json();
return mapElasticSearchHitsToOptions(data.hits.hits || [])
}


function downloadDatasetMappingAsCSV(datasetMapping, datasetMappingHeader) {
// Prepare data in the format that Papa Parse expects
const data = Object.values(datasetMapping).map(row => {
const rowData = {};
datasetMappingHeader.forEach((header, index) => {
rowData[header] = row[index] || '';
});
return rowData;
});

// eslint-disable-next-line no-undef
const csv = Papa.unparse({
fields: datasetMappingHeader,
data: data,
});

// Trigger download
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement("a");
const url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", "datasetMapping.csv");
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

0 comments on commit 7784d67

Please sign in to comment.