Skip to content

Commit

Permalink
preferences test fixes for tokens per account
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanmino committed Jul 25, 2018
1 parent 2770df8 commit adee549
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ class PreferencesController {
Object.defineProperty(this.store._state, 'tokens', {
get: () => {
const selectedAddress = this.store.getState().selectedAddress
const tokens = this.store.getState().addressTokens
// TODO when create vault
if (!(selectedAddress in tokens)) return []
return tokens[selectedAddress]
const addressTokens = this.store.getState().addressTokens
if (!(selectedAddress in addressTokens)) return []
return addressTokens[selectedAddress]
},
})
}
Expand Down Expand Up @@ -88,12 +87,16 @@ class PreferencesController {
*/
setAddresses (addresses) {
const oldIdentities = this.store.getState().identities
const addressTokens = this.store.getState().addressTokens
const identities = addresses.reduce((ids, address, index) => {
const oldId = oldIdentities[address] || {}
ids[address] = {name: `Account ${index + 1}`, address, ...oldId}
return ids
}, {})
this.store.updateState({ identities })
for (const address in identities) {
if (!(address in addressTokens)) addressTokens[address] = []
}
this.store.updateState({ identities, addressTokens })
}

/**
Expand All @@ -104,11 +107,13 @@ class PreferencesController {
*/
removeAddress (address) {
const identities = this.store.getState().identities
const addressTokens = this.store.getState().addressTokens
if (!identities[address]) {
throw new Error(`${address} can't be deleted cause it was not found`)
}
delete identities[address]
this.store.updateState({ identities })
delete addressTokens[address]
this.store.updateState({ identities, addressTokens })

// If the selected account is no longer valid,
// select an arbitrary other account:
Expand Down Expand Up @@ -194,8 +199,10 @@ class PreferencesController {
setSelectedAddress (_address) {
return new Promise((resolve, reject) => {
const address = normalizeAddress(_address)
const tokens = this.store.getState().addressTokens[_address]
this.store.updateState({ selectedAddress: address, tokens: tokens })
const addressTokens = this.store.getState().addressTokens
if (!(address in addressTokens)) addressTokens[address] = []
const tokens = addressTokens[address]
this.store.updateState({ selectedAddress: address, tokens })
resolve()
})
}
Expand Down Expand Up @@ -265,7 +272,7 @@ class PreferencesController {
removeToken (rawAddress) {
const addressTokens = this.store.getState().addressTokens
const selectedAddress = this.store.getState().selectedAddress

const updatedTokens = addressTokens[selectedAddress].filter(token => token.address !== rawAddress)
addressTokens[selectedAddress] = updatedTokens
this.store.updateState({ addressTokens, tokens: updatedTokens })
Expand Down

0 comments on commit adee549

Please sign in to comment.