Skip to content

Commit

Permalink
- ported csv generation functionality from backend to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
boriskovar-m2ms committed Jul 23, 2024
1 parent f40bcb1 commit 1ec9788
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 26 deletions.
26 changes: 14 additions & 12 deletions js/components/datasets/compoundSetList.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import SearchField from '../common/Components/SearchField';
import { base_url } from '../routes/constants';
import { METHOD, api } from '../../utils/api';
import { compoundsColors } from '../preview/compounds/redux/constants';
import { downloadRHSCSVExport } from '../../utils/csv';

const useStyles = makeStyles(theme => ({
table: {
Expand Down Expand Up @@ -197,19 +198,20 @@ export const CompoundSetList = () => {

const fileName = `${datasetID}.csv`;
const reqObj = { title: datasetID, filename: fileName, dict: listOfMols };
const jsonString = JSON.stringify(reqObj);
downloadRHSCSVExport(reqObj);
// const jsonString = JSON.stringify(reqObj);

api({
url: `${base_url}/api/dicttocsv/`,
method: METHOD.POST,
data: jsonString
}).then(resp => {
var anchor = document.createElement('a');
anchor.href = `${base_url}/api/dicttocsv/?file_url=${resp.data['file_url']}`;
anchor.target = '_blank';
anchor.download = `${fileName}`; //'download';
anchor.click();
});
// api({
// url: `${base_url}/api/dicttocsv/`,
// method: METHOD.POST,
// data: jsonString
// }).then(resp => {
// var anchor = document.createElement('a');
// anchor.href = `${base_url}/api/dicttocsv/?file_url=${resp.data['file_url']}`;
// anchor.target = '_blank';
// anchor.download = `${fileName}`; //'download';
// anchor.click();
// });
};

useEffect(() => {
Expand Down
29 changes: 15 additions & 14 deletions js/components/datasets/selectedCompoundsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { ARROW_TYPE, VIEWS } from '../../constants/constants';
import { useScrollToCompound } from './useScrollToCompound';
import useDisableNglControlButtons from '../preview/molecule/useDisableNglControlButtons';
import useDisableDatasetNglControlButtons from './useDisableDatasetNglControlButtons';
import { downloadRHSCSVExport } from '../../utils/csv';

const useStyles = makeStyles(theme => ({
container: {
Expand Down Expand Up @@ -572,20 +573,20 @@ export const SelectedCompoundList = memo(() => {
const fileName = `${targetName}-RHS-selection.csv`;
// console.log(`href - ${fileName}`);
const reqObj = { title: sharedSnapshot.url, filename: fileName, dict: listOfMols };
const jsonString = JSON.stringify(reqObj);

api({
url: `${base_url}/api/dicttocsv/`,
method: METHOD.POST,
data: jsonString
}).then(resp => {
var anchor = document.createElement('a');
anchor.href = `${base_url}/api/dicttocsv/?file_url=${resp.data['file_url']}`;
// console.log(`href - ${base_url}/api/dicttocsv/?file_url=${resp.data['file_url']}`);
anchor.target = '_blank';
anchor.download = `${fileName}`; //'download';
anchor.click();
});
downloadRHSCSVExport(reqObj);
// const jsonString = JSON.stringify(reqObj);

// api({
// url: `${base_url}/api/dicttocsv/`,
// method: METHOD.POST,
// data: jsonString
// }).then(resp => {
// var anchor = document.createElement('a');
// anchor.href = `${base_url}/api/dicttocsv/?file_url=${resp.data['file_url']}`;
// anchor.target = '_blank';
// anchor.download = `${fileName}`; //'download';
// anchor.click();
// });
});
};

Expand Down
64 changes: 64 additions & 0 deletions js/utils/csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export const downloadRHSCSVExport = data => {
const csvContent = generateCSVExportFromObject(data);
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('download', data.filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};

export const generateCSVExportFromObject = data => {
let csvContent = '';
const filename = data.filename;

let newLine = '\r\n';

csvContent += data.title + newLine;

if (data?.dict?.length < 1) {
return '';
}

const headerKeys = getReferenceDataForHeader(data.dict[0]);
csvContent += generateCSVHeaderFromKeys(headerKeys) + newLine;

for (let i = 0; i < data.dict.length; i++) {
const row = data.dict[i];
csvContent += generateCSVRowFromObject(row, headerKeys) + newLine;
}

return csvContent;
};

const generateCSVRowFromObject = (row, keys) => {
let csvRow = '';

for (let i = 0; i < keys.length; i++) {
csvRow += row[keys[i]] ? row[keys[i]] : '';
if (i < keys.length - 1) {
csvRow += ',';
}
}

return csvRow;
};

const generateCSVHeaderFromKeys = keys => {
let csvHeader = '';

for (let i = 0; i < keys.length; i++) {
csvHeader += keys[i];
if (i < keys.length - 1) {
csvHeader += ',';
}
}

return csvHeader;
};

const getReferenceDataForHeader = data => {
return Object.keys(data);
};

0 comments on commit 1ec9788

Please sign in to comment.