<commit_before>import { service, send } from "connectors";
import SendPage from "./Page";
import ErrorScreen from "ErrorScreen";
import { FormattedMessage as T } from "react-intl";
import { spring, presets } from "react-motion";
import OutputRow from "./OutputRow";
import { DescriptionHeader } from "layout";

export const SendTabHeader = service(({ isTestNet }) =>
  <DescriptionHeader
    description={isTestNet
      ? <T id="transactions.description.send.testnet" m={"Testnet Decred addresses always begin with letter T and contain 26-35 alphanumeric characters\n(e.g. TxxXXXXXxXXXxXXXXxxx0XxXXXxxXxXxX0)."} />
      : <T id="transactions.description.send.mainnet" m={"Mainnet Decred addresses always begin with letter D and contain 26-35 alphanumeric characters\n(e.g. DxxXXXXXxXXXxXXXXxxx0XxXXXxxXxXxX0X)."} />}
  />);

@autobind
class Send extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isShowingConfirm: false,
      isSendAll: false,
      isSendSelf: false,
      outputs: [ { key: "output_0", data: this.getBaseOutput() } ],
      sendAllAmount: this.props.totalSpent,
      unsignedRawTx: null,
      account: this.props.defaultSpendingAccount,
      insuficientFunds: false,
    };
  }

  componentDidUpdate(prevProps) {
    const { constructTxLowBalance, unsignedRawTx, isWatchingOnly, nextAddress, publishTxResponse } = this.props;
    const { isSendSelf, outputs } = this.state;
    if (publishTxResponse && publishTxResponse !== prevProps.publishTxResponse) {
      this.setState({ outputs: [ { key: "output_0", data: this.getBaseOutput() } ] });
    }
    if (isSendSelf && (prevProps.nextAddress != nextAddress)) {
      let newOutputs = outputs.map(o => ({ ...o, data:{ ...o.data, destination: nextAddress } }));
      this.setState({ outputs: newOutputs }, this.onAttemptConstructTransaction);
    }
    if (constructTxLowBalance !== prevProps.constructTxLowBalance) {
      if (constructTxLowBalance) {
        this.setState({ insuficientFunds: true });
      } else {
        this.setState({ insuficientFunds: false });
      }
    }
    if (unsignedRawTx !== prevProps.unsignedRawTx && isWatchingOnly) {
      this.setState({ unsignedRawTx });
    }
  }

  render() {
    if (!this.props.walletService) {
      return  <ErrorScreen />;
    }

    const {
      onChangeAccount,
      onShowConfirm,
      onShowSendAll,
      onHideSendAll,
      onShowSendSelf,
      onShowSendOthers,
      onAttemptConstructTransaction,
      onAddOutput,
      onValidateAmount,
      willEnter,
      willLeave,
      getStyles,
      getDefaultStyles,
      onKeyDown,
      resetShowPassphraseModal,
    } = this;
    const isValid = this.getIsValid();
    const showPassphraseModal = this.getShowPassphraseModal();

    return (
      <SendPage
        {...{ ...this.props, ...this.state }}
        {...{
          isValid,
          onKeyDown,
          onChangeAccount,
          onShowConfirm,
          onShowSendAll,
          onHideSendAll,
          onShowSendSelf,
          onShowSendOthers,
          onAttemptConstructTransaction,
          onAddOutput,
          onValidateAmount,
          willEnter,
          willLeave,
          getStyles,
          getDefaultStyles,
          showPassphraseModal,
          resetShowPassphraseModal,
        }}
      />
    );
  }

  getBaseOutput() {
    return { destination: "", amount: null, value: null, error: { address: null, amount: null } };
  }

  getDefaultStyles() {
    return this.state.outputs.map(output => ({ ...output, style: { height: 0, opacity: 1 } }));
  }

  getStyles() {
    const { outputs, isSendAll, sendAllAmount, isSendSelf } = this.state;
    const { totalSpent } = this.props;
    const {
      onChangeAccount, onShowSendSelf, onShowSendOthers, onKeyDown, onAddOutput,
    } = this;
    const {
      onValidateAddress, onValidateAmount, onRemoveOutput, onShowSendAll, onHideSendAll,
    } = this;
    return outputs.map((output, index) => ({
      data: <OutputRow
        {...{ ...this.props, index, outputs, ...output.data, isSendAll, isSendSelf, totalSpent, sendAllAmount,
          onValidateAddress, onValidateAmount, onShowSendAll, onHideSendAll, onRemoveOutput,   onChangeAccount,
          onShowSendSelf, onShowSendOthers, onAddOutput, onKeyDown, }}
      />,
      key: "output_" + index,
      style: {
        opacity: spring(1, presets.gentle),
      }
    }));
  }

  willEnter() {
    return {
      opacity: 0,
    };
  }

  willLeave() {
    return {
      opacity: spring(0, { stiffness: 210, damping: 20 }),
    };
  }

  onChangeAccount(account) {
    this.setState({ account }, this.onAttemptConstructTransaction);
  }

  onClearTransaction() {
    this.props.onClearTransaction();
  }
  onShowSendAll() {
    const { account, outputs } = this.state;<commit_msg>[send] Fix AccountsSelect (#2240)<commit_after>import { service, send } from "connectors";
import SendPage from "./Page";
import ErrorScreen from "ErrorScreen";
import { FormattedMessage as T } from "react-intl";
import { spring, presets } from "react-motion";
import OutputRow from "./OutputRow";
import { DescriptionHeader } from "layout";

export const SendTabHeader = service(({ isTestNet }) =>
  <DescriptionHeader
    description={isTestNet
      ? <T id="transactions.description.send.testnet" m={"Testnet Decred addresses always begin with letter T and contain 26-35 alphanumeric characters\n(e.g. TxxXXXXXxXXXxXXXXxxx0XxXXXxxXxXxX0)."} />
      : <T id="transactions.description.send.mainnet" m={"Mainnet Decred addresses always begin with letter D and contain 26-35 alphanumeric characters\n(e.g. DxxXXXXXxXXXxXXXXxxx0XxXXXxxXxXxX0X)."} />}
  />);

@autobind
class Send extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isShowingConfirm: false,
      isSendAll: false,
      isSendSelf: false,
      outputs: [ { key: "output_0", data: this.getBaseOutput() } ],
      sendAllAmount: this.props.totalSpent,
      unsignedRawTx: null,
      account: this.props.defaultSpendingAccount,
      insuficientFunds: false,
    };
  }

  componentDidUpdate(prevProps) {
    const { constructTxLowBalance, unsignedRawTx, isWatchingOnly, nextAddress, publishTxResponse } = this.props;
    const { isSendSelf, outputs } = this.state;
    if (publishTxResponse && publishTxResponse !== prevProps.publishTxResponse) {
      this.setState({ outputs: [ { key: "output_0", data: this.getBaseOutput() } ] });
    }
    if (isSendSelf && (prevProps.nextAddress != nextAddress)) {
      let newOutputs = outputs.map(o => ({ ...o, data:{ ...o.data, destination: nextAddress } }));
      this.setState({ outputs: newOutputs }, this.onAttemptConstructTransaction);
    }
    if (constructTxLowBalance !== prevProps.constructTxLowBalance) {
      if (constructTxLowBalance) {
        this.setState({ insuficientFunds: true });
      } else {
        this.setState({ insuficientFunds: false });
      }
    }
    if (unsignedRawTx !== prevProps.unsignedRawTx && isWatchingOnly) {
      this.setState({ unsignedRawTx });
    }
  }

  render() {
    if (!this.props.walletService) {
      return  <ErrorScreen />;
    }

    const {
      onChangeAccount,
      onShowConfirm,
      onShowSendAll,
      onHideSendAll,
      onShowSendSelf,
      onShowSendOthers,
      onAttemptConstructTransaction,
      onAddOutput,
      onValidateAmount,
      willEnter,
      willLeave,
      getStyles,
      getDefaultStyles,
      onKeyDown,
      resetShowPassphraseModal,
    } = this;
    const isValid = this.getIsValid();
    const showPassphraseModal = this.getShowPassphraseModal();

    return (
      <SendPage
        {...{ ...this.props, ...this.state }}
        {...{
          isValid,
          onKeyDown,
          onChangeAccount,
          onShowConfirm,
          onShowSendAll,
          onHideSendAll,
          onShowSendSelf,
          onShowSendOthers,
          onAttemptConstructTransaction,
          onAddOutput,
          onValidateAmount,
          willEnter,
          willLeave,
          getStyles,
          getDefaultStyles,
          showPassphraseModal,
          resetShowPassphraseModal,
        }}
      />
    );
  }

  getBaseOutput() {
    return { destination: "", amount: null, value: null, error: { address: null, amount: null } };
  }

  getDefaultStyles() {
    return this.state.outputs.map(output => ({ ...output, style: { height: 0, opacity: 1 } }));
  }

  getStyles() {
    const { outputs, isSendAll, sendAllAmount, isSendSelf, account } = this.state;
    const { totalSpent } = this.props;
    const {
      onChangeAccount, onShowSendSelf, onShowSendOthers, onKeyDown, onAddOutput,
    } = this;
    const {
      onValidateAddress, onValidateAmount, onRemoveOutput, onShowSendAll, onHideSendAll,
    } = this;
    return outputs.map((output, index) => ({
      data: <OutputRow
        {...{
          ...this.props, index, outputs, account, ...output.data, isSendAll, isSendSelf, totalSpent, sendAllAmount,
          onValidateAddress, onValidateAmount, onShowSendAll, onHideSendAll, onRemoveOutput,   onChangeAccount,
          onShowSendSelf, onShowSendOthers, onAddOutput, onKeyDown, }}
      />,
      key: "output_" + index,
      style: {
        opacity: spring(1, presets.gentle),
      }
    }));
  }

  willEnter() {
    return {
      opacity: 0,
    };
  }

  willLeave() {
    return {
      opacity: spring(0, { stiffness: 210, damping: 20 }),
    };
  }

  onChangeAccount(account) {
    this.setState({ account }, this.onAttemptConstructTransaction);
  }

  onClearTransaction() {
    this.props.onClearTransaction();
  }
  onShowSendAll() {
    const { account, outputs } = this.state;
{'fraction_non_alphanumeric': 0.11938607334157396, 'fraction_numerical': 0.005356407086938607, 'mean_word_length': 2.8044670846394983, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, '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': '2270943', 'n_tokens_mistral': 2968, 'n_tokens_neox': 2941, 'n_words': 606}