feat(max): Added 'Max' button to Send Card

This commit is contained in:
Daniel Gonzalez
2020-11-18 20:25:59 -04:00
parent da025c49a4
commit 8bc29d9c35
+124 -9
View File
@@ -22,7 +22,8 @@ class Send extends React.Component {
txId: '', txId: '',
showScan: false, showScan: false,
inFetch: false, inFetch: false,
sendCurrency: 'USD' sendCurrency: 'USD',
sendMax: false
} }
_this.BchWallet = BchWallet _this.BchWallet = BchWallet
} }
@@ -74,31 +75,38 @@ class Send extends React.Component {
labelPosition='above' labelPosition='above'
onChange={_this.handleUpdate} onChange={_this.handleUpdate}
addonRight={_this.state.sendCurrency} addonRight={_this.state.sendCurrency}
disabled={_this.state.sendMax}
buttonRight={ buttonRight={
<Button <Button
icon='fa-random' icon='fa-random'
onClick={_this.handleChangeCurrency} onClick={_this.handleChangeCurrency}
/> />
} }
buttonLeft={
<Button
text={_this.state.sendMax ? 'UNDO' : 'MAX'}
onClick={_this.handleSendType}
/>
}
/> />
<div className='text-left pb-4'> <div className='text-left pb-4'>
<p> <p>
{_this.state.sendCurrency === 'BCH' {_this.state.sendCurrency === 'BCH'
? `USD: ${( ? `USD: ${(
_this.state.amountSat * _this.state.amountSat *
(_this.props.currentRate / 100) (_this.props.currentRate / 100)
).toFixed(2)}` ).toFixed(2)}`
: `BCH: ${( : `BCH: ${(
_this.state.amountSat / _this.state.amountSat /
(_this.props.currentRate / 100) (_this.props.currentRate / 100)
).toFixed(8)}`} ).toFixed(8)}`}
</p> </p>
</div> </div>
<Button <Button
text='Send' text='Send'
type='primary' type='primary'
className='btn-lg' className='btn-lg'
onClick={_this.handleSend} onClick={_this.state.sendMax ? _this.handleSendAll : _this.handleSend}
/> />
</Box> </Box>
</Col> </Col>
@@ -159,6 +167,112 @@ class Send extends React.Component {
} }
} }
handleSendType () {
const sendMax = !_this.state.sendMax
_this.setState({
sendMax
})
if (sendMax) {
_this.getMaxAmount()
} else {
_this.setState({
amountSat: ''
})
}
}
async getMaxAmount () {
try {
const bchWalletLib = _this.props.bchWallet
// Ensure the wallet UTXOs are up-to-date.
const walletAddr = bchWalletLib.walletInfo.address
await bchWalletLib.utxos.initUtxoStore(walletAddr)
const utxos = bchWalletLib.utxos.bchUtxos
if (!utxos.length) {
throw new Error('No BCH Utxos to spend!')
}
// Get total of satoshis fron the bch utxos
let totalAmount = 0
utxos.map(val => {
totalAmount += val.satoshis
})
// Convert satoshis to bch
let amountSat = totalAmount / 100000000
// Change the amount to send to USD if is the selected currency
if (_this.state.sendCurrency === 'USD') {
const _usdAmount = amountSat * (_this.props.currentRate / 100)
const usdAmount = Number(_usdAmount.toFixed(2)) // usd Amount
amountSat = usdAmount
}
_this.setState({
amountSat: amountSat
})
} catch (error) {
console.error(error)
_this.setState({
errMsg: error.message,
sendMax: false
})
}
}
async handleSendAll () {
try {
_this.validateInputs()
const bchWalletLib = _this.props.bchWallet
let { address, amountSat } = _this.state
if (_this.state.sendCurrency === 'USD') {
amountSat = (amountSat / (_this.props.currentRate / 100)).toFixed(8)
}
const amountToSend = Math.floor(Number(amountSat) * 100000000)
console.log(`Sending ${amountToSend} satoshis to ${address}`)
if (!bchWalletLib) {
throw new Error('Wallet not found')
}
_this.setState({
inFetch: true
})
// Ensure the wallet UTXOs are up-to-date.
const walletAddr = bchWalletLib.walletInfo.address
await bchWalletLib.utxos.initUtxoStore(walletAddr)
// Send the BCH.
const result = await bchWalletLib.sendAll(address)
// console.log('result',result)
_this.setState({
txId: result
})
// update balance
setTimeout(async () => {
const myBalance = await bchWalletLib.getBalance()
const bchjs = bchWalletLib.bchjs
// TODO: Get the current rate of the selected chain (bchn or bcha)
const currentRate = await bchjs.Price.getUsd() * 100
_this.props.updateBalance({ myBalance, currentRate })
}, 1000)
_this.resetValues()
} catch (error) {
_this.handleError(error)
}
}
handleUpdate (event) { handleUpdate (event) {
const value = event.target.value const value = event.target.value
_this.setState({ _this.setState({
@@ -237,7 +351,8 @@ class Send extends React.Component {
address: '', address: '',
amountSat: '', amountSat: '',
errMsg: '', errMsg: '',
inFetch: false inFetch: false,
sendMax: ''
}) })
const amountEle = document.getElementById('amountToSend') const amountEle = document.getElementById('amountToSend')
amountEle.value = '' amountEle.value = ''