Unlock.js 4.78 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
Amaury Martiny's avatar
Amaury Martiny committed
import React, { Component } from 'react';
import { Field, Form } from 'react-final-form';
import { Form as FetherForm, Header } from 'fether-ui';
import { inject, observer } from 'mobx-react';
import { Link, Redirect } from 'react-router-dom';
import { withProps } from 'recompose';
import i18n, { packageNS } from '../../i18n';
import RequireHealthOverlay from '../../RequireHealthOverlay';
import TokenAddress from '../../Tokens/TokensList/TokenAddress';
Amaury Martiny's avatar
Amaury Martiny committed
import withAccount from '../../utils/withAccount.js';
import withTokens from '../../utils/withTokens';
Amaury Martiny's avatar
Amaury Martiny committed
@inject('sendStore')
@withAccount
Amaury Martiny's avatar
Amaury Martiny committed
@withTokens
Axel Chalon's avatar
Axel Chalon committed
@withProps(({ match: { params: { tokenAddress } }, tokens }) => ({
  token: tokens[tokenAddress]
@observer
class Unlock extends Component {
  handleAccept = values => {
    const {
      account: { address },
      history,
      sendStore,
      token
    } = this.props;
    return sendStore
      .send(values.password)
      .then(() => history.push(`/send/${token.address}/from/${address}/sent`))
      .catch(error => ({
        password:
          error.text === 'Method not found'
            ? "Please enable the 'personal' api in the --ws-apis launch flag of Parity Ethereum."
            : error.text
Amaury Martiny's avatar
Amaury Martiny committed
  render () {
      account: { address },
      sendStore: { tx },
      token
    } = this.props;

    if (!tx || !token) {
Amaury Martiny's avatar
Amaury Martiny committed
      return <Redirect to='/' />;
Amaury Martiny's avatar
Amaury Martiny committed
      <div>
        <Header
          left={
            <Link to={`/tokens/${address}`} className='icon -back'>
              {i18n.t(`${packageNS}:navigation.close`)}
Amaury Martiny's avatar
Amaury Martiny committed
            </Link>
          title={
            token && (
              <h1>
                {i18n.t(`${packageNS}:tx.header_send_prefix`, {
                  token: token.name
                })}
              </h1>
            )
          }
          <div className='window_content'>
            <div className='box -padded'>
                drawers={[
                  <div key='txForm'>
                    <FetherForm.Field
                      className='form_field_value'
                      disabled
                      defaultValue={tx.to}
                      label={i18n.t(`${packageNS}:tx.form.field.to`)}
                    <FetherForm.Field
                      className='form_field_value'
                      disabled
                      defaultValue={`${tx.amount} ${token.symbol}`}
                      label={i18n.t(`${packageNS}:tx.form.field.amount`)}

                    <FetherForm.Field
                      as='textarea'
                      className='form_field_value'
                      disabled
                      defaultValue={`${tx.data}`}
                      label={i18n.t(`${packageNS}:tx.form.field.data`)}
                    />
                  </div>,
                  <Form
                    key='signerForm'
                    onSubmit={this.handleAccept}
                    render={({ handleSubmit, pristine, submitting }) => (
                      <form noValidate onSubmit={handleSubmit}>
                        <div className='text'>
                          <p>{i18n.t(`${packageNS}:tx.form.label_unlock`)}</p>
                          label={i18n.t(`${packageNS}:tx.form.field.password`)}
                          name='password'
                          render={FetherForm.Field}
                          required
                          type='password'
                        />
                        <nav className='form-nav -binary'>
                          <button
Luke Schoen's avatar
Luke Schoen committed
                            className='button -back'
                            onClick={history.goBack}
                            type='button'
                          >
                            {i18n.t(`${packageNS}:navigation.back`)}
                          <button
                            className='button -submit'
                            disabled={pristine || submitting}
                          >
                            {i18n.t(`${packageNS}:tx.form.button_confirm_tx`)}
export default Unlock;