<commit_before>// @flow
import React, { Component } from 'react';
import { Button, Divider, Form, Grid, Header, Input, Message, Modal, Segment, Table } from 'semantic-ui-react';
import InputRange from 'react-input-range';

import NumericLabel from '../../utils/NumericLabel';
import AccountName from '../global/AccountName';

export default class AccountsProxy extends Component {
  constructor(props) {
    super(props);
    this.state = {
      editDelegationFor: false,
      sp: 0,
      undelegateError: false,
      vests: 0,
    };
    this.props.actions.resetState = this.resetState.bind(this);
  }
  state = {
    editDelegationFor: false,
    sp: 0,
    vests: 0,
  }
  componentWillReceiveProps = (nextProps) => {
    if (nextProps.processing.account_delegate_vesting_shares_error) {
      this.setState({
        undelegateError: nextProps.processing.account_delegate_vesting_shares_error
      })
      nextProps.actions.setDelegateVestingSharesCompleted();
    }
    if (nextProps.processing.account_delegate_vesting_shares_resolved) {
      nextProps.actions.setDelegateVestingSharesCompleted();
      this.resetState();
    }
  }
  resetState = () => {
    this.setState({
      editDelegationFor: false,
      sp: 0,
      undelegateError: false,
      vests: 0,
      [this.state.editDelegationFor]: '',
    });
  }
  handleCancel = () => {
    this.resetState();
  }
  handleOnChange = (value) => {
    const vests = parseFloat(value).toFixed(6);
    const props = this.props.steem.props;
    const totalVestsSteem = parseFloat(props.total_vesting_fund_steem.split(" ")[0])
    const totalVests = parseFloat(props.total_vesting_shares.split(" ")[0])
    const sp = totalVestsSteem * vests / totalVests;
    this.setState({ vests, sp });
  }

  handleOnChangeComplete = (value) => {
    const vests = parseFloat(value).toFixed(6);
    const props = this.props.steem.props;
    const totalVestsSteem = parseFloat(props.total_vesting_fund_steem.split(" ")[0])
    const totalVests = parseFloat(props.total_vesting_shares.split(" ")[0])
    const sp = totalVestsSteem * vests / totalVests;
    this.setState({ vests, sp });
  }
  handleVestingSharesRemove = (e, props) => {
    let { delegator, delegatee, id } = props.value.delegatee;
    delegatee = delegatee.toLowerCase().replace('@', '');
    const permissions = this.props.keys.permissions;
    this.setState({undelegateError: false})
    this.props.actions.useKey('setDelegateVestingShares', { delegator, delegatee, vestingShares: 0.000000 }, permissions[delegator])
  }
  handleVestingSharesEdit = (e, props) => {
    const t = this
    let { delegator, delegatee, id, vesting_shares } = props.value.delegatee;
    this.setState({
      editDelegationFor: delegator,
      [delegator]: delegatee,
    }, () => {
      this.handleOnChange(parseFloat(vesting_shares.split(" ")[0]))
    })
  }
  handleSetDelegateVesting = (e, props) => {
    this.setState({
      editDelegationFor: props.value
    })
  }
  handleSetDelegateVestingConfirm = (e, props) => {
    const { vests } = this.state;
    const { permissions } = this.props.keys;
    const delegator = this.state.editDelegationFor;
    const delegatee = this.state[delegator];
    this.props.actions.useKey('setDelegateVestingShares', { delegator, delegatee, vestingShares: vests }, permissions[delegator])
    e.preventDefault();
  }
  handleChangeVestingShares = (e, props) => {
    const editing = this.state.editDelegationFor;
    const newState = {};
    newState[editing] = props.value.trim();
    this.setState(newState);
  }
  render() {
    const t = this;
    let addVesting = false;
    let undelegateError = false;
    if (this.state.undelegateError) {
      undelegateError = (
        <Message
          error
          header='Operation Error'
          content={this.state.undelegateError}
        />
      )
    }
    const numberFormat = {
      shortFormat: true,
      shortFormatMinValue: 1000
    };
    const {
      account_delegate_vesting_shares_error,
      account_delegate_vesting_shares_pending,
      account_delegate_vesting_shares_resolved
    } = this.props.processing;
    if (this.state.editDelegationFor) {
      const name = this.state.editDelegationFor;
      const account = this.props.account.accounts[name];
      const delegated = parseFloat(account.delegated_vesting_shares.split(" ")[0]);
      const vests = parseFloat(account.vesting_shares.split(" ")[0]);
      let existingDelegation = 0
      if (this.props.account.vestingDelegations && this.props.account.vestingDelegations[name]) {
        const existingDelegations = this.props.account.vestingDelegations[name]
        existingDelegation = existingDelegations.reduce((a, b) => (b.delegatee === this.state[name]) ? a + parseFloat(b.vesting_shares.split(" ")[0]) : 0, 0)
      }
      const available = vests - delegated + existingDelegation;
      const target = parseFloat(this.state.vests)
      const delegateWarning = (target > available - 100)
      addVesting = (
        <Modal
          size="small"
          open
          header="Delegate Vests to another Account"
          onClose={this.resetState}
          content={
            <Form
              loading={account_delegate_vesting_shares_pending}
            >
              <Segment
                padded
                basic
              >
                <p>
                  Please enter the name of the target account that you wish to delegate
                  a portion of the {name} account's vested weight to.
                </p>
                <Input
                  fluid
                  name="delegatee"
                  placeholder="Delegatee Account Name"
                  defaultValue={this.state[name]}
                  autoFocus
                  onChange={this.handleChangeVestingShares}
                />
                <Divider />
                <p>
                  Use the slider to determine how much of your VESTS to delegate. If you are editing an existing delegation, set the value to the new total you wish to delegate.
                </p>
                <Grid>
                  <Grid.Row>
                    <Grid.Column width={12}>
                      <Segment padded="very" basic>
                        <InputRange
                          maxValue={available}
                          minValue={0}
                          value={this.state.vests}
                          onChange={this.handleOnChange}
                          onChangeComplete={this.handleOnChangeComplete}<commit_msg>Better delegation controls<commit_after>// @flow
import React, { Component } from 'react';
import { Button, Divider, Form, Grid, Header, Input, Message, Modal, Segment, Table } from 'semantic-ui-react';
import InputRange from 'react-input-range';

import NumericLabel from '../../utils/NumericLabel';
import AccountName from '../global/AccountName';

export default class AccountsProxy extends Component {
  constructor(props) {
    super(props);
    this.state = {
      editDelegationFor: false,
      sp: 0,
      undelegateError: false,
      vests: 0,
    };
    this.props.actions.resetState = this.resetState.bind(this);
  }
  state = {
    editDelegationFor: false,
    sp: 0,
    vests: 0,
  }
  componentWillReceiveProps = (nextProps) => {
    if (nextProps.processing.account_delegate_vesting_shares_error) {
      this.setState({
        undelegateError: nextProps.processing.account_delegate_vesting_shares_error
      })
      nextProps.actions.setDelegateVestingSharesCompleted();
    }
    if (nextProps.processing.account_delegate_vesting_shares_resolved) {
      nextProps.actions.setDelegateVestingSharesCompleted();
      this.resetState();
    }
  }
  resetState = () => {
    this.setState({
      editDelegationFor: false,
      sp: 0,
      undelegateError: false,
      vests: 0,
      [this.state.editDelegationFor]: '',
    });
  }
  handleCancel = () => {
    this.resetState();
  }
  handleOnChange = (value) => {
    const parsed = parseFloat(value)
    if (Number.isNaN(parsed)) {
      return
    }
    const vests = parsed.toFixed(6);
    const props = this.props.steem.props;
    const totalVestsSteem = parseFloat(props.total_vesting_fund_steem.split(" ")[0])
    const totalVests = parseFloat(props.total_vesting_shares.split(" ")[0])
    const sp = (totalVestsSteem * vests / totalVests).toFixed(3);
    this.setState({ vests, sp });
  }

  handleOnChangeComplete = (value) => {
    const parsed = parseFloat(value)
    if (Number.isNaN(parsed)) {
      return
    }
    const vests = parsed.toFixed(6);
    const props = this.props.steem.props;
    const totalVestsSteem = parseFloat(props.total_vesting_fund_steem.split(" ")[0])
    const totalVests = parseFloat(props.total_vesting_shares.split(" ")[0])
    const sp = (totalVestsSteem * vests / totalVests).toFixed(3);
    this.setState({ vests, sp });
  }
  handleVestingSharesRemove = (e, props) => {
    let { delegator, delegatee, id } = props.value.delegatee;
    delegatee = delegatee.toLowerCase().replace('@', '');
    const permissions = this.props.keys.permissions;
    this.setState({undelegateError: false})
    this.props.actions.useKey('setDelegateVestingShares', { delegator, delegatee, vestingShares: 0.000000 }, permissions[delegator])
  }
  handleVestingSharesEdit = (e, props) => {
    const t = this
    let { delegator, delegatee, id, vesting_shares } = props.value.delegatee;
    this.setState({
      editDelegationFor: delegator,
      [delegator]: delegatee,
    }, () => {
      this.handleOnChange(parseFloat(vesting_shares.split(" ")[0]))
    })
  }
  handleSetDelegateVesting = (e, props) => {
    this.setState({
      editDelegationFor: props.value
    })
  }
  handleSetDelegateVestingConfirm = (e, props) => {
    const { vests } = this.state;
    const { permissions } = this.props.keys;
    const delegator = this.state.editDelegationFor;
    const delegatee = this.state[delegator];
    this.props.actions.useKey('setDelegateVestingShares', { delegator, delegatee, vestingShares: vests }, permissions[delegator])
    e.preventDefault();
  }
  handleOnChangeInput = (e, props) => {
    const { name, value } = props
    const globalProps = this.props.steem.props;
    const totalVestsSteem = parseFloat(globalProps.total_vesting_fund_steem.split(" ")[0])
    const totalVests = parseFloat(globalProps.total_vesting_shares.split(" ")[0])
    const parsed = parseFloat(value)
    let vests, sp
    if (Number.isNaN(parsed)) {
      this.setState({ [name]: value });
      return
    }
    if (name === 'vests') {
      const fixed = parsed.toFixed(6);
      sp = (totalVestsSteem * fixed / totalVests).toFixed(3)
      vests = value
    }
    if (name === 'sp') {
      const fixed = parsed.toFixed(3);
      sp = value
      vests = (fixed / totalVestsSteem * totalVests).toFixed(6)
    }
    this.setState({ vests, sp });
  }
  handleChangeVestingShares = (e, props) => {
    const editing = this.state.editDelegationFor;
    const newState = {};
    newState[editing] = props.value.trim();
    this.setState(newState);
  }
  render() {
    const t = this;
    let addVesting = false;
    let undelegateError = false;
    if (this.state.undelegateError) {
      undelegateError = (
        <Message
          error
          header='Operation Error'
          content={this.state.undelegateError}
        />
      )
    }
    const numberFormat = {
      shortFormat: true,
      shortFormatMinValue: 1000
    };
    const {
      account_delegate_vesting_shares_error,
      account_delegate_vesting_shares_pending,
      account_delegate_vesting_shares_resolved
    } = this.props.processing;
    if (this.state.editDelegationFor) {
      const name = this.state.editDelegationFor;
      const account = this.props.account.accounts[name];
      const delegated = parseFloat(account.delegated_vesting_shares.split(" ")[0]);
      const vests = parseFloat(account.vesting_shares.split(" ")[0]);
      let existingDelegation = 0
      if (this.props.account.vestingDelegations && this.props.account.vestingDelegations[name]) {
        const existingDelegations = this.props.account.vestingDelegations[name]
        existingDelegation = existingDelegations.reduce((a, b) => (b.delegatee === this.state[name]) ? a + parseFloat(b.vesting_shares.split(" ")[0]) : 0, 0)
      }
      const available = vests - delegated + existingDelegation;
      const target = parseFloat(this.state.vests)
      const delegateWarning = (target > available - 100)
      addVesting = (
        <Modal
          size="small"
          open
          header="Delegate Vests to another Account"
          onClose={this.resetState}
          content={
            <Form
              loading={account_delegate_vesting_shares_pending}
            >
              <Segment
                padded
                basic
              >
                <p>
                  Please enter the name of the target account that you wish to delegate
                  a portion of the {name} account's vested weight to.
                </p>
                <Segment padded>
                  <Form.Field>
                    <label>Delegatee Account Name</label>
                    <Input
                      fluid
                      name="delegatee"
                      placeholder="Delegatee Account Name"
                      defaultValue={this.state[name]}
                      autoFocus
                      onChange={this.handleChangeVestingShares}
                    />
                  </Form.Field>
                  <Form.Field>
                    <label>Steem Power</label>
                    <Input
                      fluid
                      name="sp"
                      value={this.state.sp}
                      onChange={this.handleOnChangeInput}
                    />
                  </Form.Field>
                  <Form.Field>
                    <label>Vesting Shares</label>
                    <Input
                      fluid
                      name="vests"
                      value={this.state.vests}
                      onChange={this.handleOnChangeInput}
                    />
                  </Form.Field>
                </Segment>
                <Grid>
                  <Grid.Row>
                    <Grid.Column width={12}>
                      <Segment padded="very" basic>
                        <InputRange
                          maxValue={available}
                          minValue={0}
                          value={this.state.vests}
                          onChange={this.handleOnChange}
                          onChangeComplete={this.handleOnChangeComplete}
{'fraction_non_alphanumeric': 0.1113834422657952, 'fraction_numerical': 0.0054466230936819175, 'mean_word_length': 2.244037102473498, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 69, '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': '1770401', 'n_tokens_mistral': 4369, 'n_tokens_neox': 4249, 'n_words': 827}