<commit_before>import React, {Component} from 'react'
import classNames from 'classnames'
import {FormattedMessage, intlShape} from 'react-intl'
import {Button, LoadMask} from 'tocco-ui'
import PasswordInput from './PasswordInput'
import PasswordMatchDisplay from './PasswordMatchDisplay'
import ValidationRules from '../ValidationRules'
import FailureMessage from '../FailureMessage'

class PasswordUpdateDialog extends Component {
  componentWillMount() {
    this.props.fetchValidationRules()
  }

  handleSubmit(e) {
    e.preventDefault()

    if (this.isSubmittable() === true) {
      this.props.savePassword()
    }
  }

  componentDidUpdate() {
    if (this.props.validationRules) {
      window.requestAnimationFrame(() => {
        this.props.initialized()
      })
    }
  }

  render() {
    const {password, validationRules, updateOldPassword, updateNewPassword, updateNewPasswordRepeat}
      = this.props

    if (!validationRules) {
      return <LoadMask className="password-update-dialog"/>
    }

    const oldPasswordReadOnly = password.passwordUpdatePending
    const newPasswordReadOnly = (!password.oldPassword && this.props.showOldPasswordField)
      || password.passwordUpdatePending
    const newPasswordRepeatReadOnly = !password.newPassword
      || password.passwordUpdatePending
      || (password.newPasswordValidationErrors && Object.keys(password.newPasswordValidationErrors).length > 0)

    return (
      <div>
        <div>
          {this.props.showTitle
          && <h1><FormattedMessage id="client.login.passwordUpdate.title"/></h1>
          }
          {
            this.props.forcedUpdate
            && <p><FormattedMessage id="client.login.passwordUpdate.introduction"/></p>
          }
        </div>

        <form className="password-update-dialog" onSubmit={this.handleSubmit.bind(this)}>
          {this.props.showOldPasswordField === true
          && <PasswordInput
            label={this.msg('client.login.passwordUpdate.oldPassword')}
            name="oldPassword"
            value={password.oldPassword}
            onChange={updateOldPassword}
            readOnly={oldPasswordReadOnly}
            autoFocus
          />}
          <PasswordInput
            label={this.msg('client.login.passwordUpdate.newPassword')}
            name="newPassword"
            value={password.newPassword}
            onChange={updateNewPassword}
            readOnly={newPasswordReadOnly}
            autoFocus={this.props.showOldPasswordField !== true}
            valid={!newPasswordRepeatReadOnly}
          />
          <ValidationRules
            rules={validationRules}
            errors={password.newPasswordValidationErrors}
            rulesNeutral={!password.newPassword}
          />
          <PasswordInput
            label={this.msg('client.login.passwordUpdate.newPasswordRepeat')}
            name="newPasswordRepeat"
            value={password.newPasswordRepeat}
            onChange={updateNewPasswordRepeat}
            readOnly={newPasswordRepeatReadOnly}
            valid={this.isSubmittable()}
          />
          <PasswordMatchDisplay password={password.newPassword} passwordRepeat={password.newPasswordRepeat}/>
          <Button
            label={this.msg('client.login.passwordUpdate.saveButton')}
            disabled={this.isSubmittable() === false}
            className={classNames('last-element-in-block', {'update-pending': password.passwordUpdatePending})}
            type="submit"
            primary
          />
          {password.passwordUpdateFailed === true && <FailureMessage errorCode={password.passwordUpdateErrorCode}/>}
        </form>
      </div>
    )
  }

  isSubmittable() {
    if (!this.props.password.newPasswordRepeat) {
      return false
    }
    if (this.props.password.newPassword !== this.props.password.newPasswordRepeat) {
      return false
    }
    if (this.props.password.passwordUpdatePending) {
      return false
    }<commit_msg>refactor(login): set validation on oldPassword field<commit_after>import React, {Component} from 'react'
import classNames from 'classnames'
import {FormattedMessage, intlShape} from 'react-intl'
import {Button, LoadMask} from 'tocco-ui'
import PasswordInput from './PasswordInput'
import PasswordMatchDisplay from './PasswordMatchDisplay'
import ValidationRules from '../ValidationRules'
import FailureMessage from '../FailureMessage'

class PasswordUpdateDialog extends Component {
  componentWillMount() {
    this.props.fetchValidationRules()
  }

  handleSubmit(e) {
    e.preventDefault()

    if (this.isSubmittable() === true) {
      this.props.savePassword()
    }
  }

  componentDidUpdate() {
    if (this.props.validationRules) {
      window.requestAnimationFrame(() => {
        this.props.initialized()
      })
    }
  }

  render() {
    const {password, validationRules, updateOldPassword, updateNewPassword, updateNewPasswordRepeat}
      = this.props

    if (!validationRules) {
      return <LoadMask className="password-update-dialog"/>
    }

    const oldPasswordReadOnly = password.passwordUpdatePending
    const newPasswordReadOnly = (!password.oldPassword && this.props.showOldPasswordField)
      || password.passwordUpdatePending
    const newPasswordRepeatReadOnly = !password.newPassword
      || password.passwordUpdatePending
      || (password.newPasswordValidationErrors && Object.keys(password.newPasswordValidationErrors).length > 0)

    return (
      <div>
        <div>
          {this.props.showTitle
          && <h1><FormattedMessage id="client.login.passwordUpdate.title"/></h1>
          }
          {
            this.props.forcedUpdate
            && <p><FormattedMessage id="client.login.passwordUpdate.introduction"/></p>
          }
        </div>

        <form className="password-update-dialog" onSubmit={this.handleSubmit.bind(this)}>
          {this.props.showOldPasswordField === true
          && <PasswordInput
            label={this.msg('client.login.passwordUpdate.oldPassword')}
            name="oldPassword"
            value={password.oldPassword}
            onChange={updateOldPassword}
            readOnly={oldPasswordReadOnly}
            autoFocus
            valid={
              password.passwordUpdateFailed && password.passwordUpdateErrorCode === 'INVALID_CREDENTIALS' ? false : null
            }
          />}
          <PasswordInput
            label={this.msg('client.login.passwordUpdate.newPassword')}
            name="newPassword"
            value={password.newPassword}
            onChange={updateNewPassword}
            readOnly={newPasswordReadOnly}
            autoFocus={this.props.showOldPasswordField !== true}
            valid={!newPasswordRepeatReadOnly}
          />
          <ValidationRules
            rules={validationRules}
            errors={password.newPasswordValidationErrors}
            rulesNeutral={!password.newPassword}
          />
          <PasswordInput
            label={this.msg('client.login.passwordUpdate.newPasswordRepeat')}
            name="newPasswordRepeat"
            value={password.newPasswordRepeat}
            onChange={updateNewPasswordRepeat}
            readOnly={newPasswordRepeatReadOnly}
            valid={this.isSubmittable()}
          />
          <PasswordMatchDisplay password={<PASSWORD>} passwordRepeat={<PASSWORD>}/>
          {password.passwordUpdateFailed === true && <FailureMessage errorCode={password.passwordUpdateErrorCode}/>}
          <Button
            label={this.msg('client.login.passwordUpdate.saveButton')}
            disabled={this.isSubmittable() === false}
            className={classNames('last-element-in-block', {'update-pending': password.passwordUpdatePending})}
            type="submit"
            primary
          />
        </form>
      </div>
    )
  }

  isSubmittable() {
    if (!this.props.password.newPasswordRepeat) {
      return false
    }
    if (this.props.password.newPassword !== this.props.password.<PASSWORD>) {
      return false
    }
    if (this.props.password.passwordUpdatePending) {
      return false
    }
{'fraction_non_alphanumeric': 0.11251392499071668, 'fraction_numerical': 0.0007426661715558856, 'mean_word_length': 2.661078386950612, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 50, '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': '2897608', 'n_tokens_mistral': 2066, 'n_tokens_neox': 2067, 'n_words': 319}