<commit_before>'use strict'
import React from 'react'
import InputField from '../Mixin/Form/InputField.jsx'
import CheckValues from '../Mixin/Helper/CheckValues'

/* global $ */

class ManagerForm extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      manager: {},
      errors: {
        password: null,
        first_name: null,
        last_name: null,
        phone: null,
        email: null,
        company_name: null
      }
    }

    let bindable = [
      'checkCompanyName',
      'checkEmailAddress',
      'checkEmailDuplicate',
      'checkPassword',
      'checkPhone',
      'save',
      'setValue',
      'setError'
    ]

    bindable.map(function (v) {
      this[v] = this[v].bind(this)
    }.bind(this))
  }

  componentDidMount() {
    this.load()
  }

  load() {
    $.getJSON('./properties/Manager/view').done(function (data) {
      this.setState({manager: data})
    }.bind(this))
  }

  setValue(varname, value) {
    if (typeof value === 'object' && value.target !== undefined) {
      value = value.target.value
    }
    this.setError(varname, null)
    let manager = this.state.manager
    manager[varname] = value
    this.setState({manager})
  }

  setError(varname, value) {
    let errors = this.state.errors
    errors[varname] = value
    this.setState({errors})
  }

  postErrors(errors) {
    if (errors.companyEmpty) {
      this.setError('company_name', 'Please enter a company name')
    } else if (errors.companyDuplicate) {
      this.setError('company_name', 'Company name already in use')
    } else {
      this.setError('company_name', null)
    }

    if (errors.emailEmpty) {
      this.setError('email', 'Email may not be empty')
    } else {
      this.setError('email', null)
    }

    if (errors.firstNameEmpty) {
      this.setError('first_name', 'First name may not be empty')
    } else {
      this.setError('first_name', null)
    }

    if (errors.lastNameEmpty) {
      this.setError('last_name', 'Last name may not be empty')
    } else {
      this.setError('last_name', null)
    }

    if (errors.passwordEmpty) {
      this.setError('password', 'Password may not be empty')
    } else if (errors.passwordShort) {
      this.setError('password', 'Password must be a least 8 characters')
    } else {
      this.setError('password', null)
    }

    if (errors.phoneEmpty) {
      this.setError('phone', 'Phone number may not be empty')
    } else if (errors.phoneBadFormat) {
      this.setError('phone', 'Phone number is improperly formatted')
    } else {
      this.setError('phone', null)
    }

    if (errors.usernameEmpty) {
      this.setError('username', 'Username may not be empty')
    } else if (errors.usernameDuplicate) {
      this.setError('username', 'Username already in use')
    } else {
      this.setError('username', null)
    }
  }

  save() {
    $.post('properties/Manager/', this.state.manager, null, 'json').done(function (data) {
      if (data.status === 'error') {
        this.postErrors(data)
      } else {
        window.location.href = './properties/Manager/desktop'
      }
    }.bind(this)).fail(function () {})
  }

  checkPhone() {
    if (!CheckValues.isPhone(this.state.manager.phone)) {
      this.setError('phone', 'Phone number not formatted properly')
    } else {
      this.setError('phone', null)
    }
  }<commit_msg>Manager edit post changed to put<commit_after>'use strict'
import React from 'react'
import InputField from '../Mixin/Form/InputField.jsx'
import CheckValues from '../Mixin/Helper/CheckValues'

/* global $ */

class ManagerForm extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      manager: {},
      errors: {
        password: null,
        first_name: null,
        last_name: null,
        phone: null,
        email: null,
        company_name: null
      }
    }

    let bindable = [
      'checkCompanyName',
      'checkEmailAddress',
      'checkEmailDuplicate',
      'checkPassword',
      'checkPhone',
      'save',
      'setValue',
      'setError'
    ]

    bindable.map(function (v) {
      this[v] = this[v].bind(this)
    }.bind(this))
  }

  componentDidMount() {
    this.load()
  }

  load() {
    $.getJSON('./properties/Manager/view').done(function (data) {
      this.setState({manager: data})
    }.bind(this))
  }

  setValue(varname, value) {
    if (typeof value === 'object' && value.target !== undefined) {
      value = value.target.value
    }
    this.setError(varname, null)
    let manager = this.state.manager
    manager[varname] = value
    this.setState({manager})
  }

  setError(varname, value) {
    let errors = this.state.errors
    errors[varname] = value
    this.setState({errors})
  }

  postErrors(errors) {
    if (errors.companyEmpty) {
      this.setError('company_name', 'Please enter a company name')
    } else if (errors.companyDuplicate) {
      this.setError('company_name', 'Company name already in use')
    } else {
      this.setError('company_name', null)
    }

    if (errors.emailEmpty) {
      this.setError('email', 'Email may not be empty')
    } else {
      this.setError('email', null)
    }

    if (errors.firstNameEmpty) {
      this.setError('first_name', 'First name may not be empty')
    } else {
      this.setError('first_name', null)
    }

    if (errors.lastNameEmpty) {
      this.setError('last_name', 'Last name may not be empty')
    } else {
      this.setError('last_name', null)
    }

    if (errors.passwordEmpty) {
      this.setError('password', 'Password may not be empty')
    } else if (errors.passwordShort) {
      this.setError('password', 'Password must be a least 8 characters')
    } else {
      this.setError('password', null)
    }

    if (errors.phoneEmpty) {
      this.setError('phone', 'Phone number may not be empty')
    } else if (errors.phoneBadFormat) {
      this.setError('phone', 'Phone number is improperly formatted')
    } else {
      this.setError('phone', null)
    }

    if (errors.usernameEmpty) {
      this.setError('username', 'Username may not be empty')
    } else if (errors.usernameDuplicate) {
      this.setError('username', 'Username already in use')
    } else {
      this.setError('username', null)
    }
  }

  save() {
    $.ajax({
      url: 'properties/Manager/',
      data: this.state.manager,
      dataType: 'json',
      type: 'put'
    }).done(function (data) {
      if (data.status === 'error') {
        this.postErrors(data)
      } else {
        window.location.href = './properties/Manager/desktop'
      }
    }.bind(this))
  }

  checkPhone() {
    if (!CheckValues.isPhone(this.state.manager.phone)) {
      this.setError('phone', 'Phone number not formatted properly')
    } else {
      this.setError('phone', null)
    }
  }
{'fraction_non_alphanumeric': 0.14308823529411765, 'fraction_numerical': 0.0002941176470588235, 'mean_word_length': 2.555148980658651, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '395973', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2108, 'n_words': 469}