CreateAccount.js 3.26 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 { 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 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
Amaury Martiny's avatar
Amaury Martiny committed
            <h1>{isImport ? 'Import account' : 'Create a new account'}</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>

        <nav className='footer-nav'>
          {step > 1 ? (
            <div className='footer-nav_status'>
              <Health />
            </div>
          ) : (
            <div className='footer-nav_option'>
Thibaut Sardan's avatar
Thibaut Sardan committed
              {isImport ? null : (
                <p>
                  Already have an account?
                  <button
                    className='button -footer'
                    onClick={this.handleToggleCreateImport}
                  >
                    Import account
                  </button>
                </p>
              )}
            </div>
          )}
      </React.Fragment>
    );
  }
}

export default CreateAccount;