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

import i18n, { packageNS } from '../../../i18n';
@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: `${i18n.t(
          `${packageNS}:account.password.create.error_msg_password_confirmation_no_match`
        )}`
YJ's avatar
YJ committed
      });
      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' noValidate onSubmit={this.handleSubmit}>
            <div className='text'>
              <p>
                {' '}
                {jsonString
                  ? i18n.t(
                    `${packageNS}:account.password.import.label_msg_unlock_json`
                  )
                  : i18n.t(
                    `${packageNS}:account.password.create.label_msg_password`
                  )}
              </p>
            </div>
            <FetherForm.Field
              autoFocus
              label={i18n.t(
                `${packageNS}:account.password.common.label_password`
              )}
              onChange={this.handlePasswordChange}
              required
              type='password'
              value={password}
            />

            {!jsonString && (
              <FetherForm.Field
                  `${packageNS}:account.password.common.label_password_confirm`
                required
                type='password'
            )}

            <p>
              {error &&
                error +
                  ' ' +
                  i18n.t(
                    `${packageNS}:account.password.common.error_msg_password_incorrect`
            <nav className='form-nav -space-around'>
              {currentStep > 1 && (
Amaury Martiny's avatar
Amaury Martiny committed
                <button
                  className='button -back'
                  onClick={history.goBack}
                  type='button'
              )}
              <button
                autoFocus
                className='button'
                disabled={
                  !password ||
                  (!jsonString && confirm !== password) ||
                  isLoading
                }
              >
                {i18n.t(`${packageNS}:account.password.common.button_confirm`, {
                  postfix: isImport
                    ? i18n.t(
                      `${packageNS}:account.password.common.button_confirm_opt1`
                    )
                    : i18n.t(
                      `${packageNS}:account.password.common.button_confirm_opt2`
                    )
                })}
              </button>
            </nav>
          </form>
        ]}
        i18n={i18n}
        packageNS={packageNS}
      />
export default AccountPassword;