Skip to content

Commit

Permalink
fix(ui): fixing data download inconsistencies on JSON and CSV format (#…
Browse files Browse the repository at this point in the history
…1442)

relate to #1384
  • Loading branch information
AlexisSouquiere authored and tchiotludo committed Apr 4, 2023
1 parent 12fc9ef commit 5c85db9
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions client/src/containers/Topic/Topic/Topic.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Topic extends Root {
configs: [],
isAllTopicDataSelected: false,
downloadFormat: 'Select',
downloadOptions: ['Select', 'csv', 'json'],
downloadOptions: ['csv', 'json'],
messages: []
};

Expand Down Expand Up @@ -108,15 +108,30 @@ class Topic extends Root {

_handleDownloadAll(option) {
let messages = this.state.messages;
if (this.state.isAllTopicDataSelected && option !== 'Select') {
if (this.state.isAllTopicDataSelected) {
let allData = [];
messages.map(tableData => {
allData.push(tableData.value);
allData.push('\n');
});
switch (option) {
case 'json':
try {
allData = [
JSON.stringify(
messages.map(m => JSON.parse(m.value)),
null,
2
)
];
} catch (e) {
toast.warn('Unable to export data in JSON. Please use CSV instead');
return;
}
break;
case 'csv':
allData = [messages.map(m => m.value).join('\n')];
break;
}
const a = document.createElement('a');
const type = 'text/' + option;
a.href = URL.createObjectURL(new Blob([allData], { type: type }));
a.href = URL.createObjectURL(new Blob(allData, { type: type, endings: 'native' }));
a.download = `file.${option}`;

a.click();
Expand Down

0 comments on commit 5c85db9

Please sign in to comment.