AccountPassword.js 3.25 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, Form as FetherForm } from 'fether-ui';
import { inject, observer } from 'mobx-react';

@inject('createAccountStore')
@observer
class AccountPassword extends Component {
  state = {
    confirm: '',
    password: '',
    error: ''
  };

  handleConfirmChange = ({ target: { value } }) => {
    this.setState({ confirm: value });
  };

YJ's avatar
YJ committed
  handleKeyPress = e => {
    if (e.key === 'Enter') {
YJ's avatar
YJ committed
      this.handleSubmit();
YJ's avatar
YJ committed
    }
  };

  handlePasswordChange = ({ target: { value } }) => {
    this.setState({ password: value });
  };

  handleSubmit = event => {
    const { createAccountStore, history } = this.props;
    const { password } = this.state;
    event.preventDefault();

    // Save to parity
    createAccountStore
      .saveAccountToParity(password)
      .then(res => {
        createAccountStore.clear();
        history.push('/accounts');
      })
      .catch(err => {
        console.error(err);

        this.setState({
          isLoading: false,
          error: err.text
        });
      });
  };

  goBack = () => {
    const { createAccountStore, history } = this.props;

    createAccountStore.clear();
    history.goBack();
Amaury Martiny's avatar
Amaury Martiny committed
  render () {
      createAccountStore: { address, name, isJSON },
      location: { pathname }
    } = this.props;
    const { confirm, error, isLoading, password } = this.state;
    const currentStep = pathname.slice(-1);

    return (
      <AccountCard
        address={address}
        name={name}
        drawers={[
          <form key='createAccount' onSubmit={this.handleSubmit}>
            <div className='text'>
              <p>
                {' '}
                {isJSON
                  ? 'Unlock your account: '
                  : 'Secure your account with a password:'}
              </p>
            <FetherForm.Field
              label='Password'
              onChange={this.handlePasswordChange}
              required
              type='password'
              value={password}
            />
            {!isJSON ? (
              <FetherForm.Field
                label='Confirm'
                onChange={this.handleConfirmChange}
                onKeyPress={this.handleKeyPress}
                required
                type='password'
                value={confirm}
              />
            ) : null}

            <p>
              {error
                ? error + ' Please check your password and try again.'
                : null}{' '}
            </p>
            <nav className='form-nav -space-around'>
              {currentStep > 1 && (
                <button className='button -cancel' onClick={this.goBack}>
              <button
                className='button'
                disabled={
                  !password || (!isJSON && confirm !== password) || isLoading
                }
              >
                Confirm account creation
              </button>
            </nav>
          </form>
        ]}
      />
export default AccountPassword;