<commit_before>import React, { Component, PropTypes } from 'react'
import { Link } from 'react-router'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { AccountActions } from '../../../account/store/account'
import { AvailabilityActions } from '../../store/availability'
import { IdentityActions } from '../../store/identity'
import { RegistrationActions } from '../../store/registration'
import { hasNameBeenPreordered, isSubdomain } from '../../../utils/name-utils'
import { decryptBitcoinPrivateKey, findAddressIndex } from '../../../utils'
import roundTo from 'round-to'
import { QRCode } from 'react-qr-svg'
import Alert from '../../../components/Alert'
import InputGroup from '../../../components/InputGroup'
import log4js from 'log4js'

const logger = log4js.getLogger('profiles/components/registration/RegistrationSelectView.js')
const CHECK_FOR_PAYMENT_INTERVAL = 10000

function mapStateToProps(state) {
  return {
    api: state.settings.api,
    availability: state.profiles.availability,
    addresses: state.account.bitcoinAccount.addresses,
    identityKeypairs: state.account.identityAccount.keypairs,
    identityAddresses: state.account.identityAccount.addresses,
    registration: state.profiles.registration,
    localIdentities: state.profiles.identity.localIdentities,
    balanceUrl: state.settings.api.zeroConfBalanceUrl,
    encryptedBackupPhrase: state.account.encryptedBackupPhrase,
    balances: state.account.bitcoinAccount.balances,
    insightUrl: state.settings.api.insightUrl
  }
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators(Object.assign({},
    IdentityActions, AccountActions, RegistrationActions, AvailabilityActions), dispatch)
}

class AddUsernameSelectPage extends Component {
  static propTypes = {
    routeParams: PropTypes.object.isRequired,
    router: PropTypes.object.isRequired,
    api: PropTypes.object.isRequired,
    availability: PropTypes.object.isRequired,
    registerName: PropTypes.func.isRequired,
    identityKeypairs: PropTypes.array.isRequired,
    identityAddresses: PropTypes.array.isRequired,
    registration: PropTypes.object.isRequired,
    localIdentities: PropTypes.object.isRequired,
    balanceUrl: PropTypes.string.isRequired,
    encryptedBackupPhrase: PropTypes.string.isRequired,
    addresses: PropTypes.array.isRequired,
    balances: PropTypes.object.isRequired,
    insightUrl: PropTypes.string.isRequired,
    refreshBalances: PropTypes.func.isRequired
  }

  constructor(props) {
    super(props)
    const ownerAddress = this.props.routeParams.index
    const name = this.props.routeParams.name
    const availableNames = this.props.availability.names
    const nameAvailabilityObject = availableNames[name]
    if (!nameAvailabilityObject) {
      logger.error(`componentDidMount: not sure if ${name} is available.`)
      props.router.push(`/profiles/i/add-username/${ownerAddress}/search`)
    }

    const nameHasBeenPreordered = hasNameBeenPreordered(name, props.localIdentities)
    if (nameHasBeenPreordered) {
      logger.error(`constructor: Name ${name} has already been preordered.`)
      props.router.push('/profiles')
    }


    const nameIsSubdomain = isSubdomain(name)
    let enoughMoney = false
    let price = 0
    if (nameAvailabilityObject) {
      price = nameAvailabilityObject.price
    }
    price = roundTo.up(price, 3)
    const walletBalance = this.props.balances.total

    if (nameIsSubdomain || (walletBalance > price)) {
      enoughMoney = true
    }

    const that = this
    if (!enoughMoney) {
      this.paymentTimer = setInterval(() => {
        logger.debug('paymentTimer: calling refreshBalances...')
        that.props.refreshBalances(that.props.insightUrl, that.props.addresses,
              that.props.api.coreAPIPassword)
      }, CHECK_FOR_PAYMENT_INTERVAL)
    }
    this.state = {
      ownerAddress,
      name,
      nameIsSubdomain,
      enoughMoney,
      registrationInProgress: false,
      alerts: [],
      password: ''
    }
    this.onValueChange = this.onValueChange.bind(this)
    this.register = this.register.bind(this)
    this.updateAlert = this.updateAlert.bind(this)
  }

  componentDidMount() {
    logger.trace('componentDidMount')
    this.props.refreshBalances(this.props.insightUrl, this.props.addresses,
          this.props.api.coreAPIPassword)
  }

  componentWillReceiveProps(nextProps) {
    const registration = nextProps.registration
    const name = nextProps.routeParams.name
    const address = nextProps.routeParams.index

    if (this.state.registrationInProgress && registration.registrationSubmitted) {
      logger.debug('componentWillReceiveProps: registration submitted! redirecting...')
      this.props.router.push(`/profiles/i/add-username/${address}/submitted/${name}`)
    }

    const availableNames = this.props.availability.names
    const nameAvailabilityObject = availableNames[name]
    const nameIsSubdomain = isSubdomain(name)
    let enoughMoney = false
    let price = 0
    if (nameAvailabilityObject) {
      price = nameAvailabilityObject.price
    }
    price = roundTo.up(price, 3)
    const walletBalance = this.props.balances.total

    if (nameIsSubdomain || (walletBalance > price)) {
      enoughMoney = true
    }<commit_msg>allow registration when user sends exact amount<commit_after>import React, { Component, PropTypes } from 'react'
import { Link } from 'react-router'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { AccountActions } from '../../../account/store/account'
import { AvailabilityActions } from '../../store/availability'
import { IdentityActions } from '../../store/identity'
import { RegistrationActions } from '../../store/registration'
import { hasNameBeenPreordered, isSubdomain } from '../../../utils/name-utils'
import { decryptBitcoinPrivateKey, findAddressIndex } from '../../../utils'
import roundTo from 'round-to'
import { QRCode } from 'react-qr-svg'
import Alert from '../../../components/Alert'
import InputGroup from '../../../components/InputGroup'
import log4js from 'log4js'

const logger = log4js.getLogger('profiles/components/registration/RegistrationSelectView.js')
const CHECK_FOR_PAYMENT_INTERVAL = 10000

function mapStateToProps(state) {
  return {
    api: state.settings.api,
    availability: state.profiles.availability,
    addresses: state.account.bitcoinAccount.addresses,
    identityKeypairs: state.account.identityAccount.keypairs,
    identityAddresses: state.account.identityAccount.addresses,
    registration: state.profiles.registration,
    localIdentities: state.profiles.identity.localIdentities,
    balanceUrl: state.settings.api.zeroConfBalanceUrl,
    encryptedBackupPhrase: state.account.encryptedBackupPhrase,
    balances: state.account.bitcoinAccount.balances,
    insightUrl: state.settings.api.insightUrl
  }
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators(Object.assign({},
    IdentityActions, AccountActions, RegistrationActions, AvailabilityActions), dispatch)
}

class AddUsernameSelectPage extends Component {
  static propTypes = {
    routeParams: PropTypes.object.isRequired,
    router: PropTypes.object.isRequired,
    api: PropTypes.object.isRequired,
    availability: PropTypes.object.isRequired,
    registerName: PropTypes.func.isRequired,
    identityKeypairs: PropTypes.array.isRequired,
    identityAddresses: PropTypes.array.isRequired,
    registration: PropTypes.object.isRequired,
    localIdentities: PropTypes.object.isRequired,
    balanceUrl: PropTypes.string.isRequired,
    encryptedBackupPhrase: PropTypes.string.isRequired,
    addresses: PropTypes.array.isRequired,
    balances: PropTypes.object.isRequired,
    insightUrl: PropTypes.string.isRequired,
    refreshBalances: PropTypes.func.isRequired
  }

  constructor(props) {
    super(props)
    const ownerAddress = this.props.routeParams.index
    const name = this.props.routeParams.name
    const availableNames = this.props.availability.names
    const nameAvailabilityObject = availableNames[name]
    if (!nameAvailabilityObject) {
      logger.error(`componentDidMount: not sure if ${name} is available.`)
      props.router.push(`/profiles/i/add-username/${ownerAddress}/search`)
    }

    const nameHasBeenPreordered = hasNameBeenPreordered(name, props.localIdentities)
    if (nameHasBeenPreordered) {
      logger.error(`constructor: Name ${name} has already been preordered.`)
      props.router.push('/profiles')
    }


    const nameIsSubdomain = isSubdomain(name)
    let enoughMoney = false
    let price = 0
    if (nameAvailabilityObject) {
      price = nameAvailabilityObject.price
    }
    const roundedUpPrice = roundTo.up(price, 3)
    const walletBalance = this.props.balances.total

    if (nameIsSubdomain || (walletBalance >= price)) {
      enoughMoney = true
    }

    const that = this
    if (!enoughMoney) {
      this.paymentTimer = setInterval(() => {
        logger.debug('paymentTimer: calling refreshBalances...')
        that.props.refreshBalances(that.props.insightUrl, that.props.addresses,
              that.props.api.coreAPIPassword)
      }, CHECK_FOR_PAYMENT_INTERVAL)
    }
    this.state = {
      ownerAddress,
      name,
      nameIsSubdomain,
      enoughMoney,
      registrationInProgress: false,
      alerts: [],
      password: ''
    }
    this.onValueChange = this.onValueChange.bind(this)
    this.register = this.register.bind(this)
    this.updateAlert = this.updateAlert.bind(this)
  }

  componentDidMount() {
    logger.trace('componentDidMount')
    this.props.refreshBalances(this.props.insightUrl, this.props.addresses,
          this.props.api.coreAPIPassword)
  }

  componentWillReceiveProps(nextProps) {
    const registration = nextProps.registration
    const name = nextProps.routeParams.name
    const address = nextProps.routeParams.index

    if (this.state.registrationInProgress && registration.registrationSubmitted) {
      logger.debug('componentWillReceiveProps: registration submitted! redirecting...')
      this.props.router.push(`/profiles/i/add-username/${address}/submitted/${name}`)
    }

    const availableNames = this.props.availability.names
    const nameAvailabilityObject = availableNames[name]
    const nameIsSubdomain = isSubdomain(name)
    let enoughMoney = false
    let price = 0
    if (nameAvailabilityObject) {
      price = nameAvailabilityObject.price
    }
    price = roundTo.up(price, 3)
    const walletBalance = this.props.balances.total

    if (nameIsSubdomain || (walletBalance >= price)) {
      enoughMoney = true
    }
{'fraction_non_alphanumeric': 0.1077574047954866, 'fraction_numerical': 0.002256699576868829, 'mean_word_length': 3.8389444949954505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5831399', 'n_tokens_mistral': 3024, 'n_tokens_neox': 2994, 'n_words': 579}