<commit_before>import React, {Component} from 'react';
import {graphql} from 'react-apollo';
import gql from 'graphql-tag';
import DocumentTitle from 'react-document-title';
import {Container, Loader, Grid, Header, Dropdown} from 'semantic-ui-react';

import ApiClient from '../../services/ApiClient';

import QueryResult from '../../components/QueryResult';
import QueryVariables from '../../components/QueryVariables';
import Menu from 'semantic-ui-react/dist/commonjs/collections/Menu/Menu';

class QuestionContainer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      variables: [],
    };
  }

  componentWillReceiveProps(nextProps) {
    const {data: {loading, error, question}} = nextProps;

    if (loading || error) {
      return;
    }

    const {variables} = question;

    this.setState({variables: variables.map(v => ({name: v.name, value: v.value, isControllable: v.isControllable}))});
  }

  handleVariableChange = (key, value) => {
    const {variables} = this.state;
    const index = variables.findIndex(e => e['name'] == key);
    this.setState({variables: variables.map((item, i) => (index !== i ? item : {...item, value: value}))});
  };

  exportCSV = e => {
    e.preventDefault();
    const {data: {question: {dataSource, query}}} = this.props;

    ApiClient.post('export/csv', {Source: dataSource, Query: query})
      .then(response => response.blob())
      .then(blob => {
        const url = window.URL.createObjectURL(new Blob([blob], {type: {type: 'text/csv;charset=utf-8;'}}));
        let a = document.createElement('a');
        a.href = url;
        a.download = 'chart.csv';
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
      });
  };

  onNewView = view => {
    this.view = view;
  };

  exportPNG = e => {
    e.preventDefault();
    this.view.toImageURL('png').then(url => {
      let link = document.createElement('a');
      link.setAttribute('href', url);
      link.setAttribute('target', '_blank');
      link.setAttribute('download', 'chart.png');
      link.dispatchEvent(new MouseEvent('click'));
    });
  };

  render() {
    const {data: {loading, error, question}} = this.props;
<commit_msg>Better names for export files<commit_after>import React, {Component} from 'react';
import {graphql} from 'react-apollo';
import gql from 'graphql-tag';
import DocumentTitle from 'react-document-title';
import {Container, Loader, Grid, Header, Dropdown, Menu} from 'semantic-ui-react';

import ApiClient from '../../services/ApiClient';

import QueryResult from '../../components/QueryResult';
import QueryVariables from '../../components/QueryVariables';

import routing from '../../helpers/routing';

class QuestionContainer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      variables: [],
    };
  }

  componentWillReceiveProps(nextProps) {
    const {data: {loading, error, question}} = nextProps;

    if (loading || error) {
      return;
    }

    const {variables} = question;

    this.setState({variables: variables.map(v => ({name: v.name, value: v.value, isControllable: v.isControllable}))});
  }

  handleVariableChange = (key, value) => {
    const {variables} = this.state;
    const index = variables.findIndex(e => e['name'] == key);
    this.setState({variables: variables.map((item, i) => (index !== i ? item : {...item, value: value}))});
  };

  exportCSV = e => {
    e.preventDefault();
    const {data: {question}} = this.props;

    ApiClient.post('export/csv', {Source: question.dataSource, Query: question.query})
      .then(response => response.blob())
      .then(blob => {
        const url = window.URL.createObjectURL(new Blob([blob], {type: {type: 'text/csv;charset=utf-8;'}}));
        let a = document.createElement('a');
        a.href = url;
        a.download = `${routing.urlFor(question, ['title'])}.csv`;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
      });
  };

  onNewView = view => {
    this.view = view;
  };

  exportPNG = e => {
    const {data: {question}} = this.props;
    e.preventDefault();
    this.view.toImageURL('png').then(url => {
      let link = document.createElement('a');
      link.setAttribute('href', url);
      link.setAttribute('target', '_blank');
      link.setAttribute('download', `${routing.urlFor(question, ['title'])}.png`);
      link.dispatchEvent(new MouseEvent('click'));
    });
  };

  render() {
    const {data: {loading, error, question}} = this.props;

{'fraction_non_alphanumeric': 0.17601928978518194, 'fraction_numerical': 0.00043840420868040335, 'mean_word_length': 3.3333333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '956423', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1391, 'n_words': 311}