AccountRewritePhrase.js 3.35 KiB
Newer Older
// Copyright 2015-2019 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';
import i18n, { packageNS } from '../../../i18n';
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 />
              {i18n.t(
                `${packageNS}:account.phrase_rewrite.label_msg_rewrite_phrase`
              )}
YJ's avatar
YJ committed
        <FetherForm.Field
YJ's avatar
YJ committed
          autoFocus
YJ's avatar
YJ committed
          as='textarea'
          label={i18n.t(
            `${packageNS}:account.phrase_rewrite.label_rewrite_phrase`
          )}
YJ's avatar
YJ committed
          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'
            >
              {i18n.t(`${packageNS}:navigation.back`)}
Amaury Martiny's avatar
Amaury Martiny committed
      </form>
    return isImport ? (
      <Card>{body}</Card>
    ) : (
      <AccountCard
        address={address}
        name={
          address && !name
            ? i18n.t(`${packageNS}:account.existing.no_name`)
            : name
        }
        drawers={body}
        i18n={i18n}
        packageNS={packageNS}
      />
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}>
          {i18n.t(`${packageNS}:navigation.next`)}
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}>
        {i18n.t(`${packageNS}:navigation.next`)}
Amaury Martiny's avatar
Amaury Martiny committed
      </button>
    );
  };
export default AccountRewritePhrase;