Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

fix: Relates to #138. Prevents user from importing account that is already in account list #327

Merged
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
// SPDX-License-Identifier: BSD-3-Clause

import React, { Component } from 'react';
import { Card, Form as FetherForm } from 'fether-ui';
import { addressShort, Card, Form as FetherForm } from 'fether-ui';
import { accounts$, withoutLoading } from '@parity/light.js';
import light from '@parity/light.js-react';
import { inject, observer } from 'mobx-react';

@light({
accounts: () => accounts$().pipe(withoutLoading())
})
@inject('createAccountStore')
@observer
class AccountImportOptions extends Component {
Expand All @@ -32,30 +37,45 @@ class AccountImportOptions extends Component {
handleSubmitPhrase = async () => {
const phrase = this.state.phrase.trim();
const {
createAccountStore,
createAccountStore: { setPhrase }
} = this.props;

this.setState({ isLoading: true, phrase });

try {
await setPhrase(phrase);

if (this.hasExistingAddressForImport(createAccountStore.address)) {
return;
}

this.handleNextStep();
} catch (e) {
} catch (error) {
this.setState({
isLoading: false,
error:
'The passphrase was not recognized. Please verify that you entered your passphrase correctly.'
});
console.error(error);
}
};

handleChangeFile = async jsonString => {
const {
createAccountStore,
createAccountStore: { setJsonString }
} = this.props;

this.setState({ isLoading: true });

try {
await setJsonString(jsonString);

if (this.hasExistingAddressForImport(createAccountStore.address)) {
return;
}

this.handleNextStep();
} catch (error) {
this.setState({
Expand All @@ -67,6 +87,22 @@ class AccountImportOptions extends Component {
}
};

hasExistingAddressForImport = addressForImport => {
const { accounts } = this.props;
const isExistingAddress = accounts
.map(address => address && address.toLowerCase())
.includes(addressForImport.toLowerCase());

if (isExistingAddress) {
this.setState({
isLoading: false,
error: `Account ${addressShort(addressForImport)} already listed`
});
}

return isExistingAddress;
};

render () {
const {
history,
Expand Down
8 changes: 3 additions & 5 deletions packages/fether-ui/src/AddressShort/AddressShort.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
import React from 'react';
import PropTypes from 'prop-types';

import { addressShort } from '../utils/addressShort';

export const AddressShort = ({ address, as: T = 'span', ...otherProps }) => (
<T {...otherProps}>
{address.slice(0, 6)}
..
{address.slice(-4)}
</T>
<T {...otherProps}>{addressShort(address)}</T>
);

AddressShort.propTypes = {
Expand Down
1 change: 1 addition & 0 deletions packages/fether-ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './Form';
export * from './Header';
export * from './Placeholder';
export * from './TokenCard';
export * from './utils';
7 changes: 7 additions & 0 deletions packages/fether-ui/src/utils/addressShort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause

export const addressShort = address =>
address ? `${address.slice(0, 6)}..${address.slice(-4)}` : '';
6 changes: 6 additions & 0 deletions packages/fether-ui/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause

export * from './addressShort';