<commit_before>import React from "react";
import utils from "common/utils"
import validation from "common/validation"
import AccountImage from "../Account/AccountImage";
import Translate from "react-translate-component";
import ChainStore from "api/ChainStore";
import ChainTypes from "../Utility/ChainTypes";
import BindToChainState from "../Utility/BindToChainState";

/**
 * @brief Allows the user to enter an account by name or #ID
 *
 * This component is designed to be stateless as possible.  It's primary responsbility is to
 * manage the layout of data and to filter the user input.
 *
 * Properties:
 *    label         - a translation key for the label
 *    error         - a transaltion key for the error message override, displayed when there is otherwise no error
 *    onChange      - a method to be called any time user input changes
 *    placeholder   - the placeholder text to be displayed when there is no user_input
 *    accountName   - the current value of the account selector, the string the user enters
 *    account       - account object retrieved via BindToChainState decorator
 *    tabIndex      - tabindex property to be passed to input tag
 *
 */

@BindToChainState()
class AccountSelector extends React.Component {

    static propTypes = {
        account: ChainTypes.ChainAccount
    }

    // can be used in parent component: this.refs.account_selector.getAccount()
    getAccount() {
        return this.props.account;
    }

    getNameType(value) {
        if(!value) return null;
        if(value[0] === "#" && utils.is_object_id("1.2." + value.substring(1))) return "id";
        if(validation.is_account_name(value)) return "name";
        return null;
    }

    onInputChanged(event) {
        let value = event.target.value.trim().toLowerCase();
        if
        (   this.props.onChange
            && (value.length < 4 || this.getNameType(value))
            && value !== this.props.accountName
        ) this.props.onChange(value);
    }

    onKeyDown(e) {
        if (event.keyCode === 13) this.onInputChanged(e);
    }

    render() {
        //console.log("-- AccountSelector.render -->", this.props.accountName, this.props.account ? this.props.account.toJS().name : "-");
        let error = this.props.error;
        if (!error && this.props.accountName && !this.getNameType(this.props.accountName))
            error = "invalid account name";

        let lookup_display = null;
        if(this.props.account) {
            let type = this.getNameType(this.props.accountName);
            if(type === "name") lookup_display = "#" + this.props.account.get("id").substring(4);
            else if(type === "id") lookup_display = this.props.account.get("name");
        }

        let member_status = null;
        if (this.props.account)<commit_msg>added onAccountChanged support to AccountSelector<commit_after>import React from "react";
import utils from "common/utils"
import validation from "common/validation"
import AccountImage from "../Account/AccountImage";
import Translate from "react-translate-component";
import ChainStore from "api/ChainStore";
import ChainTypes from "../Utility/ChainTypes";
import BindToChainState from "../Utility/BindToChainState";

/**
 * @brief Allows the user to enter an account by name or #ID
 *
 * This component is designed to be stateless as possible.  It's primary responsbility is to
 * manage the layout of data and to filter the user input.
 *
 */

@BindToChainState()
class AccountSelector extends React.Component {

    static propTypes = {
        label: React.PropTypes.string.isRequired, // a translation key for the label
        error: React.PropTypes.string, // the error message override
        placeholder: React.PropTypes.string, // the placeholder text to be displayed when there is no user_input
        onChange: React.PropTypes.func, // a method to be called any time user input changes
        onAccountChanged: React.PropTypes.func, // a method to be called when existing account is selected
        accountName: React.PropTypes.string, // the current value of the account selector, the string the user enters
        account: ChainTypes.ChainAccount, // account object retrieved via BindToChainState decorator (not input)
        tabIndex: React.PropTypes.number // tabindex property to be passed to input tag
    }

    // can be used in parent component: this.refs.account_selector.getAccount()
    getAccount() {
        return this.props.account;
    }

    getNameType(value) {
        if(!value) return null;
        if(value[0] === "#" && utils.is_object_id("1.2." + value.substring(1))) return "id";
        if(validation.is_account_name(value)) return "name";
        return null;
    }

    onInputChanged(event) {
        let value = event.target.value.trim().toLowerCase();
        if
        (   this.props.onChange
            && (value.length < 4 || this.getNameType(value))
            && value !== this.props.accountName
        ) this.props.onChange(value);
    }

    onKeyDown(e) {
        if (event.keyCode === 13) this.onInputChanged(e);
    }

    componentWillReceiveProps(newProps) {
        if(this.props.onAccountChanged && newProps.account !== this.props.account)
            this.props.onAccountChanged(newProps.account);
    }

    render() {
        let error = this.props.error;
        if (!error && this.props.accountName && !this.getNameType(this.props.accountName))
            error = "invalid account name";

        let lookup_display = null;
        if(this.props.account) {
            let type = this.getNameType(this.props.accountName);
            if(type === "name") lookup_display = "#" + this.props.account.get("id").substring(4);
            else if(type === "id") lookup_display = this.props.account.get("name");
        }

        let member_status = null;
        if (this.props.account)
{'fraction_non_alphanumeric': 0.11128178870114354, 'fraction_numerical': 0.00273084144051886, 'mean_word_length': 2.8200782268578877, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '5518419', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1580, 'n_words': 476}