AccountPassword.js 3.39 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 });
  };

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

YJ's avatar
YJ committed
  handleSubmit = event => {
    const { createAccountStore, history } = this.props;
YJ's avatar
YJ committed
    const { confirm, password } = this.state;

    event.preventDefault();
YJ's avatar
YJ committed

    if (!createAccountStore.jsonString && confirm !== password) {
      this.setState({
        error: 'Password confirmation does not match.'
      });
      return;
    }

    // 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
        });
      });
  };

Amaury Martiny's avatar
Amaury Martiny committed
  render () {
      createAccountStore: { address, name, jsonString, isImport },
YJ's avatar
YJ committed
      history,
      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>
                {' '}
YJ's avatar
YJ committed
                  ? 'Unlock your account to decrypt your JSON keystore file: '
                  : 'Secure your account with a password:'}
              </p>
            <FetherForm.Field
YJ's avatar
YJ committed
              autoFocus
YJ's avatar
YJ committed
              label='Password'
              onChange={this.handlePasswordChange}
              required
              type='password'
              value={password}
            />
              <FetherForm.Field
                label='Confirm'
                onChange={this.handleConfirmChange}
                required
                type='password'
                value={confirm}
              />
YJ's avatar
YJ committed
            )}
YJ's avatar
YJ committed
              {error && error + ' Please check your password and try again.'}
            </p>
            <nav className='form-nav -space-around'>
              {currentStep > 1 && (
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'
                >
YJ's avatar
YJ committed
                autoFocus
YJ's avatar
YJ committed
                className='button'
                disabled={
                  !password ||
                  (!jsonString && confirm !== password) ||
                  isLoading
YJ's avatar
YJ committed
                Confirm account {isImport ? `${'import'}` : `${'creation'}`}
              </button>
            </nav>
          </form>
        ]}
      />
export default AccountPassword;