BackupAccount.js 3.1 KiB
Newer Older
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause

import React, { Component } from 'react';
import { AccountHeader, Card, Form as FetherForm } from 'fether-ui';
import { observer } from 'mobx-react';
import { Link, withRouter } from 'react-router-dom';
Axel Chalon's avatar
Axel Chalon committed
import backupAccount from '../utils/backupAccount';
import withAccount from '../utils/withAccount';

@withRouter
@withAccount
@observer
class BackupAccount extends Component {
  state = {
    isLoading: false,
    password: '',
    message: ''
  };

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

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

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

    this.setState({ isLoading: true });

    backupAccount(address, password)
      .then(res => {
YJ's avatar
YJ committed
        /*
          FIXME: this timeout is a placeholder for after the backup file is saved.
          AFAICT there is no callback from FileSaver.saveAs() so I'm not sure how to handle this yet.
          If it just goes straight to the accounts page it's not clear anything happened at all, and
          a little loading time however arbitrary at least gives the sense that something's happened.
        */
        setTimeout(() => history.push(`/accounts`), 3000);
      })
      .catch(err => {
        this.setState({
          message: err.text + ' Please check your password and try again.'
        });
YJ's avatar
YJ committed
        this.setState({ isLoading: false });
      });
  };

  render () {
    const {
      account: { name, address, type },
      history
    } = this.props;
    const { isLoading, message, password } = this.state;

    return (
      <div>
        <AccountHeader
          address={address}
          copyAddress
          name={name}
          type={type}
          left={
            <Link to='/accounts' className='icon -back'>
              Back
            </Link>
          }
        />
        <br />
        <Card className='-space-around'>
          <form key='backupAccount' onSubmit={this.handleSubmit}>
            <div className='text'>
YJ's avatar
YJ committed
              <p>Unlock your account to encrypt the JSON keystore file:</p>
            </div>

            <FetherForm.Field
              label='Password'
              onChange={this.handlePasswordChange}
YJ's avatar
YJ committed
              autoFocus
              required
              type='password'
              value={password}
            />

            <p className='error'> {message} </p>

            <nav className='form-nav -space-around'>
YJ's avatar
YJ committed
              <button
Luke Schoen's avatar
Luke Schoen committed
                className='button -back'
YJ's avatar
YJ committed
                onClick={history.goBack}
                type='button'
              >
                Back
              </button>
YJ's avatar
YJ committed
              <button
                className='button'
                disabled={!password || isLoading}
                autoFocus
              >
                Confirm backup
              </button>
            </nav>
          </form>
        </Card>
      </div>
    );
  }
}

export default BackupAccount;