add subtract from global balance on register event

This commit is contained in:
Barry Gitarts 2018-06-13 15:04:06 -04:00
parent b9113b9f20
commit c5bf75d171
3 changed files with 34 additions and 5 deletions

View File

@ -1,4 +1,6 @@
import React, { Fragment, PureComponent } from 'react';
import { connect } from 'react-redux';
import { actions as accountActions } from '../../reducers/accounts';
import ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry';
import { Button, Field, TextInput, Card, Info, Text } from '../../ui/components'
import { IconCheck } from '../../ui/icons'
@ -72,6 +74,13 @@ class Register extends PureComponent {
.then((res) => { this.setState({ domainPrice: res })});
}
onRegistered = (address, statusAccount) => {
const { domainPrice } = this.state;
const { subtractFromBalance } = this.props;
subtractFromBalance(domainPrice);
this.setState({ registered: { address, statusAccount } });
}
render() {
const { domainName, setStatus } = this.props;
const { domainPrice, registered } = this.state;
@ -88,7 +97,7 @@ class Register extends PureComponent {
subDomain={formattedDomainArray[0]}
domainName={formattedDomainArray.slice(1).join('.')}
domainPrice={domainPrice}
registeredCallbackFn={(address, statusAccount) => this.setState({ registered: { address, statusAccount } })} />
registeredCallbackFn={this.onRegistered} />
</Fragment> :
<RenderAddresses {...this.props} address={registered.address} statusAccount={registered.statusAccount} />}
<div style={backButton} onClick={() => setStatus(null)}>&larr;</div>
@ -97,6 +106,14 @@ class Register extends PureComponent {
}
}
const mapDispatchToProps = dispatch => ({
subtractFromBalance(amount) {
dispatch(accountActions.subtractfromSntTokenBalance(amount));
},
});
const ConnectedRegister = connect(null, mapDispatchToProps)(Register);
const DisplayAddress = (props) => (
<Fragment>
{validAddress(props.address) ?
@ -141,7 +158,7 @@ const InnerForm = ({
address={status.address}
statusAccount={status.statusAccount}
setStatus={setStatus} /> :
<Register
<ConnectedRegister
setStatus={setStatus}
domainName={values.domainName} />
}

View File

@ -55,7 +55,6 @@ class TokenHandle extends PureComponent {
render() {
const { symbol, account, isLoading } = this.props;
const { approved } = this.state;
console.log('account:', this.props)
return (
<Fragment>
{!isLoading && !!account && <div style={{ display: 'flex', justifyContent: 'center', margin: '10px', paddingLeft: '60px' }}>

View File

@ -4,12 +4,14 @@ import { createSelector } from 'reselect'
export const types = createTypes([
'RECEIVE_ACCOUNTS',
'UPDATE_DEFAULT_ACCOUNT',
'ADD_TO_SNT_TOKEN_BALANCE'
'ADD_TO_SNT_TOKEN_BALANCE',
'SUBTRACT_FROM_SNT_TOKEN_BALANCE'
], 'ACCOUNTS')
export const actions = {
receiveAccounts: actionCreator(types.RECEIVE_ACCOUNTS, 'defaultAccount','accounts'),
updateDefaultAccount: actionCreator(types.UPDATE_DEFAULT_ACCOUNT, 'defaultAccount'),
addToSntTokenBalance: actionCreator(types.ADD_TO_SNT_TOKEN_BALANCE, 'amount')
addToSntTokenBalance: actionCreator(types.ADD_TO_SNT_TOKEN_BALANCE, 'amount'),
subtractfromSntTokenBalance: actionCreator(types.SUBTRACT_FROM_SNT_TOKEN_BALANCE, 'amount')
}
export default function(state = { loading: true, accounts: [] }, action) {
@ -38,6 +40,17 @@ export default function(state = { loading: true, accounts: [] }, action) {
accounts
}
}
case types.SUBTRACT_FROM_SNT_TOKEN_BALANCE: {
const currentAccount = { ...getCurrentAccount({accounts: state}) }
currentAccount.SNTTokenBalance = Number(currentAccount.SNTTokenBalance) - Number(action.payload.amount)
const accounts = [ ...state.accounts ]
const idx = accounts.findIndex(a => a.address === currentAccount.address)
accounts[idx] = currentAccount
return {
...state,
accounts
}
}
default:
return state;
}