<commit_before>import React from 'react';
import PropTypes from 'prop-types';
import { observer } from 'mobx-react';

import FormSelect from './FormSelect';

@observer
class CascadingSelect extends React.Component {
  static propTypes = {
    namePrefix: PropTypes.string,
    optionNodeId: PropTypes.string,
    optionSetId: PropTypes.string.isRequired,
    levels: PropTypes.arrayOf(PropTypes.object),
    updateLevels: PropTypes.func.isRequired,
    onChange: PropTypes.func,
  };

  async componentDidMount() {
    const { updateLevels } = this.props;
    await updateLevels();
  }

  nodeChanged = (isLastLevel) => (newNodeId) => {
    const { onChange, updateLevels } = this.props;

    if (onChange) {
      onChange(newNodeId);
    }

    if (!isLastLevel) {
      updateLevels();
    }
  }

  buildLevelProps = (level, isLastLevel) => {
    const { namePrefix } = this.props;
    return {
      type: 'select',
      name: `${namePrefix}[option_node_ids][]`,
      key: 'display_conditions_attributes_option_node_ids_',
      value: level.selected,
      options: level.options,
      prompt: this.optionPrompt(level),
      onChange: this.nodeChanged(isLastLevel),
    };
  }

  buildLevels = () => {
    const { levels } = this.props;
    const self = this;
    let result = [];
    if (levels) {
      result = levels.map((level, i) => {
        return (
          <div
            className="level"
            key={level.name}
          >
            <FormSelect {...self.buildLevelProps(level, self.isLastLevel(i))} />
          </div>
        );
      });
    }
    return result;
  }

  isLastLevel = (i) => {
    const { levels } = this.props;
    return levels && levels.length === (i + 1);
  }

  optionPrompt = (level) => {<commit_msg>9397: Pass levelIndex instead of isLastLevel<commit_after>import React from 'react';
import PropTypes from 'prop-types';
import { observer } from 'mobx-react';

import FormSelect from './FormSelect';

@observer
class CascadingSelect extends React.Component {
  static propTypes = {
    namePrefix: PropTypes.string,
    optionNodeId: PropTypes.string,
    optionSetId: PropTypes.string.isRequired,
    levels: PropTypes.arrayOf(PropTypes.object),
    updateLevels: PropTypes.func.isRequired,
    onChange: PropTypes.func,
  };

  async componentDidMount() {
    const { updateLevels } = this.props;
    await updateLevels();
  }

  nodeChanged = (levelIndex) => (newNodeId) => {
    const { onChange, updateLevels } = this.props;
    const isLastLevel = this.isLastLevel(levelIndex);

    if (onChange) {
      onChange(newNodeId);
    }

    if (!isLastLevel) {
      updateLevels();
    }
  }

  buildLevelProps = (level, levelIndex) => {
    const { namePrefix } = this.props;
    return {
      type: 'select',
      name: `${namePrefix}[option_node_ids][]`,
      key: 'display_conditions_attributes_option_node_ids_',
      value: level.selected,
      options: level.options,
      prompt: this.optionPrompt(level),
      onChange: this.nodeChanged(levelIndex),
    };
  }

  buildLevels = () => {
    const { levels } = this.props;
    let result = [];
    if (levels) {
      result = levels.map((level, index) => {
        return (
          <div
            className="level"
            key={level.name}
          >
            <FormSelect {...this.buildLevelProps(level, index)} />
          </div>
        );
      });
    }
    return result;
  }

  isLastLevel = (i) => {
    const { levels } = this.props;
    return levels && levels.length === (i + 1);
  }

  optionPrompt = (level) => {
{'fraction_non_alphanumeric': 0.13451277730974445, 'fraction_numerical': 0.0016849199663016006, 'mean_word_length': 2.612576064908722, 'pattern_counts': {'":': 0, '<': 9, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6878340', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1107, 'n_words': 229}