1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 11:46:13 +02:00
metamask-extension/ui/app/pages/send/send.container.js
Mark Stacey ddaa492751
Use send state for send flow token (#8695)
The chosen token in the `send` flow was set from one of two places:
`metamask.selectedTokenAddress` or `metamask.send.token`. The former is
used most of the time, but the latter is used for the 'Edit' button
shown in the upper-left of the confirmation UI.

The send flow will now exclusively use `metamask.send.token` for the
token state during the send flow. `metamask.selectedTokenAddress` is
now only used for the selected token state on the Home screen. This
simplifies the Redux state, as the send token is now in one place
instead of two, and `metamask.selectedTokenAddress` has only one
purpose.
2020-05-29 14:46:10 -03:00

127 lines
3.6 KiB
JavaScript

import { connect } from 'react-redux'
import SendEther from './send.component'
import { withRouter } from 'react-router-dom'
import { compose } from 'redux'
import {
getBlockGasLimit,
getConversionRate,
getCurrentNetwork,
getGasLimit,
getGasPrice,
getGasTotal,
getPrimaryCurrency,
getSendToken,
getSendTokenContract,
getSendAmount,
getSendEditingTransactionId,
getSendHexDataFeatureFlagState,
getSendFromObject,
getSendTo,
getSendToNickname,
getTokenBalance,
getQrCodeData,
getSelectedAddress,
getAddressBook,
} from '../../selectors'
import {
updateSendTo,
updateSendTokenBalance,
updateGasData,
setGasTotal,
showQrScanner,
qrCodeDetected,
updateSendEnsResolution,
updateSendEnsResolutionError,
} from '../../store/actions'
import {
resetSendState,
updateSendErrors,
} from '../../ducks/send/send.duck'
import {
fetchBasicGasEstimates,
} from '../../ducks/gas/gas.duck'
import { getTokens } from '../../ducks/metamask/metamask'
import {
calcGasTotal,
} from './send.utils.js'
import {
isValidDomainName,
} from '../../helpers/utils/util'
function mapStateToProps (state) {
const editingTransactionId = getSendEditingTransactionId(state)
return {
addressBook: getAddressBook(state),
amount: getSendAmount(state),
blockGasLimit: getBlockGasLimit(state),
conversionRate: getConversionRate(state),
editingTransactionId,
from: getSendFromObject(state),
gasLimit: getGasLimit(state),
gasPrice: getGasPrice(state),
gasTotal: getGasTotal(state),
network: getCurrentNetwork(state),
primaryCurrency: getPrimaryCurrency(state),
qrCodeData: getQrCodeData(state),
selectedAddress: getSelectedAddress(state),
sendToken: getSendToken(state),
showHexData: getSendHexDataFeatureFlagState(state),
to: getSendTo(state),
toNickname: getSendToNickname(state),
tokens: getTokens(state),
tokenBalance: getTokenBalance(state),
tokenContract: getSendTokenContract(state),
}
}
function mapDispatchToProps (dispatch) {
return {
updateAndSetGasLimit: ({
blockGasLimit,
editingTransactionId,
gasLimit,
gasPrice,
selectedAddress,
sendToken,
to,
value,
data,
}) => {
!editingTransactionId
? dispatch(updateGasData({ gasPrice, selectedAddress, sendToken, blockGasLimit, to, value, data }))
: dispatch(setGasTotal(calcGasTotal(gasLimit, gasPrice)))
},
updateSendTokenBalance: ({ sendToken, tokenContract, address }) => {
dispatch(updateSendTokenBalance({
sendToken,
tokenContract,
address,
}))
},
updateSendErrors: (newError) => dispatch(updateSendErrors(newError)),
resetSendState: () => dispatch(resetSendState()),
scanQrCode: () => dispatch(showQrScanner()),
qrCodeDetected: (data) => dispatch(qrCodeDetected(data)),
updateSendTo: (to, nickname) => dispatch(updateSendTo(to, nickname)),
fetchBasicGasEstimates: () => dispatch(fetchBasicGasEstimates()),
updateSendEnsResolution: (ensResolution) => dispatch(updateSendEnsResolution(ensResolution)),
updateSendEnsResolutionError: (message) => dispatch(updateSendEnsResolutionError(message)),
updateToNicknameIfNecessary: (to, toNickname, addressBook) => {
if (isValidDomainName(toNickname)) {
const addressBookEntry = addressBook.find(({ address }) => to === address) || {}
if (!addressBookEntry.name !== toNickname) {
dispatch(updateSendTo(to, addressBookEntry.name || ''))
}
}
},
}
}
export default compose(
withRouter,
connect(mapStateToProps, mapDispatchToProps)
)(SendEther)