1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00

Enables remove token and ensures add/remove update the list without need for refresh.

This commit is contained in:
Dan 2017-10-03 14:55:52 -02:30 committed by Chi Kei Chan
parent 45dbd017e6
commit ac4868170f
7 changed files with 72 additions and 14 deletions

View File

@ -42,7 +42,18 @@ class PreferencesController {
} }
this.store.updateState({ tokens }) this.store.updateState({ tokens })
return Promise.resolve() return Promise.resolve(tokens)
}
removeToken (rawAddress) {
const address = normalizeAddress(rawAddress)
const tokens = this.store.getState().tokens
const updatedTokens = tokens.filter(token => token.address !== rawAddress)
this.store.updateState({ tokens: updatedTokens })
return Promise.resolve(updatedTokens)
} }
getTokens () { getTokens () {

View File

@ -327,6 +327,7 @@ module.exports = class MetamaskController extends EventEmitter {
// PreferencesController // PreferencesController
setSelectedAddress: nodeify(preferencesController.setSelectedAddress, preferencesController), setSelectedAddress: nodeify(preferencesController.setSelectedAddress, preferencesController),
addToken: nodeify(preferencesController.addToken, preferencesController), addToken: nodeify(preferencesController.addToken, preferencesController),
removeToken: nodeify(preferencesController.removeToken, preferencesController),
setCurrentAccountTab: nodeify(preferencesController.setCurrentAccountTab, preferencesController), setCurrentAccountTab: nodeify(preferencesController.setCurrentAccountTab, preferencesController),
setDefaultRpc: nodeify(this.setDefaultRpc, this), setDefaultRpc: nodeify(this.setDefaultRpc, this),
setCustomRpc: nodeify(this.setCustomRpc, this), setCustomRpc: nodeify(this.setCustomRpc, this),

View File

@ -152,6 +152,9 @@ var actions = {
showAddTokenPage, showAddTokenPage,
addToken, addToken,
addTokens, addTokens,
removeToken,
updateTokens,
UPDATE_TOKENS: 'UPDATE_TOKENS',
setRpcTarget: setRpcTarget, setRpcTarget: setRpcTarget,
setDefaultRpcTarget: setDefaultRpcTarget, setDefaultRpcTarget: setDefaultRpcTarget,
setProviderType: setProviderType, setProviderType: setProviderType,
@ -753,16 +756,31 @@ function addToken (address, symbol, decimals) {
return (dispatch) => { return (dispatch) => {
dispatch(actions.showLoadingIndication()) dispatch(actions.showLoadingIndication())
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
background.addToken(address, symbol, decimals, (err) => { background.addToken(address, symbol, decimals, (err, tokens) => {
dispatch(actions.hideLoadingIndication()) dispatch(actions.hideLoadingIndication())
if (err) { if (err) {
dispatch(actions.displayWarning(err.message)) dispatch(actions.displayWarning(err.message))
reject(err) reject(err)
} }
resolve() dispatch(actions.updateTokens(tokens))
// setTimeout(() => { resolve(tokens)
// dispatch(actions.goHome()) })
// }, 250) })
}
}
function removeToken (address) {
return (dispatch) => {
dispatch(actions.showLoadingIndication())
return new Promise((resolve, reject) => {
background.removeToken(address, (err, tokens) => {
dispatch(actions.hideLoadingIndication())
if (err) {
dispatch(actions.displayWarning(err.message))
reject(err)
}
dispatch(actions.updateTokens(tokens))
resolve(tokens)
}) })
}) })
} }
@ -786,6 +804,13 @@ function addTokens (tokens) {
} }
} }
function updateTokens(newTokens) {
return {
type: actions.UPDATE_TOKENS,
newTokens
}
}
function goBackToInitView () { function goBackToInitView () {
return { return {
type: actions.BACK_TO_INIT_MENU, type: actions.BACK_TO_INIT_MENU,

View File

@ -41,6 +41,7 @@ TokenMenuDropdown.prototype.render = function () {
onClick: (e) => { onClick: (e) => {
e.stopPropagation() e.stopPropagation()
showHideTokenConfirmationModal(this.props.token) showHideTokenConfirmationModal(this.props.token)
this.props.onClose()
}, },
}, 'Hide Token') }, 'Hide Token')

View File

@ -13,7 +13,15 @@ function mapStateToProps (state) {
} }
function mapDispatchToProps (dispatch) { function mapDispatchToProps (dispatch) {
return {} return {
hideModal: () => dispatch(actions.hideModal()),
hideToken: address => {
dispatch(actions.removeToken(address))
.then(() => {
dispatch(actions.hideModal())
})
},
}
} }
inherits(HideTokenConfirmationModal, Component) inherits(HideTokenConfirmationModal, Component)
@ -26,7 +34,7 @@ function HideTokenConfirmationModal () {
module.exports = connect(mapStateToProps, mapDispatchToProps)(HideTokenConfirmationModal) module.exports = connect(mapStateToProps, mapDispatchToProps)(HideTokenConfirmationModal)
HideTokenConfirmationModal.prototype.render = function () { HideTokenConfirmationModal.prototype.render = function () {
const { token, network } = this.props const { token, network, hideToken, hideModal } = this.props
const { symbol, address } = token const { symbol, address } = token
return h('div.hide-token-confirmation', {}, [ return h('div.hide-token-confirmation', {}, [
@ -51,12 +59,12 @@ HideTokenConfirmationModal.prototype.render = function () {
h('div.hide-token-confirmation__buttons', {}, [ h('div.hide-token-confirmation__buttons', {}, [
h('button.btn-clear', { h('button.btn-clear', {
onClick: () => {}, onClick: () => hideModal(),
}, [ }, [
'CANCEL', 'CANCEL',
]), ]),
h('button.btn-clear', { h('button.btn-clear', {
onClick: () => {}, onClick: () => hideToken(address),
}, [ }, [
'HIDE', 'HIDE',
]), ]),

View File

@ -27,7 +27,6 @@ for (const address in contracts) {
module.exports = connect(mapStateToProps)(TokenList) module.exports = connect(mapStateToProps)(TokenList)
inherits(TokenList, Component) inherits(TokenList, Component)
function TokenList () { function TokenList () {
this.state = { this.state = {
@ -129,15 +128,22 @@ TokenList.prototype.componentDidUpdate = function (nextProps) {
const { const {
network: oldNet, network: oldNet,
userAddress: oldAddress, userAddress: oldAddress,
tokens,
} = this.props } = this.props
const { const {
network: newNet, network: newNet,
userAddress: newAddress, userAddress: newAddress,
tokens: newTokens,
} = nextProps } = nextProps
if (newNet === 'loading') return const isLoading = newNet === 'loading'
if (!oldNet || !newNet || !oldAddress || !newAddress) return const missingInfo = !oldNet || !newNet || !oldAddress || !newAddress
if (oldAddress === newAddress && oldNet === newNet) return const sameUserAndNetwork = oldAddress === newAddress && oldNet === newNet
const shouldUpdateTokens = isLoading || missingInfo || sameUserAndNetwork
const tokensLengthUnchanged = tokens.length === newTokens.length
if (tokensLengthUnchanged && shouldUpdateTokens) return
this.setState({ isLoading: true }) this.setState({ isLoading: true })
this.createFreshTokenTracker() this.createFreshTokenTracker()

View File

@ -19,6 +19,7 @@ function reduceMetamask (state, action) {
addressBook: [], addressBook: [],
selectedTokenAddress: null, selectedTokenAddress: null,
tokenExchangeRates: {}, tokenExchangeRates: {},
tokens: [],
}, state.metamask) }, state.metamask)
switch (action.type) { switch (action.type) {
@ -146,6 +147,11 @@ function reduceMetamask (state, action) {
}, },
}) })
case actions.UPDATE_TOKENS:
return extend(metamaskState, {
tokens: action.newTokens,
})
default: default:
return metamaskState return metamaskState