AccountPassword.js 4.71 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';
import RequireHealthOverlay from '../../../RequireHealthOverlay';

@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 (
      <RequireHealthOverlay require='node'>
        <AccountCard
          address={address}
          name={name}
          drawers={[
            <form key='createAccount' noValidate onSubmit={this.handleSubmit}>
                    ? i18n.t(
                      `${packageNS}:account.password.import.label_msg_unlock_json`
                    )
                    : i18n.t(
                      `${packageNS}:account.password.create.label_msg_password`
                    )}
              <FetherForm.Field
                label={i18n.t(
                  `${packageNS}:account.password.common.label_password`
                )}
                required
                type='password'
                  label={i18n.t(
                    `${packageNS}:account.password.common.label_password_confirm`
                  )}
                  onChange={this.handleConfirmChange}
                  required
                  type='password'
                  value={confirm}
                />
              )}
                {error &&
                  error +
                    ' ' +
                    i18n.t(
                      `${packageNS}:account.password.common.error_msg_password_incorrect`
                    )}
              </p>

              <nav className='form-nav -space-around'>
                {currentStep > 1 && (
                  <button
                    className='button -back'
                    onClick={history.goBack}
                    type='button'
                  >
                    {i18n.t(`${packageNS}:navigation.back`)}
Amaury Martiny's avatar
Amaury Martiny committed
                <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`
                        )
                    }
                  )}
          i18n={i18n}
          packageNS={packageNS}
export default AccountPassword;