AccountRewritePhrase.js 3.24 KiB
Newer Older
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
Amaury Martiny's avatar
Amaury Martiny committed
// SPDX-License-Identifier: BSD-3-Clause
import React, { Component } from 'react';
import { AccountCard, FormField } from 'fether-ui';
import debounce from 'lodash/debounce';
import { inject, observer } from 'mobx-react';

@inject('createAccountStore')
@observer
class AccountRewritePhrase extends Component {
  state = {
    isLoading: false,
    value: ''
  };

  handleChange = ({ target: { value } }) => {
    const {
      createAccountStore: { isImport }
    } = this.props;

    this.setState({ value });

    // If we're importing, we show the current address generated by the current
    // recovery phrase
    if (isImport) {
      this.setState({ isLoading: true });
      this.handleSavePhrase();
    }
  };
  handleNextStep = () => {
    const {
      history,
      location: { pathname }
    } = this.props;
    const currentStep = pathname.slice(-1);
    history.push(`/accounts/new/${+currentStep + 1}`);
  };

  handleSavePhrase = debounce(() => {
    const {
      createAccountStore: { setPhrase }
    } = this.props;
Amaury Martiny's avatar
Amaury Martiny committed
    const { value } = this.state;

    return setPhrase(value).then(() => {
      this.setState({ isLoading: false });
    });
  }, 1000);
Amaury Martiny's avatar
Amaury Martiny committed

Amaury Martiny's avatar
Amaury Martiny committed
  render () {
      createAccountStore: { address, isImport, name },
      history,
      location: { pathname }
    } = this.props;
    const { value } = this.state;
    const currentStep = pathname.slice(-1);

    return (
      <AccountCard
        address={address}
        name={address && !name ? '(no name)' : name}
        drawers={[
          <div key='createAccount'>
            <div className='text'>
              {isImport ? (
                <p>Type your Recovery phrase</p>
              ) : (
                <p>
                  Type your secret phrase to confirm that you wrote it down
                  correctly:
                </p>
              )}
            </div>
            <FormField
              input={
                <textarea onChange={this.handleChange} required value={value} />
              }
              label='Recovery phrase'
            />

            <nav className='form-nav -space-around'>
              {currentStep > 1 && (
                <button className='button -cancel' onClick={history.goBack}>
                  Back
                </button>
              )}
              {this.renderButton()}
            </nav>
Amaury Martiny's avatar
Amaury Martiny committed

  renderButton = () => {
    const {
      createAccountStore: { isImport, phrase }
    } = this.props;
    const { isLoading, value } = this.state;
Amaury Martiny's avatar
Amaury Martiny committed

    // If we are creating a new account, the button just checks the phrase has
    // been correctly written by the user.
    if (!isImport) {
      return (
        <button
          className='button'
          disabled={isLoading || value !== phrase}
          onClick={this.handleNextStep}
        >
          Next
        </button>
      );
Amaury Martiny's avatar
Amaury Martiny committed
    }

    // If we are importing an existing account, the button goes to the next step
Amaury Martiny's avatar
Amaury Martiny committed
    return (
        disabled={isLoading || !value.length}
        onClick={this.handleNextStep}
Amaury Martiny's avatar
Amaury Martiny committed
        Next
      </button>
    );
  };
export default AccountRewritePhrase;