<commit_before>import React from 'react'
import modal, { Modal } from '../../components/form/modal'
import Joi from 'joi'
import { toastr } from 'react-redux-toastr'
import Dropzone from 'react-dropzone'
import { Field, change } from 'redux-form'
import { Button, Input } from 'reactstrap'
import { actions as async } from '../../reduxers/async'
import { actions as common } from '../../reduxers/common'
import { FormattedMessage } from 'react-intl'
import { actions as oauth2 } from 'react-redux-oauth2'
import _ from 'lodash'
const FormData = window.FormData

const schema = Joi.object().keys({
  avatar: Joi.string().allow(''),
  old_password: Joi.string().empty(''),
  password: Joi.string().min(6).empty(''),
  password_confirm: Joi.string().valid(Joi.ref('password')).options({
    language: {
      any: {
        allowOnly: 'Please check your password'
      }
    }
  })
}).unknown().and('old_password', 'password')

export class ProfileForm extends Modal {
  handleUpload (files) {
    const { dispatch } = this.props
    const data = new FormData()
    const file = files[0]
    data.append('file', file, file.name)
    return dispatch(async.post('avatar')('/files', data, {
      headers: { 'content-type': 'multipart/form-data' }
    })).then(v => {
      dispatch(change('profile', 'avatar', v.value.data.file_path))
    })
  }
  handleClose () {
    const { dispatch } = this.props
    return dispatch(common.toggleProfileModal(false))
  }
  handleSubmit (values) {
    const { intl, dispatch } = this.props
    const { avatar, password, old_password: oldPassword } = values
    let data = { avatar }
    if (password && oldPassword) {
      data = {...data, password, old_password: oldPassword}
    }
    return new Promise((resolve, reject) => {
      dispatch(async.patch('profile')(`/auth/user/profile`, data)).then(v => {
        this.close()
        dispatch(oauth2.loadUser(v.value.data))
        toastr.success(intl.formatMessage({id: 'success_title'}), intl.formatMessage({id: 'success_message'}))
        return v
      }).then(resolve).catch(e => {
        console.log(e)
        reject(e)
      })
    })
  }
  renderBody () {
    const { async, initialValues } = this.props
    return (
      <div className='row'>
        <div className='col-sm-3'>
          <Dropzone ref={node => { this.dropzone = node }} onDrop={this.handleUpload.bind(this)} multiple={false} style={{}}>
            {({acceptedFiles}) => {
              if (_.get(async, 'avatar.status') === 'fulfilled') {
                return <div className='image rounded' style={{backgroundImage: `url(${async.avatar.response.file_path})`}} />
              } else {
                return <div className='image rounded' style={{backgroundImage: `url(${initialValues.avatar})`}} />
              }<commit_msg>improve<commit_after>import React from 'react'
import modal, { Modal } from '../../components/form/modal'
import Joi from 'joi'
import { toastr } from 'react-redux-toastr'
import Dropzone from 'react-dropzone'
import { Field, change } from 'redux-form'
import { Button, Input } from 'reactstrap'
import { actions as async } from '../../reduxers/async'
import { actions as common } from '../../reduxers/common'
import { FormattedMessage } from 'react-intl'
import { actions as oauth2 } from 'react-redux-oauth2'
import _ from 'lodash'
const FormData = window.FormData

const schema = Joi.object().keys({
  avatar: Joi.string().allow(''),
  old_password: Joi.string().empty(''),
  password: Joi.string().min(6).empty(''),
  password_confirm: Joi.string().valid(Joi.ref('password')).options({
    language: {
      any: {
        allowOnly: 'Please check your password'
      }
    }
  })
}).unknown().and('old_password', 'password')

export class ProfileForm extends Modal {
  handleUpload (files) {
    const { dispatch } = this.props
    const data = new FormData()
    const file = files[0]
    data.append('file', file, file.name)
    return dispatch(async.post('avatar')('/files', data, {
      headers: { 'content-type': 'multipart/form-data' }
    })).then(v => {
      dispatch(change('profile', 'avatar', v.value.data.file_path))
    })
  }
  handleClose () {
    const { dispatch } = this.props
    return dispatch(common.toggleProfileModal(false))
  }
  handleSubmit (values) {
    const { intl, dispatch } = this.props
    const { avatar, password, old_password: oldPassword } = values
    let data = { avatar }
    if (password && oldPassword) {
      data = {...data, password, old_password: oldPassword}
    }
    return new Promise((resolve, reject) => {
      dispatch(async.patch('profile')(`/auth/user/profile`, data)).then(v => {
        this.handleClose()
        dispatch(oauth2.loadUser(v.value.data))
        toastr.success(intl.formatMessage({id: 'success_title'}), intl.formatMessage({id: 'success_message'}))
        return v
      }).then(resolve).catch(e => {
        console.log(e)
        reject(e)
      })
    })
  }
  renderBody () {
    const { async, initialValues } = this.props
    return (
      <div className='row'>
        <div className='col-sm-3'>
          <Dropzone ref={node => { this.dropzone = node }} onDrop={this.handleUpload.bind(this)} multiple={false} style={{}}>
            {({acceptedFiles}) => {
              if (_.get(async, 'avatar.status') === 'fulfilled') {
                return <div className='image rounded' style={{backgroundImage: `url(${async.avatar.response.file_path})`}} />
              } else {
                return <div className='image rounded' style={{backgroundImage: `url(${initialValues.avatar})`}} />
              }
{'fraction_non_alphanumeric': 0.16547533952823446, 'fraction_numerical': 0.002144388849177984, 'mean_word_length': 3.0705454545454547, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, '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': '2391627', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1718, 'n_words': 380}