lbry-desktop/ui/component/common/file-exporter.jsx

96 lines
2.1 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
2018-11-26 02:21:25 +01:00
import * as ICONS from 'constants/icons';
2019-03-05 05:46:57 +01:00
2018-02-24 01:24:00 +01:00
import React from 'react';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
2018-11-21 22:20:55 +01:00
import parseData from 'util/parse-data';
2018-03-26 23:32:43 +02:00
import { remote } from 'electron';
2019-03-12 20:53:55 +01:00
import path from 'path';
// @if TARGET='app'
import fs from 'fs';
2019-02-22 06:01:59 +01:00
// @endif
2018-02-24 01:24:00 +01:00
2018-03-26 23:32:43 +02:00
type Props = {
data: Array<any>,
title: string,
label: string,
defaultPath?: string,
filters: Array<string>,
onFileCreated?: string => void,
2019-09-23 04:47:07 +02:00
disabled: boolean,
2018-03-26 23:32:43 +02:00
};
2018-02-24 01:24:00 +01:00
2018-03-26 23:32:43 +02:00
class FileExporter extends React.PureComponent<Props> {
2018-03-22 16:43:35 +01:00
static defaultProps = {
filters: [],
};
2018-03-26 23:32:43 +02:00
constructor() {
super();
2019-03-28 17:53:13 +01:00
(this: any).handleButtonClick = this.handleButtonClick.bind(this);
2018-02-24 01:24:00 +01:00
}
2018-03-26 23:32:43 +02:00
handleFileCreation(filename: string, data: any) {
2018-02-24 01:24:00 +01:00
const { onFileCreated } = this.props;
2019-03-05 05:46:57 +01:00
// @if TARGET='app'
2018-02-24 01:24:00 +01:00
fs.writeFile(filename, data, err => {
if (err) throw err;
// Do something after creation
2018-11-21 22:20:55 +01:00
if (onFileCreated) {
onFileCreated(filename);
}
2018-02-24 01:24:00 +01:00
});
2019-03-05 05:46:57 +01:00
// @endif
2018-02-24 01:24:00 +01:00
}
handleButtonClick() {
2018-03-22 16:43:35 +01:00
const { title, data, defaultPath, filters } = this.props;
2018-02-24 01:24:00 +01:00
const options = {
title,
defaultPath,
2018-03-22 16:43:35 +01:00
filters: [
{
name: 'CSV',
extensions: ['csv'],
},
{
name: 'JSON',
extensions: ['json'],
},
],
2018-02-24 01:24:00 +01:00
};
2018-11-21 22:20:55 +01:00
remote.dialog.showSaveDialog(remote.getCurrentWindow(), options, filename => {
// User hit cancel so do nothing:
if (!filename) return;
// Get extension and remove initial dot
2019-03-05 05:46:57 +01:00
// @if TARGET='app'
2018-11-21 22:20:55 +01:00
const format = path.extname(filename).replace(/\./g, '');
2019-03-05 05:46:57 +01:00
// @endif
2018-11-21 22:20:55 +01:00
// Parse data to string with the chosen format
const parsed = parseData(data, format, filters);
// Write file
if (parsed) {
this.handleFileCreation(filename, parsed);
2018-06-21 22:16:55 +02:00
}
2018-11-21 22:20:55 +01:00
});
2018-02-24 01:24:00 +01:00
}
render() {
2019-09-23 04:47:07 +02:00
const { label, disabled } = this.props;
2018-02-24 01:24:00 +01:00
return (
2019-09-23 04:47:07 +02:00
<Button
button="primary"
disabled={disabled}
icon={ICONS.DOWNLOAD}
label={label || __('Export')}
onClick={this.handleButtonClick}
/>
2018-02-24 01:24:00 +01:00
);
}
}
export default FileExporter;