AccountImportOptions.js 3.5 KiB
Newer Older
YJ's avatar
YJ committed
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause

YJ's avatar
YJ committed
import React, { Component } from 'react';
import { Card, Form as FetherForm } from 'fether-ui';
import { inject, observer } from 'mobx-react';

@inject('createAccountStore')
@observer
class AccountImportOptions extends Component {
  state = {
YJ's avatar
YJ committed
    isLoading: false,
    isFileValid: false,
    json: null,
    value: ''
  };

  handleNextStep = async () => {
    const {
      history,
      location: { pathname },
      createAccountStore: { isImport, isJSON, setJSON, setPhrase }
    } = this.props;
    const currentStep = pathname.slice(-1);
    const { json, value } = this.state;

    if (isJSON) {
      this.setState({ isLoading: true });
YJ's avatar
YJ committed
      await setJSON(json);
YJ's avatar
YJ committed
    }

    if (isImport && !isJSON) {
      this.setState({ isLoading: true });
      await setPhrase(value);
    }

    history.push(`/accounts/new/${+currentStep + 1}`);
  };

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

  handleChangeFile = data => {
YJ's avatar
YJ committed
    const {
      createAccountStore: { setIsJSON }
    } = this.props;

    try {
      const json = JSON.parse(data);
YJ's avatar
YJ committed

      const isFileValid =
YJ's avatar
YJ committed
        json && json.address.length === 40 && json.version === 3;
YJ's avatar
YJ committed

      if (isFileValid) {
        setIsJSON(true);
YJ's avatar
YJ committed
        this.setState({
          isFileValid: true,
YJ's avatar
YJ committed
          json: json
        });

        this.handleNextStep();
YJ's avatar
YJ committed
      } else {
        throw new Error('File is not valid');
YJ's avatar
YJ committed
      }
    } catch (error) {
      console.error(error);

      this.setState({
        isFileValid: false,
        error:
          'Invalid file. Please check this is your actual Parity backup json keyfile and try again.'
      });
YJ's avatar
YJ committed
    }
  };

  render () {
    const {
      history,
      location: { pathname }
    } = this.props;
    const { error, value } = this.state;
YJ's avatar
YJ committed
    const currentStep = pathname.slice(-1);

YJ's avatar
YJ committed
    const jsonCard = (
YJ's avatar
YJ committed
      <div key='createAccount'>
        <div className='text -centered'>
          <p> Recover from JSON Keyfile </p>

          <FetherForm.InputFile
            label='JSON Backup Keyfile'
            onChangeFile={this.handleChangeFile}
            required
          />
        </div>
      </div>
YJ's avatar
YJ committed
    );
YJ's avatar
YJ committed

YJ's avatar
YJ committed
    const phraseCard = (
YJ's avatar
YJ committed
      <div key='importBackup'>
        <div className='text -centered'>
          <p>Recover from Seed Phrase</p>

YJ's avatar
YJ committed
          <FetherForm.Field
            as='textarea'
            label='Recovery phrase'
            onChange={this.handleChange}
            required
            value={value}
          />

          {this.renderButton()}
        </div>
YJ's avatar
YJ committed
      </div>
YJ's avatar
YJ committed
    );
YJ's avatar
YJ committed

    return (
      <div className='center-md'>
        <Card> {jsonCard} </Card>
        <br />
        <Card> {phraseCard} </Card>
        <br />
YJ's avatar
YJ committed
        <p>{error}</p>
YJ's avatar
YJ committed
        <nav className='form-nav -space-around'>
          {currentStep > 1 && (
            <button className='button -cancel' onClick={history.goBack}>
              Back
            </button>
          )}
        </nav>
      </div>
    );
  }

  renderButton = () => {
    const { isFileValid, isLoading, json, value } = this.state;
    // If we are importing an existing account, the button goes to the next step
    return (
      <button
        className='button'
        disabled={
          (!json && !value.length) || (json && !isFileValid) || isLoading
        }
        onClick={this.handleNextStep}
      >
        Next
      </button>
    );
  };
}

export default AccountImportOptions;