AccountRewritePhrase.js 2.98 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
YJ's avatar
YJ committed
import React, { Component } from 'react';
import { AccountCard, Card, Form as FetherForm } from 'fether-ui';
YJ's avatar
YJ committed
import { inject, observer } from 'mobx-react';
YJ's avatar
YJ committed
import AccountImportOptions from '../AccountImportOptions';
YJ's avatar
YJ committed
@inject('createAccountStore')
@observer
class AccountRewritePhrase extends Component {
  state = {
YJ's avatar
YJ committed
    value: ''
  handleChange = ({ target: { value } }) => {
    this.setState({ value });
  };
Amaury Martiny's avatar
Amaury Martiny committed
  handleSubmit = async () => {
    const {
      history,
YJ's avatar
YJ committed
      createAccountStore: { isImport, setPhrase }
    } = this.props;
    const currentStep = pathname.slice(-1);
YJ's avatar
YJ committed
    const { value } = this.state;

    // If we're importing, derive address from recovery phrase when we submit
YJ's avatar
YJ committed
    if (isImport) {
      this.setState({ isLoading: true });
      await setPhrase(value);
    }
    history.push(`/accounts/new/${+currentStep + 1}`);
  };

YJ's avatar
YJ committed
  render () {
YJ's avatar
YJ committed
      createAccountStore: { address, isImport, name },
      history,
      location: { pathname }
    } = this.props;
    const { value } = this.state;
    const currentStep = pathname.slice(-1);
Amaury Martiny's avatar
Amaury Martiny committed
      <form key='createAccount' onSubmit={this.handleSubmit}>
YJ's avatar
YJ committed
        <div className='text -centered'>
YJ's avatar
YJ committed
            <AccountImportOptions />
          ) : (
            <p>
              Type your secret phrase to confirm that you wrote it down
              correctly:
            </p>
          )}
        </div>
YJ's avatar
YJ committed
        <FetherForm.Field
YJ's avatar
YJ committed
          autoFocus
YJ's avatar
YJ committed
          as='textarea'
          label='Recovery phrase'
          onChange={this.handleChange}
          required
          value={value}
        />
YJ's avatar
YJ committed
        <nav className='form-nav -space-around'>
Amaury Martiny's avatar
Amaury Martiny committed
            <button
Luke Schoen's avatar
Luke Schoen committed
              className='button -back'
Amaury Martiny's avatar
Amaury Martiny committed
              onClick={history.goBack}
              type='button'
            >
Amaury Martiny's avatar
Amaury Martiny committed
      </form>
      <AccountCard
        address={address}
YJ's avatar
YJ committed
        name={address && !name ? '(no name)' : name}
Thibaut Sardan's avatar
Thibaut Sardan committed
        drawers={body}
Amaury Martiny's avatar
Amaury Martiny committed

  renderButton = () => {
    const {
      createAccountStore: { isImport, bip39Phrase }
    } = this.props;
YJ's avatar
YJ committed
    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 (
Amaury Martiny's avatar
Amaury Martiny committed
        <button className='button' disabled={value !== bip39Phrase}>
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 (
Amaury Martiny's avatar
Amaury Martiny committed
      <button className='button' disabled={!value.length || isLoading}>
Amaury Martiny's avatar
Amaury Martiny committed
        Next
      </button>
    );
  };
export default AccountRewritePhrase;