Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add 'Export Query Result' button which downloads csv file of query result set #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Papa from "papaparse";
import React, { Component } from "react";
import Button from "react-bootstrap/lib/Button";
import Modal from "react-bootstrap/lib/Modal";
Expand Down Expand Up @@ -111,16 +112,33 @@ class App extends Component {
}
};

downloadResultSet = () => {
var outputData = this.state.result.rows;
outputData.unshift(this.state.result.cols);
const csvString = Papa.unparse(outputData);
const blob = new Blob([csvString], { type: "text/csv;charset=utf-8;" });

const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "CSV SQL LIVE output.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};

updateState = state => {
this.setState(state);
};

componentDidMount() {
emitter.addListener("downloadResultSet", this.downloadResultSet);
emitter.addListener("runQuery", this.runQuery);
emitter.addListener("updateState", this.updateState);
}

componentWillUnmount() {
emitter.removeListener("downloadResultSet", this.downloadResultSet);
emitter.removeListener("runQuery", this.runQuery);
emitter.removeListener("updateState", this.updateState);
}
Expand Down
27 changes: 25 additions & 2 deletions src/QueryForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import emitter from "./emitter";
class QueryForm extends Component {
constructor(props) {
super(props);
this.state = { queryText: "" };
this.state = {
queryText: "",
queryRan: false
};
}

handleChange = e => {
Expand All @@ -18,6 +21,14 @@ class QueryForm extends Component {
handleSubmit = e => {
e.preventDefault();
emitter.emit("runQuery", this.state.queryText);
this.setState({
queryRan: true
});
};

handleClick = e => {
e.preventDefault();
emitter.emit("downloadResultSet");
};

newTable = tableName => {
Expand Down Expand Up @@ -54,9 +65,21 @@ class QueryForm extends Component {
}
/>
<Button
bsStyle="primary"
onClick={this.handleClick}
bsStyle="success"
style={{ marginTop: "0.5em" }}
className="pull-right"
disabled={
this.props.status !== "loaded" ||
this.state.queryRan === false
}
>
Export Query Result
</Button>
<Button
bsStyle="primary"
style={{ marginTop: "0.5em", marginRight: "0.5em" }}
className="pull-right"
type="submit"
disabled={
this.props.status !== "loaded" &&
Expand Down