Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display correct balance from state when creating an account that already holds funds #1759

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions app/components/UI/AccountList/AccountElement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { renderFromWei } from '../../../../util/number';
import { getTicker } from '../../../../util/transactions';
import { strings } from '../../../../../locales/i18n';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import { connect } from 'react-redux';

const EMPTY = '0x0';
const BALANCE_KEY = 'balance';

const styles = StyleSheet.create({
account: {
Expand Down Expand Up @@ -76,7 +80,7 @@ const styles = StyleSheet.create({
/**
* View that renders specific account element in AccountList
*/
export default class AccountElement extends PureComponent {
class AccountElement extends PureComponent {
static propTypes = {
/**
* Callback to be called onPress
Expand All @@ -94,7 +98,11 @@ export default class AccountElement extends PureComponent {
* Whether the account element should be disabled (opaque and not clickable)
*/
disabled: PropTypes.bool,
item: PropTypes.object
item: PropTypes.object,
/**
* Updated balance using stored in state
*/
updatedBalanceFromStore: PropTypes.string
};

onPress = () => {
Expand All @@ -110,8 +118,8 @@ export default class AccountElement extends PureComponent {
};

render() {
const { disabled } = this.props;
const { address, balance, ticker, name, isSelected, isImported, balanceError } = this.props.item;
const { disabled, updatedBalanceFromStore } = this.props;
const { address, ticker, name, isSelected, isImported, balanceError } = this.props.item;
const selected = isSelected ? <Icon name="check-circle" size={30} color={colors.blue} /> : null;
const imported = isImported ? (
<View style={styles.importedWrapper}>
Expand All @@ -120,6 +128,7 @@ export default class AccountElement extends PureComponent {
</Text>
</View>
) : null;

return (
<TouchableOpacity
style={[styles.account, disabled ? styles.disabledAccount : null]}
Expand All @@ -136,7 +145,7 @@ export default class AccountElement extends PureComponent {
</Text>
<View style={styles.accountBalanceWrapper}>
<Text style={styles.accountBalance}>
{renderFromWei(balance)} {getTicker(ticker)}
{renderFromWei(updatedBalanceFromStore)} {getTicker(ticker)}
</Text>
{!!balanceError && (
<Text style={[styles.accountBalance, styles.accountBalanceError]}>{balanceError}</Text>
Expand All @@ -150,3 +159,27 @@ export default class AccountElement extends PureComponent {
);
}
}

const mapStateToProps = (
{
engine: {
backgroundState: { PreferencesController, AccountTrackerController }
}
},
{ item: { balance, address } }
) => {
const { selectedAddress } = PreferencesController;
const { accounts } = AccountTrackerController;
const selectedAccount = accounts[selectedAddress];
const selectedAccountHasBalance =
selectedAccount && Object.prototype.hasOwnProperty.call(selectedAccount, BALANCE_KEY);
Copy link
Member Author

@rickycodes rickycodes Aug 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normally i would just use obj.hasOwnProperty() directly here, but I guess we don't allow that with this rule: https://eslint.org/docs/rules/no-prototype-builtins

const updatedBalanceFromStore =
balance === EMPTY && selectedAddress === address && selectedAccount && selectedAccountHasBalance
? selectedAccount[BALANCE_KEY]
: balance;
return {
updatedBalanceFromStore
};
};

export default connect(mapStateToProps)(AccountElement);