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 committed Mar 30, 2023
1 parent 5d62e5b commit 597aa4e
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

1 comment on commit 597aa4e

@rajeshsub
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Alexis, thank you for this fix. Could you please clarify the following points:

  1. Selecting all records and downloading them as a single CSV does not appear to produce a CSV file.
  2. Selecting the JSON format issues an error asking for the CSV format to be selected instead.

Please sign in to comment.