import React from "react";
import { BasicToolbarMenuItem } from "../../application-controls";
import { connect } from "redux-bundler-react";

class AddKmlFileButton extends React.Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
    this.handleFile = this.handleFile.bind(this);
  }
  handleClick(e) {
    this.geojsonInput.click();
  }

  handleFile(e) {
    const { doAddDataFile } = this.props;
    const file = this.geojsonInput.files[0];
    doAddDataFile(file);
  }

  render() {
    return (
      <BasicToolbarMenuItem
        active={false}
        iconClass="ms ms-kml icon-inline"
        title="Add Local KML File"
        text="Add Local KML File"
        onClick={this.handleClick}
        passProps={{}}
        disableSubmenu={true}
      >
        <input
          onChange={this.handleFile}
          accept=".kml"
          className="hidden"
          type="file"
          ref={(el) => {
            this.geojsonInput = el;
          }}
        />
      </BasicToolbarMenuItem>
    );
  }
}

export default connect("doAddDataFile", AddKmlFileButton);
