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

import simpleFormConnector from '../containers/simpleFormConnector'
import {
  getFormDefinition,
  getGroupedValues,
  reportSettingsDefinitionPropType,
  transformValues
} from '../utils/report'
import {StyledStickyButtons, StyledReportSettings} from './StyledReportSettings'

export const ReportSettings = ({settingsDefinition, formApp, onSubmit, listApp, intl}) => {
  const customSettingsDefined = settingsDefinition.customSettings && settingsDefinition.customSettings.entity
  const [settings, setSettings] = useState({
    valid: false,
    customSettingsValid: !customSettingsDefined
  })

  const SimpleFormContainer = useMemo(() => simpleFormConnector(formApp), [])

  const handleSettingsChange = (values, valid) => {
    setSettings({
      ...settings,
      values,
      valid
    })
  }

  const handleCustomSettingsChange = (customSettings, customSettingsValid) => {
    setSettings({
      ...settings,
      customSettings,
      customSettingsValid
    })
  }

  const handleButtonClick = () => {
    const groupedValues = {
      ...getGroupedValues(settingsDefinition, transformValues(settings.values)),
      customSettings: settings.customSettings
    }

    onSubmit(groupedValues)
  }

  return (
    <StyledReportSettings>
      <SimpleFormContainer
        listApp={listApp}
        form={getFormDefinition(settingsDefinition, intl)}
        noButtons
        onChange={({values, valid}) => {
          handleSettingsChange(values, valid)
        }}
        mode="create"
      />
      {customSettingsDefined
        && <SimpleFormContainer
          listApp={listApp}
          form={settingsDefinition.customSettings.form.form}
          noButtons
          onChange={({values, valid}) => {
            handleCustomSettingsChange(values, valid)
          }}
          mode="create"
        />
      }
      <StyledStickyButtons>
        <Button
          ink="primary"
          disabled={!settings.customSettingsValid || !settings.valid}
          onClick={handleButtonClick}
          look="raised"
        >
          <FormattedMessage id="client.common.report.generate"/>
        </Button>
      </StyledStickyButtons>
    </StyledReportSettings>
  )<commit_msg>fix(app-extensions): repair report settings<commit_after>import React, {useState, useMemo, useRef} from 'react'
import PropTypes from 'prop-types'
import {Button} from 'tocco-ui'
import {FormattedMessage, injectIntl} from 'react-intl'

import simpleFormConnector from '../containers/simpleFormConnector'
import {
  getFormDefinition,
  getGroupedValues,
  reportSettingsDefinitionPropType,
  transformValues
} from '../utils/report'
import {StyledStickyButtons, StyledReportSettings} from './StyledReportSettings'

export const ReportSettings = ({settingsDefinition, formApp, onSubmit, listApp, intl}) => {
  const customSettingsDefined = settingsDefinition.customSettings && settingsDefinition.customSettings.entity
  const [settings, setSettings] = useState({
    valid: false,
    customSettingsValid: !customSettingsDefined
  })
  const settingsRef = useRef()
  settingsRef.current = settings // workaround to read current state in static callbacks (because of memo hook)

  const SimpleFormContainer = useMemo(() => simpleFormConnector(formApp), [formApp])

  const handleSettingsChange = ({values, valid}) => {
    setSettings({
      ...settingsRef.current,
      values,
      valid
    })
  }

  const handleCustomSettingsChange = ({values, valid}) => {
    setSettings({
      ...settingsRef.current,
      customSettings: values,
      customSettingsValid: valid
    })
  }

  const handleButtonClick = () => {
    const groupedValues = {
      ...getGroupedValues(settingsDefinition, transformValues(settings.values)),
      customSettings: settings.customSettings
    }

    onSubmit(groupedValues)
  }

  return (
    <StyledReportSettings>
      <SimpleFormContainer
        listApp={listApp}
        form={getFormDefinition(settingsDefinition, intl)}
        noButtons
        onChange={handleSettingsChange}
        mode="create"
      />
      {customSettingsDefined
        && <SimpleFormContainer
          listApp={listApp}
          form={settingsDefinition.customSettings.form.form}
          noButtons
          onChange={handleCustomSettingsChange}
          mode="create"
        />
      }
      <StyledStickyButtons>
        <Button
          ink="primary"
          disabled={!settings.customSettingsValid || !settings.valid}
          onClick={handleButtonClick}
          look="raised"
        >
          <FormattedMessage id="client.common.report.generate"/>
        </Button>
      </StyledStickyButtons>
    </StyledReportSettings>
  )
{'fraction_non_alphanumeric': 0.10467751906037502, 'fraction_numerical': 0.0, 'mean_word_length': 3.1665236051502146, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5216857', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1286, 'n_words': 254}