add subtract from global balance on register event
This commit is contained in:
parent
b9113b9f20
commit
c5bf75d171
|
@ -1,4 +1,6 @@
|
||||||
import React, { Fragment, PureComponent } from 'react';
|
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 ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry';
|
||||||
import { Button, Field, TextInput, Card, Info, Text } from '../../ui/components'
|
import { Button, Field, TextInput, Card, Info, Text } from '../../ui/components'
|
||||||
import { IconCheck } from '../../ui/icons'
|
import { IconCheck } from '../../ui/icons'
|
||||||
|
@ -72,6 +74,13 @@ class Register extends PureComponent {
|
||||||
.then((res) => { this.setState({ domainPrice: res })});
|
.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() {
|
render() {
|
||||||
const { domainName, setStatus } = this.props;
|
const { domainName, setStatus } = this.props;
|
||||||
const { domainPrice, registered } = this.state;
|
const { domainPrice, registered } = this.state;
|
||||||
|
@ -88,7 +97,7 @@ class Register extends PureComponent {
|
||||||
subDomain={formattedDomainArray[0]}
|
subDomain={formattedDomainArray[0]}
|
||||||
domainName={formattedDomainArray.slice(1).join('.')}
|
domainName={formattedDomainArray.slice(1).join('.')}
|
||||||
domainPrice={domainPrice}
|
domainPrice={domainPrice}
|
||||||
registeredCallbackFn={(address, statusAccount) => this.setState({ registered: { address, statusAccount } })} />
|
registeredCallbackFn={this.onRegistered} />
|
||||||
</Fragment> :
|
</Fragment> :
|
||||||
<RenderAddresses {...this.props} address={registered.address} statusAccount={registered.statusAccount} />}
|
<RenderAddresses {...this.props} address={registered.address} statusAccount={registered.statusAccount} />}
|
||||||
<div style={backButton} onClick={() => setStatus(null)}>←</div>
|
<div style={backButton} onClick={() => setStatus(null)}>←</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) => (
|
const DisplayAddress = (props) => (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
{validAddress(props.address) ?
|
{validAddress(props.address) ?
|
||||||
|
@ -141,7 +158,7 @@ const InnerForm = ({
|
||||||
address={status.address}
|
address={status.address}
|
||||||
statusAccount={status.statusAccount}
|
statusAccount={status.statusAccount}
|
||||||
setStatus={setStatus} /> :
|
setStatus={setStatus} /> :
|
||||||
<Register
|
<ConnectedRegister
|
||||||
setStatus={setStatus}
|
setStatus={setStatus}
|
||||||
domainName={values.domainName} />
|
domainName={values.domainName} />
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,6 @@ class TokenHandle extends PureComponent {
|
||||||
render() {
|
render() {
|
||||||
const { symbol, account, isLoading } = this.props;
|
const { symbol, account, isLoading } = this.props;
|
||||||
const { approved } = this.state;
|
const { approved } = this.state;
|
||||||
console.log('account:', this.props)
|
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
{!isLoading && !!account && <div style={{ display: 'flex', justifyContent: 'center', margin: '10px', paddingLeft: '60px' }}>
|
{!isLoading && !!account && <div style={{ display: 'flex', justifyContent: 'center', margin: '10px', paddingLeft: '60px' }}>
|
||||||
|
|
|
@ -4,12 +4,14 @@ import { createSelector } from 'reselect'
|
||||||
export const types = createTypes([
|
export const types = createTypes([
|
||||||
'RECEIVE_ACCOUNTS',
|
'RECEIVE_ACCOUNTS',
|
||||||
'UPDATE_DEFAULT_ACCOUNT',
|
'UPDATE_DEFAULT_ACCOUNT',
|
||||||
'ADD_TO_SNT_TOKEN_BALANCE'
|
'ADD_TO_SNT_TOKEN_BALANCE',
|
||||||
|
'SUBTRACT_FROM_SNT_TOKEN_BALANCE'
|
||||||
], 'ACCOUNTS')
|
], 'ACCOUNTS')
|
||||||
export const actions = {
|
export const actions = {
|
||||||
receiveAccounts: actionCreator(types.RECEIVE_ACCOUNTS, 'defaultAccount','accounts'),
|
receiveAccounts: actionCreator(types.RECEIVE_ACCOUNTS, 'defaultAccount','accounts'),
|
||||||
updateDefaultAccount: actionCreator(types.UPDATE_DEFAULT_ACCOUNT, 'defaultAccount'),
|
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) {
|
export default function(state = { loading: true, accounts: [] }, action) {
|
||||||
|
@ -38,6 +40,17 @@ export default function(state = { loading: true, accounts: [] }, action) {
|
||||||
accounts
|
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:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue