<commit_before>import React from "react";
import classNames from "classnames";
import AccountActions from "actions/AccountActions";
import AccountStore from "stores/AccountStore";
import AccountNameInput from "./../Forms/AccountNameInput";
import PasswordInput from "./../Forms/PasswordInput";
import WalletDb from "stores/WalletDb";
import notify from 'actions/NotificationActions';
import {Link} from "react-router";
import AccountImage from "./AccountImage";
import AccountSelect from "../Forms/AccountSelect";
import WalletUnlockActions from "actions/WalletUnlockActions";
import TransactionConfirmStore from "stores/TransactionConfirmStore";
import LoadingIndicator from "../LoadingIndicator";


class CreateAccount extends React.Component {
    constructor() {
        super();
        this.state = {validAccountName: false, accountName: "", validPassword: false, registrar_account: null, loading: false};
        this.onFinishConfirm = this.onFinishConfirm.bind(this)
    }

    shouldComponentUpdate(nextProps, nextState) {
        return nextState.accountName !== this.state.accountName
               || nextState.validAccountName !== this.state.validAccountName
               || nextState.validPassword !== this.state.validPassword
               || nextState.registrar_account !== this.state.registrar_account
               || nextState.loading !== this.state.loading;
    }

    onAccountNameChange(e) {
        let state = {validAccountName: e.valid};
        if(e.value || e.value === "") state.accountName = e.value;
        this.setState(state);
    }

    onPasswordChange(e) {
        this.setState({validPassword: e.valid});
    }

    onFinishConfirm(confirm_store_state) {
        if(confirm_store_state.broadcast && confirm_store_state.closed && confirm_store_state.broadcasted_transaction) {
            let trx_obj = confirm_store_state.broadcasted_transaction.toObject();
            let op0 = trx_obj.operations[0];
            if(op0[0] === 5 && op0[1].name === this.state.accountName) {
                this.context.router.transitionTo("account-overview", {account_name: this.state.accountName});
            }
            TransactionConfirmStore.unlisten(this.onFinishConfirm);
        }
    }

    createAccount(name) {
        WalletUnlockActions.unlock().then(() => {
            this.setState({loading: true});
            AccountActions.createAccount(name, this.state.registrar_account, this.state.registrar_account).then(() => {
                if(this.state.registrar_account) {
                    this.setState({loading: false});
                    TransactionConfirmStore.listen(this.onFinishConfirm);
                } else {
                    this.context.router.transitionTo("account-overview", {account_name: name});
                }
            }).catch(error => {
                console.log("ERROR AccountActions.createAccount", error);
                const error_msg = error.base && error.base.length && error.base.length > 0 ? error.base[0] : "unknown error";
                notify.addNotification({
                    message: `Failed to create account: ${name} - ${error_msg}`,
                    level: "error",
                    autoDismiss: 10
                });
                this.setState({loading: false});
            });
        });
    }

    createWallet(password) {
        return WalletActions.setWallet(
            null, //wallet name<commit_msg>Fixes call to WalletAcctions on signup account creation #192<commit_after>import React from "react";
import classNames from "classnames";
import AccountActions from "actions/AccountActions";
import AccountStore from "stores/AccountStore";
import AccountNameInput from "./../Forms/AccountNameInput";
import PasswordInput from "./../Forms/PasswordInput";
import WalletDb from "stores/WalletDb";
import notify from 'actions/NotificationActions';
import {Link} from "react-router";
import AccountImage from "./AccountImage";
import AccountSelect from "../Forms/AccountSelect";
import WalletUnlockActions from "actions/WalletUnlockActions";
import TransactionConfirmStore from "stores/TransactionConfirmStore";
import LoadingIndicator from "../LoadingIndicator";
import WalletActions from "actions/WalletActions"

class CreateAccount extends React.Component {
    constructor() {
        super();
        this.state = {validAccountName: false, accountName: "", validPassword: <PASSWORD>, registrar_account: null, loading: false};
        this.onFinishConfirm = this.onFinishConfirm.bind(this)
    }

    shouldComponentUpdate(nextProps, nextState) {
        return nextState.accountName !== this.state.accountName
               || nextState.validAccountName !== this.state.validAccountName
               || nextState.validPassword !== this.state.validPassword
               || nextState.registrar_account !== this.state.registrar_account
               || nextState.loading !== this.state.loading;
    }

    onAccountNameChange(e) {
        let state = {validAccountName: e.valid};
        if(e.value || e.value === "") state.accountName = e.value;
        this.setState(state);
    }

    onPasswordChange(e) {
        this.setState({validPassword: e.valid});
    }

    onFinishConfirm(confirm_store_state) {
        if(confirm_store_state.broadcast && confirm_store_state.closed && confirm_store_state.broadcasted_transaction) {
            let trx_obj = confirm_store_state.broadcasted_transaction.toObject();
            let op0 = trx_obj.operations[0];
            if(op0[0] === 5 && op0[1].name === this.state.accountName) {
                this.context.router.transitionTo("account-overview", {account_name: this.state.accountName});
            }
            TransactionConfirmStore.unlisten(this.onFinishConfirm);
        }
    }

    createAccount(name) {
        WalletUnlockActions.unlock().then(() => {
            this.setState({loading: true});
            AccountActions.createAccount(name, this.state.registrar_account, this.state.registrar_account).then(() => {
                if(this.state.registrar_account) {
                    this.setState({loading: false});
                    TransactionConfirmStore.listen(this.onFinishConfirm);
                } else {
                    this.context.router.transitionTo("account-overview", {account_name: name});
                }
            }).catch(error => {
                console.log("ERROR AccountActions.createAccount", error);
                const error_msg = error.base && error.base.length && error.base.length > 0 ? error.base[0] : "unknown error";
                notify.addNotification({
                    message: `Failed to create account: ${name} - ${error_msg}`,
                    level: "error",
                    autoDismiss: 10
                });
                this.setState({loading: false});
            });
        });
    }

    createWallet(password) {
        return WalletActions.setWallet(
            "default", //wallet name
{'fraction_non_alphanumeric': 0.1172750790457028, 'fraction_numerical': 0.0035929864903707963, 'mean_word_length': 2.672295514511873, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 12, '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': '6072285', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1774, 'n_words': 343}