insomnia/packages/insomnia-app/app/ui/components/viewers/response-csv-viewer.js

66 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-12-28 15:34:42 +00:00
// @flow
import * as React from 'react';
import Papa from 'papaparse';
import autobind from 'autobind-decorator';
type Props = {
body: Buffer,
2017-12-28 15:34:42 +00:00
};
type State = {
result: null | { data: Array<Array<string>> },
2017-12-28 15:34:42 +00:00
};
@autobind
class ResponseCSVViewer extends React.PureComponent<Props, State> {
currentHash: string;
2018-06-25 17:42:50 +00:00
constructor(props: Props) {
2017-12-28 15:34:42 +00:00
super(props);
this.state = {
result: null,
2017-12-28 15:34:42 +00:00
};
this.currentHash = '';
}
2018-06-25 17:42:50 +00:00
update(body: Buffer) {
2017-12-28 15:34:42 +00:00
const csv = body.toString('utf8');
Papa.parse(csv, {
skipEmptyLines: true,
complete: result => {
2018-06-25 17:42:50 +00:00
this.setState({ result });
},
2017-12-28 15:34:42 +00:00
});
}
2018-06-25 17:42:50 +00:00
componentDidMount() {
2017-12-28 15:34:42 +00:00
this.update(this.props.body);
}
2018-06-25 17:42:50 +00:00
componentWillUpdate(nextProps: Props, nextState: State) {
2017-12-28 15:34:42 +00:00
if (this.props.body === nextProps.body) {
return;
}
this.update(nextProps.body);
}
2018-06-25 17:42:50 +00:00
render() {
const { result } = this.state;
2017-12-28 15:34:42 +00:00
if (!result) {
return 'Parsing CSV...';
}
return (
<div className="pad-sm">
<table className="table--fancy table--striped table--compact selectable">
2018-10-17 16:42:33 +00:00
<tbody>{result.data.map(row => <tr>{row.map(c => <td>{c}</td>)}</tr>)}</tbody>
2017-12-28 15:34:42 +00:00
</table>
</div>
);
}
}
export default ResponseCSVViewer;