<commit_before>import React from 'react';
import CustomSlider from '../../../../../../../../components/common/CustomSlider/CustomSlider';
import style from './RuleValue.css';
import ComboBox from '../../../../../../../../components/common/ComboBox/ComboBox';
import * as TypesService from '../../../../../../../../services/types-service';
import R from 'ramda';
import { withState, compose, mapProps } from 'recompose';

function replaceNaN(fallbackValue) { return isNaN(this) ? fallbackValue : this; }
const parseNumericInput = (inputValue) => inputValue === '' ? 0 : parseInt(inputValue);

function getTypedValue(value, valueType) {
  try {
    return TypesService.convertValue(value, valueType);
  }
  catch (err) {
    return valueType === TypesService.types.boolean.name ? '' : '' + value
  }
}

function updateMutateTypedValue(mutate, value, valueType) {
  mutate.updateValue(getTypedValue(value, valueType));
}

const booleanSingleVariantSuggestions = [{ label: 'true', value: true }, { label: 'false', value: false }];
const BooleanSingleVariant = ({value, valueType, mutate, identities}) => (
  <div className={style['boolean-single-variant-wrapper']}>
    <ComboBox
      options={booleanSingleVariantSuggestions}
      selected={[R.find(x => x.value === value)(booleanSingleVariantSuggestions)]}
      placeholder="Enter value here"
      showValueInOptions={false}
      onChange={e => updateMutateTypedValue(mutate, e.value, valueType)}
    />
    <button className={style['to-feature-flag-button']}
      onClick={() => mutate.apply(m =>
        m.delete()
          .in('Type').updateValue('MultiVariant').up()
          .insert('OwnerType', identities[0])
          .insert('ValueDistribution', {
            type: 'bernoulliTrial',
            args: 0.1,
          })
      )}>Gradual release</button>
  </div>
);

const SingleVariantValue = ({value, mutate, identities, autofocus, valueType}) => (
  <div className={style['rule-value-container']}>
    {valueType === TypesService.types.boolean ?
      <BooleanSingleVariant {...{ value, valueType, mutate, identities }} />
      :
      <div>
        <input
          onChange={e => updateMutateTypedValue(mutate, e.target.value, valueType)}
          value={value}
          placeholder="Enter value here"
          className={style['values-input']}
          ref={(e) => e && autofocus && e.focus()}
        />
        <button className={style['add-variant-button']}
          onClick={() => mutate.apply(m =>
            m.delete()
              .in('Type').updateValue('MultiVariant').up()
              .insert('OwnerType', identities[0])
              .insert('ValueDistribution', {
                type: 'weighted',
                args: {
                  [value]: 50,
                  'New Varaint': 50,
                },
              })
          )}>Add Variant</button><commit_msg>Another fix for boolean value type issue<commit_after>import React from 'react';
import CustomSlider from '../../../../../../../../components/common/CustomSlider/CustomSlider';
import style from './RuleValue.css';
import ComboBox from '../../../../../../../../components/common/ComboBox/ComboBox';
import * as TypesService from '../../../../../../../../services/types-service';
import R from 'ramda';
import { withState, compose, mapProps } from 'recompose';

function replaceNaN(fallbackValue) { return isNaN(this) ? fallbackValue : this; }
const parseNumericInput = (inputValue) => inputValue === '' ? 0 : parseInt(inputValue);

function getTypedValue(value, valueType) {
  try {
    return TypesService.convertValue(value, valueType);
  }
  catch (err) {
    return valueType === TypesService.types.boolean.name ? '' : '' + value
  }
}

function updateMutateTypedValue(mutate, value, valueType) {
  mutate.updateValue(getTypedValue(value, valueType));
}

const booleanSingleVariantSuggestions = [{ label: 'true', value: true }, { label: 'false', value: false }];
const BooleanSingleVariant = ({value, valueType, mutate, identities}) => (
  <div className={style['boolean-single-variant-wrapper']}>
    <ComboBox
      options={booleanSingleVariantSuggestions}
      selected={[R.find(x => x.value === value)(booleanSingleVariantSuggestions)]}
      placeholder="Enter value here"
      showValueInOptions={false}
      onChange={e => updateMutateTypedValue(mutate, e.value, valueType)}
    />
    <button className={style['to-feature-flag-button']}
      onClick={() => mutate.apply(m =>
        m.delete()
          .in('Type').updateValue('MultiVariant').up()
          .insert('OwnerType', identities[0])
          .insert('ValueDistribution', {
            type: 'bernoulliTrial',
            args: 0.1,
          })
      )}>Gradual release</button>
  </div>
);

const SingleVariantValue = ({value, mutate, identities, autofocus, valueType}) => (
  <div className={style['rule-value-container']}>
    {valueType === TypesService.types.boolean.name ?
      <BooleanSingleVariant {...{ value, valueType, mutate, identities }} />
      :
      <div>
        <input
          onChange={e => updateMutateTypedValue(mutate, e.target.value, valueType)}
          value={value}
          placeholder="Enter value here"
          className={style['values-input']}
          ref={(e) => e && autofocus && e.focus()}
        />
        <button className={style['add-variant-button']}
          onClick={() => mutate.apply(m =>
            m.delete()
              .in('Type').updateValue('MultiVariant').up()
              .insert('OwnerType', identities[0])
              .insert('ValueDistribution', {
                type: 'weighted',
                args: {
                  [value]: 50,
                  'New Varaint': 50,
                },
              })
          )}>Add Variant</button>
{'fraction_non_alphanumeric': 0.17065972222222223, 'fraction_numerical': 0.003125, 'mean_word_length': 2.692948717948718, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 47, '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': '3757025', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1605, 'n_words': 323}