CreateAccount.js 3.47 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
import React, { Component } from 'react';
import { Header } from 'fether-ui';
import { inject, observer } from 'mobx-react';
import { Link, Route } from 'react-router-dom';
import AccountCopyPhrase from './AccountCopyPhrase';
YJ's avatar
YJ committed
import AccountImportOptions from './AccountImportOptions';
import AccountRewritePhrase from './AccountRewritePhrase';
import AccountName from './AccountName';
import AccountPassword from './AccountPassword';
import Health from '../../Health';
import i18n, { packageNS } from '../../i18n';
import withAccountsInfo from '../../utils/withAccountsInfo';
@inject('createAccountStore')
@withAccountsInfo
@observer
class CreateAccount extends Component {
  constructor (props) {
    super(props);
    props.createAccountStore.clear();
  }

  /**
   * Creating account and importing accounts have different processes: 4 steps
   * for importing, and 5 steps for creating
   */
YJ's avatar
YJ committed
  getSteps = isImport =>
YJ's avatar
YJ committed
      ? [AccountImportOptions, AccountName, AccountPassword]
      : [AccountName, AccountCopyPhrase, AccountRewritePhrase, AccountPassword];
Amaury Martiny's avatar
Amaury Martiny committed
  handleToggleCreateImport = () => {
    const {
      createAccountStore,
      history,
      match: {
        params: { step }
      }
    } = this.props;
    createAccountStore.clear();
Amaury Martiny's avatar
Amaury Martiny committed
    createAccountStore.setIsImport(!createAccountStore.isImport);
    // If we were further in the account creation, go back to step 1
    if (step > 1) {
      history.push('/accounts/new/1');
    }
Amaury Martiny's avatar
Amaury Martiny committed
  render () {
      accountsInfo,
YJ's avatar
YJ committed
      createAccountStore: { isImport },
Amaury Martiny's avatar
Amaury Martiny committed
      match: {
        params: { step }
Amaury Martiny's avatar
Amaury Martiny committed
      }
    } = this.props;

    // Get all the steps of our account process
YJ's avatar
YJ committed
    const Steps = this.getSteps(isImport);
    return (
      <React.Fragment>
        <Header
          left={
            // Show back button if we already have some accounts, so we can go back to AccountsList
            accountsInfo &&
            Object.keys(accountsInfo).length > 0 && (
              <Link className='icon -back' to='/accounts'>
                Back
            <h1>
              {isImport
                ? i18n.t(`${packageNS}:account.import.title`)
                : i18n.t(`${packageNS}:account.create.title`)}
            </h1>
        <div className='window_content'>
Amaury Martiny's avatar
Amaury Martiny committed
          <div className='box -padded'>
            {Steps.map((StepComponent, index) => (
              <Route
                component={StepComponent}
                key={`Step${index + 1}`}
                path={`/accounts/new/${index + 1}`}
              />
            ))}
          </div>
        {isImport ? null : (
          <nav className='footer-nav'>
            {step > 1 ? (
              <div className='footer-nav_status'>
                <Health />
              </div>
            ) : (
              <div className='footer-nav_option'>
                  {i18n.t(`${packageNS}:account.import.question`)}
                  <button
                    className='button -footer'
                    onClick={this.handleToggleCreateImport}
                  >
                    {i18n.t(`${packageNS}:account.import.title`)}
                  </button>
                </p>
      </React.Fragment>
    );
  }
}

export default CreateAccount;