Patch for the export feature #1163

Merged
btzr-io merged 14 commits from export-data into master 2018-03-22 16:43:35 +01:00
3 changed files with 23 additions and 25 deletions
Showing only changes of commit a0244577d0 - Show all commits
src/renderer
component
util

View file

@ -10,10 +10,8 @@ const { remote } = require('electron');
class FileExporter extends React.PureComponent {
static propTypes = {
data: PropTypes.array,
name: PropTypes.string,
title: PropTypes.string,
label: PropTypes.string,
filters: PropTypes.array,
defaultPath: PropTypes.string,
onFileCreated: PropTypes.func,
};
@ -32,7 +30,7 @@ class FileExporter extends React.PureComponent {
}
handleButtonClick() {
const { title, data, filters, name: defaultPath } = this.props;
const { title, data, defaultPath, filters = [] } = this.props;
const options = {
title,

View file

@ -47,10 +47,10 @@ class TransactionList extends React.PureComponent {
{Boolean(transactionList.length) && (
<FileExporter
data={transactionList}
name={__('lbry-transactions-history')}
label={__('Export')}
title={__('Export Transactions')}
filters={['nout']}
defaultPath={__('lbry-transactions-history')}
/>
)}
{(transactionList.length || this.state.filter) && (

View file

@ -3,38 +3,38 @@ const parseJson = (data, filters = []) => {
const list = data.map(item => {
const temp = {};
// Apply filters
for (const [key, value] of Object.entries(item)) {
Object.entries(item).map(([key, value]) => {
if (!filters.includes(key)) temp[key] = value;
}
});
return temp;
});
// Beautify JSON
return JSON.stringify(list, null, '\t');
}
};
// CSV Parser
// No need for an external module:
// https://gist.github.com/btzr-io/55c3450ea3d709fc57540e762899fb85
const parseCsv = (data, filters=[]) => {
const parseCsv = (data, filters = []) => {
// Get items for header
const getHeaders = temp => {
const headers = [];
// Apply filters
for (const [key] of Object.entries(temp)) {
!filters.includes(key) && headers.push(key);
}
return headers.join(',');
const headers = [];
// Apply filters
Object.entries(temp).map(([key, value]) => {
if (!filters.includes(key)) headers.push(key);
});
return headers.join(',');
};
// Get rows content
const getData = list =>
list.map(item => {
const row = [];
// Apply filters
for (const [key, value] of Object.entries(item)) {
!filters.includes(key) && row.push(value);
}
list
.map(item => {
const row = [];
// Apply filters
Object.entries(item).map(([key, value]) => {
if (!filters.includes(key)) row.push(value);
});
return row.join(',');
})
.join('\n');
@ -42,17 +42,17 @@ const parseCsv = (data, filters=[]) => {
return `${getHeaders(data[0])} \n ${getData(data)}`;
};
const parseData = (data, format, filters=[]) => {
const parseData = (data, format, filters = []) => {
// Check for validation
const valid = data && data[0] && format;
// Pick a format
const formats = {
csv: (list, filters) => parseCsv(list, filters),
json: (list, filters) => parseJson(list, filters),
csv: list => parseCsv(list, filters),
json: list => parseJson(list, filters),
};
// Return parsed data: JSON || CSV
return valid && formats[format] ? formats[format](data, filters) : undefined;
return valid && formats[format] ? formats[format](data) : undefined;
};
export default parseData;