{"size": 2151, "ext": "js", "max_stars_repo_path": "react/src/components/DownloadButton.js", "max_stars_repo_name": "kohnalex/hof-university-weather", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "react/src/components/DownloadButton.js", "max_issues_repo_name": "kohnalex/hof-university-weather", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "react/src/components/DownloadButton.js", "max_forks_repo_name": "kohnalex/hof-university-weather", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.5769230769, "max_line_length": 113, "alphanum_fraction": 0.6234309623}
import React, { Component } from 'react';

import { Button } from '@material-ui/core';
import { connect } from 'react-redux';
import { getJSONForDownload } from '../actions/actions';


class DownloadButton extends Component {


    render() {
        return (
            <Button variant="contained" color="primary" onClick={() => { this.download(this) }}>Download</Button>
        )
    }

    download() {
        startDownload(this.props)
        let id = setInterval(() => { this.checkForData(id) }, 1500);
    }

    checkForData(id) {
        if (!this.props.pending) {
            exportToJson(this.props.data)
            clearInterval(id)
        }

    }

}



function exportToJson(objectData) {
    let filename = "Wetterdaten.json";
    let contentType = "application/json;charset=utf-8;";
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        var blob = new Blob([decodeURIComponent(encodeURI(JSON.stringify(objectData)))], { type: contentType });
        navigator.msSaveOrOpenBlob(blob, filename);
    } else {
        var a = document.createElement('a');
        a.download = filename;
        a.href = 'data:' + contentType + ',' + encodeURIComponent(JSON.stringify(objectData));
        a.target = '_blank';
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }
}


function startDownload(props) {
    props.getJSON(buildURL(props.firstDate, props.secondDate));
}

const buildURL = (firstDate, secondDate) => {
    console.log("http://127.0.0.1:8000/v1/sensors/all/?date=" + firstDate + "&date2=" + secondDate);
    return ("http://127.0.0.1:8000/v1/sensors/all/?date=" + firstDate + "&date2=" + secondDate);
}

const mapStateToProps = (state) => {
    return {
        data: state.download.data,
        firstDate: state.download.firstDate,
        secondDate: state.download.secondDate,
        pending: state.weatherData.pending
    }
}

const mapDispatchToProps = (dispatch) => {
    return {
        getJSON: (url) => {
            dispatch(getJSONForDownload(url))
        }
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(DownloadButton);