<commit_before>import React, { Component } from 'react'
import { SquealyModal, SquealyDropdown, ErrorMessage } from './SquealyUtilsComponents'
import { PARAM_TYPE_OPTIONS, PARAM_FORMAT_OPTIONS, PARAM_TYPE_MAP, PARAM_FORMAT_MAP } from './../Constant'
import { getEmptyParamDefinition } from './../Utils'
import moment from 'moment'


export default class ParamDefinitionModal extends Component {
  constructor(props) {
    super(props)
    this.state = {
      showParamDefForm: false,
      selectedFormatValue: 'string',
      query: '',
      selectedType: 'query',
      paramDefinition: getEmptyParamDefinition(),
      editMode: false,
      selectedDateFormat: 'DD-MM-YYYY',
      selectedDateTimeFormat: 'DD-MM-YYYY LT',
      errorName: false,
      errorTestValue: false,
      errorDefaultValue: false,
      validateFormat: false
    }
  }
  
  editArrayIndex = -1


  //Function to validate string
  validateString = (checkField,errorField) => {
    let change = this.state.errorName || this.state.errorTestValue || this.state.errorDefaultValue
    if (this.state.paramDefinition[checkField] === '') {
        change = true
        this.setState({ [errorField]: true })
      } else {
        this.setState({ [errorField]: false })
      }
      return change
  }

  //Function to validate Date and DateTime
  validateParamValueFormat = (checkField, errorField) => {
    let typeFormat = 'string'
    if (this.state.selectedFormatValue === 'date') {
      typeFormat = 'selectedDateFormat'
    }
    else if (this.state.selectedFormatValue === 'datetime') {
      typeFormat = 'selectedDateTimeFormat'
    }
    let change = this.state.errorName || this.state.errorTestValue || this.state.errorDefaultValue
    if (this.state.selectedFormatValue !== 'date' && this.state.selectedFormatValue !== 'datetime') {
        change = this.validateString(checkField, errorField) || change
    } else {
      if (moment(this.state.paramDefinition[checkField], this.state[typeFormat], true).isValid() == false) {
        change = true
        this.setState({ [errorField]: true })
      } else {
        this.setState({ [errorField]: false })
      }
    }
    return change
  }
 
  setInputDateFormat = (setDate, stateKey) => {
    let paramDef = JSON.parse(JSON.stringify(this.state.paramDefinition))
    paramDef.kwargs.format = setDate
    this.setState({
      [stateKey]: setDate,
      paramDefinition: paramDef
    })
  }

  //Function to open add param form and initialize form values
  addParamDefHandler = () => {
    this.setState({
      showParamDefForm: true,
      editMode: false,
      paramDefinition: getEmptyParamDefinition(),
      selectedDataType: 'string',
      selectedType: 'query',
      order: this.props.parameters.length+1
    })
  }

  //Function to open close param form
  closeParamForm = () => {
    this.setState({ showParamDefForm: false })
  }


  //Function to select Type for Predefined params
  paramFormatSelectionHandler = (value) => {
    let currentParamDefinition = JSON.parse(JSON.stringify(this.state.paramDefinition))
    currentParamDefinition['data_type'] = value
    this.setState({ selectedFormatValue: value, paramDefinition: currentParamDefinition })
  }

  paramTypeSelectionHandler = (value) => {
    let currentParamDefinition = JSON.parse(JSON.stringify(this.state.paramDefinition))
    currentParamDefinition['type'] = (value === 'query') ? 1 : 2
    this.setState({ selectedType: value, paramDefinition: currentParamDefinition })
  }

  onChangeParamHandler = (key, value) => {
    let currentParamDefinition = JSON.parse(JSON.stringify(this.state.paramDefinition))
    currentParamDefinition[key] = value
    this.setState({ paramDefinition: currentParamDefinition })
  }

  handleEditParam = (e, index) => {
    const {parameters} = this.props
    let dateFormatType = parameters[index].data_type === 'date' || null,
      dateTimeFormatType =  parameters[index].data_type === 'datetime' || null
    
    this.setState({ showParamDefForm: true }, () => {
      let currentParamDefinition = JSON.parse(JSON.stringify(this.props.parameters[index]))
      currentParamDefinition.order = currentParamDefinition.order === null ? '' : currentParamDefinition.order
      this.editArrayIndex = index

      this.setState({
        editMode: true,
        selectedDateFormat: dateFormatType ? currentParamDefinition.kwargs.format : '',
        selectedDateTimeFormat: dateTimeFormatType ? currentParamDefinition.kwargs.format : '',
        selectedFormatValue: currentParamDefinition.data_type,
        paramDefinition: currentParamDefinition,
        selectedType: PARAM_TYPE_MAP[currentParamDefinition.type]
      })
    })
  }

  deleteEntry = (e, index, fieldName) => {
    e.stopPropagation()
    let currentParameters = [...this.props.parameters]
    currentParameters.splice(index, 1)
    this.props.selectedChartChangeHandler('parameters', currentParameters)
    if (index === this.editArrayIndex) {
      this.setState({
        paramDefinition: getEmptyParamDefinition(),
        selectedFormatValue: 'string',
        selectedType: 'query',
        showParamDefForm: (index === this.editArrayIndex) ? false : this.state.showParamDefForm
      })
    }
  }


  updateOrderOfCharts = (obj1, obj2) => {
    let firstObjOrder = obj1.order === '' ? Number.MAX_VALUE : parseInt(obj1.order),
      secondObjOrder = obj2.order === '' ? Number.MAX_VALUE : parseInt(obj2.order)

    if (firstObjOrder < secondObjOrder) {
      return -1
    }
    if (firstObjOrder > secondObjOrder) {
      return 1
    }<commit_msg>Added radix value in parseInt<commit_after>import React, { Component } from 'react'
import { SquealyModal, SquealyDropdown, ErrorMessage } from './SquealyUtilsComponents'
import { PARAM_TYPE_OPTIONS, PARAM_FORMAT_OPTIONS, PARAM_TYPE_MAP, PARAM_FORMAT_MAP } from './../Constant'
import { getEmptyParamDefinition } from './../Utils'
import moment from 'moment'


export default class ParamDefinitionModal extends Component {
  constructor(props) {
    super(props)
    this.state = {
      showParamDefForm: false,
      selectedFormatValue: 'string',
      query: '',
      selectedType: 'query',
      paramDefinition: getEmptyParamDefinition(),
      editMode: false,
      selectedDateFormat: 'DD-MM-YYYY',
      selectedDateTimeFormat: 'DD-MM-YYYY LT',
      errorName: false,
      errorTestValue: false,
      errorDefaultValue: false,
      validateFormat: false
    }
  }
  
  editArrayIndex = -1


  //Function to validate string
  validateString = (checkField,errorField) => {
    let change = this.state.errorName || this.state.errorTestValue || this.state.errorDefaultValue
    if (this.state.paramDefinition[checkField] === '') {
        change = true
        this.setState({ [errorField]: true })
      } else {
        this.setState({ [errorField]: false })
      }
      return change
  }

  //Function to validate Date and DateTime
  validateParamValueFormat = (checkField, errorField) => {
    let typeFormat = 'string'
    if (this.state.selectedFormatValue === 'date') {
      typeFormat = 'selectedDateFormat'
    }
    else if (this.state.selectedFormatValue === 'datetime') {
      typeFormat = 'selectedDateTimeFormat'
    }
    let change = this.state.errorName || this.state.errorTestValue || this.state.errorDefaultValue
    if (this.state.selectedFormatValue !== 'date' && this.state.selectedFormatValue !== 'datetime') {
        change = this.validateString(checkField, errorField) || change
    } else {
      if (moment(this.state.paramDefinition[checkField], this.state[typeFormat], true).isValid() == false) {
        change = true
        this.setState({ [errorField]: true })
      } else {
        this.setState({ [errorField]: false })
      }
    }
    return change
  }
 
  setInputDateFormat = (setDate, stateKey) => {
    let paramDef = JSON.parse(JSON.stringify(this.state.paramDefinition))
    paramDef.kwargs.format = setDate
    this.setState({
      [stateKey]: setDate,
      paramDefinition: paramDef
    })
  }

  //Function to open add param form and initialize form values
  addParamDefHandler = () => {
    this.setState({
      showParamDefForm: true,
      editMode: false,
      paramDefinition: getEmptyParamDefinition(),
      selectedDataType: 'string',
      selectedType: 'query',
      order: this.props.parameters.length+1
    })
  }

  //Function to open close param form
  closeParamForm = () => {
    this.setState({ showParamDefForm: false })
  }


  //Function to select Type for Predefined params
  paramFormatSelectionHandler = (value) => {
    let currentParamDefinition = JSON.parse(JSON.stringify(this.state.paramDefinition))
    currentParamDefinition['data_type'] = value
    this.setState({ selectedFormatValue: value, paramDefinition: currentParamDefinition })
  }

  paramTypeSelectionHandler = (value) => {
    let currentParamDefinition = JSON.parse(JSON.stringify(this.state.paramDefinition))
    currentParamDefinition['type'] = (value === 'query') ? 1 : 2
    this.setState({ selectedType: value, paramDefinition: currentParamDefinition })
  }

  onChangeParamHandler = (key, value) => {
    let currentParamDefinition = JSON.parse(JSON.stringify(this.state.paramDefinition))
    currentParamDefinition[key] = value
    this.setState({ paramDefinition: currentParamDefinition })
  }

  handleEditParam = (e, index) => {
    const {parameters} = this.props
    let dateFormatType = parameters[index].data_type === 'date' || null,
      dateTimeFormatType =  parameters[index].data_type === 'datetime' || null
    
    this.setState({ showParamDefForm: true }, () => {
      let currentParamDefinition = JSON.parse(JSON.stringify(this.props.parameters[index]))
      currentParamDefinition.order = currentParamDefinition.order === null ? '' : currentParamDefinition.order
      this.editArrayIndex = index

      this.setState({
        editMode: true,
        selectedDateFormat: dateFormatType ? currentParamDefinition.kwargs.format : '',
        selectedDateTimeFormat: dateTimeFormatType ? currentParamDefinition.kwargs.format : '',
        selectedFormatValue: currentParamDefinition.data_type,
        paramDefinition: currentParamDefinition,
        selectedType: PARAM_TYPE_MAP[currentParamDefinition.type]
      })
    })
  }

  deleteEntry = (e, index, fieldName) => {
    e.stopPropagation()
    let currentParameters = [...this.props.parameters]
    currentParameters.splice(index, 1)
    this.props.selectedChartChangeHandler('parameters', currentParameters)
    if (index === this.editArrayIndex) {
      this.setState({
        paramDefinition: getEmptyParamDefinition(),
        selectedFormatValue: 'string',
        selectedType: 'query',
        showParamDefForm: (index === this.editArrayIndex) ? false : this.state.showParamDefForm
      })
    }
  }


  updateOrderOfCharts = (obj1, obj2) => {
    let firstObjOrder = obj1.order === '' ? Number.MAX_VALUE : parseInt(obj1.order, 10),
      secondObjOrder = obj2.order === '' ? Number.MAX_VALUE : parseInt(obj2.order, 10)
    if (firstObjOrder < secondObjOrder) {
      return -1
    }
    if (firstObjOrder > secondObjOrder) {
      return 1
    }
{'fraction_non_alphanumeric': 0.1174469608808522, 'fraction_numerical': 0.002685525020141438, 'mean_word_length': 3.436854646544877, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '529192', 'n_tokens_mistral': 3103, 'n_tokens_neox': 3095, 'n_words': 650}