<commit_before>import React from 'react'
import PropTypes from 'prop-types'
import {Button, ButtonGroup} from 'tocco-ui'
import {FormattedMessage, intlShape} from 'react-intl'

import {defaultModelTransformer} from '../../rest'
import SimpleFormContainer from './../containers/SimpleFormContainer'
import {
  getFormDataDefaults,
  getModel,
  getFormDefinition,
  getGroupedValues,
  reportSettingsDefinitionPropType,
  transformValues,
  submitActions
} from '../utils/report'
import StyledReportSettings from './StyledReportSettings'
import {downloadSupportedByBrowser} from '../../download/download'

class ReportSettings extends React.Component {
  constructor(props) {
    super(props)

    this.customSettingsDefined
      = props.settingsDefinition.customSettings && props.settingsDefinition.customSettings.entity
    this.state = {
      valid: false,
      customSettingsValid: !this.customSettingsDefined
    }
  }

  handleSettingsChange = (values, valid) => {
    this.setState({...this.state, values, valid})
  }

  handleCustomSettingsChange = (customSettings, customSettingsValid) => {
    this.setState({...this.state, customSettings, customSettingsValid})
  }

  handleButtonClick = action => () => {
    const groupedValues = {
      ...getGroupedValues(this.props.settingsDefinition, transformValues(this.state.values)),
      customSettings: this.state.customSettings
    }

    this.props.onSubmit(action, groupedValues)
  }

  render() {
    const {settingsDefinition} = this.props

    return (
      <StyledReportSettings>
        <SimpleFormContainer
          form={getFormDefinition(settingsDefinition, this.context.intl)}
          model={getModel(settingsDefinition)}
          noButtons
          onChange={({values, valid}) => { this.handleSettingsChange(values, valid) }}
          formData={getFormDataDefaults(settingsDefinition)}
        />
        {this.customSettingsDefined
        && <SimpleFormContainer
          form={settingsDefinition.customSettings.form.form}
          model={defaultModelTransformer(settingsDefinition.customSettings.entity)}
          noButtons
          onChange={({values, valid}) => { this.handleCustomSettingsChange(values, valid) }}
        />
        }
        <ButtonGroup>
          {downloadSupportedByBrowser()
          && <Button
            disabled={!this.state.customSettingsValid || !this.state.valid}
            icon="file-download"
            onClick={this.handleButtonClick(submitActions.DOWNLOAD)}
          >
            <FormattedMessage id="client.common.report.download"/>
          </Button><commit_msg>refactor(tocco-util): ink download button<commit_after>import React from 'react'
import PropTypes from 'prop-types'
import {Button, ButtonGroup} from 'tocco-ui'
import {FormattedMessage, intlShape} from 'react-intl'

import {defaultModelTransformer} from '../../rest'
import SimpleFormContainer from './../containers/SimpleFormContainer'
import {
  getFormDataDefaults,
  getModel,
  getFormDefinition,
  getGroupedValues,
  reportSettingsDefinitionPropType,
  transformValues,
  submitActions
} from '../utils/report'
import StyledReportSettings from './StyledReportSettings'
import {downloadSupportedByBrowser} from '../../download/download'

class ReportSettings extends React.Component {
  constructor(props) {
    super(props)

    this.customSettingsDefined
      = props.settingsDefinition.customSettings && props.settingsDefinition.customSettings.entity
    this.state = {
      valid: false,
      customSettingsValid: !this.customSettingsDefined
    }
  }

  handleSettingsChange = (values, valid) => {
    this.setState({...this.state, values, valid})
  }

  handleCustomSettingsChange = (customSettings, customSettingsValid) => {
    this.setState({...this.state, customSettings, customSettingsValid})
  }

  handleButtonClick = action => () => {
    const groupedValues = {
      ...getGroupedValues(this.props.settingsDefinition, transformValues(this.state.values)),
      customSettings: this.state.customSettings
    }

    this.props.onSubmit(action, groupedValues)
  }

  render() {
    const {settingsDefinition} = this.props

    return (
      <StyledReportSettings>
        <SimpleFormContainer
          form={getFormDefinition(settingsDefinition, this.context.intl)}
          model={getModel(settingsDefinition)}
          noButtons
          onChange={({values, valid}) => { this.handleSettingsChange(values, valid) }}
          formData={getFormDataDefaults(settingsDefinition)}
        />
        {this.customSettingsDefined
        && <SimpleFormContainer
          form={settingsDefinition.customSettings.form.form}
          model={defaultModelTransformer(settingsDefinition.customSettings.entity)}
          noButtons
          onChange={({values, valid}) => { this.handleCustomSettingsChange(values, valid) }}
        />
        }
        <ButtonGroup>
          {downloadSupportedByBrowser()
          && <Button
            ink="primary"
            disabled={!this.state.customSettingsValid || !this.state.valid}
            icon="file-download"
            onClick={this.handleButtonClick(submitActions.DOWNLOAD)}
          >
            <FormattedMessage id="client.common.report.download"/>
          </Button>
{'fraction_non_alphanumeric': 0.11231884057971014, 'fraction_numerical': 0.0, 'mean_word_length': 3.341887417218543, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6718638', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1355, 'n_words': 233}