diff options
author | Fabio Berger <me@fabioberger.com> | 2018-01-30 18:16:13 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-01-30 18:16:13 +0800 |
commit | 86cc011212088801a778d947ae925cc0b1ddadf8 (patch) | |
tree | 9c12a4dc945d3dd23416d4ea1ccde438c9ed687e | |
parent | c0facfc28f65495a52f9405613fe4a08aff5d164 (diff) | |
download | dexon-0x-contracts-86cc011212088801a778d947ae925cc0b1ddadf8.tar dexon-0x-contracts-86cc011212088801a778d947ae925cc0b1ddadf8.tar.gz dexon-0x-contracts-86cc011212088801a778d947ae925cc0b1ddadf8.tar.bz2 dexon-0x-contracts-86cc011212088801a778d947ae925cc0b1ddadf8.tar.lz dexon-0x-contracts-86cc011212088801a778d947ae925cc0b1ddadf8.tar.xz dexon-0x-contracts-86cc011212088801a778d947ae925cc0b1ddadf8.tar.zst dexon-0x-contracts-86cc011212088801a778d947ae925cc0b1ddadf8.zip |
Wholesale replace the tokenByAddress and de-dup properly
-rw-r--r-- | packages/website/ts/blockchain.ts | 24 | ||||
-rw-r--r-- | packages/website/ts/local_storage/tracked_token_storage.ts | 12 | ||||
-rw-r--r-- | packages/website/ts/redux/dispatcher.ts | 10 | ||||
-rw-r--r-- | packages/website/ts/redux/reducer.ts | 11 |
4 files changed, 31 insertions, 26 deletions
diff --git a/packages/website/ts/blockchain.ts b/packages/website/ts/blockchain.ts index aba438e8d..d310464ed 100644 --- a/packages/website/ts/blockchain.ts +++ b/packages/website/ts/blockchain.ts @@ -512,26 +512,30 @@ export class Blockchain { const tokenRegistryTokensByAddress = await this._getTokenRegistryTokensByAddressAsync(); - let trackedTokensIfExists = trackedTokenStorage.getTrackedTokensIfExists(this._userAddress, this.networkId); + const trackedTokensByAddress = trackedTokenStorage.getTrackedTokensByAddress(this._userAddress, this.networkId); const tokenRegistryTokens = _.values(tokenRegistryTokensByAddress); - if (_.isUndefined(trackedTokensIfExists)) { - trackedTokensIfExists = _.map(configs.DEFAULT_TRACKED_TOKEN_SYMBOLS, symbol => { + if (_.isEmpty(trackedTokensByAddress)) { + _.each(configs.DEFAULT_TRACKED_TOKEN_SYMBOLS, symbol => { const token = _.find(tokenRegistryTokens, t => t.symbol === symbol); token.isTracked = true; - return token; + trackedTokensByAddress[token.address] = token; }); - _.each(trackedTokensIfExists, token => { + _.each(trackedTokensByAddress, (token: Token, address: string) => { trackedTokenStorage.addTrackedTokenToUser(this._userAddress, this.networkId, token); }); } else { // Properly set all tokenRegistry tokens `isTracked` to true if they are in the existing trackedTokens array - _.each(trackedTokensIfExists, trackedToken => { - if (!_.isUndefined(tokenRegistryTokensByAddress[trackedToken.address])) { - tokenRegistryTokensByAddress[trackedToken.address].isTracked = true; + _.each(trackedTokensByAddress, (trackedToken: Token, address: string) => { + if (!_.isUndefined(tokenRegistryTokensByAddress[address])) { + tokenRegistryTokensByAddress[address].isTracked = true; } }); } - const allTokens = _.uniq([...tokenRegistryTokens, ...trackedTokensIfExists]); + const allTokensByAddress = { + ...tokenRegistryTokensByAddress, + ...trackedTokensByAddress, + }; + const allTokens = _.values(allTokensByAddress); const mostPopularTradingPairTokens: Token[] = [ _.find(allTokens, { symbol: configs.DEFAULT_TRACKED_TOKEN_SYMBOLS[0] }), _.find(allTokens, { symbol: configs.DEFAULT_TRACKED_TOKEN_SYMBOLS[1] }), @@ -544,7 +548,7 @@ export class Blockchain { address: mostPopularTradingPairTokens[1].address, }, }; - this._dispatcher.batchDispatch(allTokens, this.networkId, this._userAddress, sideToAssetToken); + this._dispatcher.batchDispatch(allTokensByAddress, this.networkId, this._userAddress, sideToAssetToken); this._dispatcher.updateBlockchainIsLoaded(true); } diff --git a/packages/website/ts/local_storage/tracked_token_storage.ts b/packages/website/ts/local_storage/tracked_token_storage.ts index 7733e8436..f92dd6298 100644 --- a/packages/website/ts/local_storage/tracked_token_storage.ts +++ b/packages/website/ts/local_storage/tracked_token_storage.ts @@ -1,6 +1,6 @@ import * as _ from 'lodash'; import { localStorage } from 'ts/local_storage/local_storage'; -import { Token, TrackedTokensByUserAddress } from 'ts/types'; +import { Token, TokenByAddress, TrackedTokensByUserAddress } from 'ts/types'; import { configs } from 'ts/utils/configs'; const TRACKED_TOKENS_KEY = 'trackedTokens'; @@ -39,10 +39,11 @@ export const trackedTokenStorage = { const trackedTokensByUserAddress = JSON.parse(trackedTokensJSONString); return trackedTokensByUserAddress; }, - getTrackedTokensIfExists(userAddress: string, networkId: number): Token[] { + getTrackedTokensByAddress(userAddress: string, networkId: number): TokenByAddress { + const trackedTokensByAddress: TokenByAddress = {}; const trackedTokensJSONString = localStorage.getItemIfExists(TRACKED_TOKENS_KEY); if (_.isEmpty(trackedTokensJSONString)) { - return undefined; + return trackedTokensByAddress; } const trackedTokensByUserAddress = JSON.parse(trackedTokensJSONString); const trackedTokensByNetworkId = trackedTokensByUserAddress[userAddress]; @@ -50,7 +51,10 @@ export const trackedTokenStorage = { return undefined; } const trackedTokens = trackedTokensByNetworkId[networkId]; - return trackedTokens; + _.each(trackedTokens, (trackedToken: Token) => { + trackedTokensByAddress[trackedToken.address] = trackedToken; + }); + return trackedTokensByAddress; }, removeTrackedToken(userAddress: string, networkId: number, tokenAddress: string): void { const trackedTokensByUserAddress = this.getTrackedTokensByUserAddress(); diff --git a/packages/website/ts/redux/dispatcher.ts b/packages/website/ts/redux/dispatcher.ts index 9df18e54c..aae7683ff 100644 --- a/packages/website/ts/redux/dispatcher.ts +++ b/packages/website/ts/redux/dispatcher.ts @@ -12,6 +12,7 @@ import { SideToAssetToken, SignatureData, Token, + TokenByAddress, TokenStateByAddress, } from 'ts/types'; @@ -121,10 +122,15 @@ export class Dispatcher { type: ActionTypes.RemoveTokenFromTokenByAddress, }); } - public batchDispatch(tokens: Token[], networkId: number, userAddress: string, sideToAssetToken: SideToAssetToken) { + public batchDispatch( + tokenByAddress: TokenByAddress, + networkId: number, + userAddress: string, + sideToAssetToken: SideToAssetToken, + ) { this._dispatch({ data: { - tokens, + tokenByAddress, networkId, userAddress, sideToAssetToken, diff --git a/packages/website/ts/redux/reducer.ts b/packages/website/ts/redux/reducer.ts index f94da003f..4b5a9138f 100644 --- a/packages/website/ts/redux/reducer.ts +++ b/packages/website/ts/redux/reducer.ts @@ -175,21 +175,12 @@ export function reducer(state: State = INITIAL_STATE, action: Action) { } case ActionTypes.BatchDispatch: { - const tokenByAddress = state.tokenByAddress; - const tokens = action.data.tokens; - _.each(tokens, token => { - const updatedToken = { - ...tokenByAddress[token.address], - ...token, - }; - tokenByAddress[token.address] = updatedToken; - }); return { ...state, networkId: action.data.networkId, userAddress: action.data.userAddress, sideToAssetToken: action.data.sideToAssetToken, - tokenByAddress, + tokenByAddress: action.data.tokenByAddress, }; } |