diff --git a/src/logic/tokens/store/actions/loadActiveTokens.ts b/src/logic/tokens/store/actions/loadActiveTokens.ts index c384bd4e..7b4ae61b 100644 --- a/src/logic/tokens/store/actions/loadActiveTokens.ts +++ b/src/logic/tokens/store/actions/loadActiveTokens.ts @@ -7,7 +7,7 @@ import { getActiveTokens } from 'src/logic/tokens/utils/tokensStorage' const loadActiveTokens = () => async (dispatch) => { try { - const tokens = await getActiveTokens() + const tokens = (await getActiveTokens()) || {} // The filter of strings was made because of the issue #751. Please see: https://github.com/gnosis/safe-react/pull/755#issuecomment-612969340 const tokenRecordsList = List( Object.values(tokens) diff --git a/src/logic/tokens/store/actions/removeToken.ts b/src/logic/tokens/store/actions/removeToken.ts deleted file mode 100644 index d6aab7bc..00000000 --- a/src/logic/tokens/store/actions/removeToken.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { createAction } from 'redux-actions' - -import { removeFromActiveTokens, removeTokenFromStorage } from 'src/logic/tokens/utils/tokensStorage' - -export const REMOVE_TOKEN = 'REMOVE_TOKEN' - -export const removeToken = createAction(REMOVE_TOKEN, (safeAddress, token) => ({ - safeAddress, - token, -})) - -const deleteToken = (safeAddress, token) => async (dispatch) => { - dispatch(removeToken(safeAddress, token)) - - await removeFromActiveTokens(safeAddress, token) - await removeTokenFromStorage(safeAddress, token) -} - -export default deleteToken diff --git a/src/logic/tokens/store/reducer/tokens.ts b/src/logic/tokens/store/reducer/tokens.ts index 418da6b0..50dc1def 100644 --- a/src/logic/tokens/store/reducer/tokens.ts +++ b/src/logic/tokens/store/reducer/tokens.ts @@ -2,7 +2,6 @@ import { Map } from 'immutable' import { handleActions } from 'redux-actions' import { ADD_TOKEN } from 'src/logic/tokens/store/actions/addToken' -import { REMOVE_TOKEN } from 'src/logic/tokens/store/actions/removeToken' import { ADD_TOKENS } from 'src/logic/tokens/store/actions/saveTokens' import { makeToken } from 'src/logic/tokens/store/model/token' @@ -27,12 +26,6 @@ export default handleActions( return state.set(tokenAddress, makeToken(token)) }, - [REMOVE_TOKEN]: (state, action) => { - const { token } = action.payload - const { address: tokenAddress } = token - - return state.remove(tokenAddress) - }, }, Map(), ) diff --git a/src/logic/tokens/utils/tokensStorage.ts b/src/logic/tokens/utils/tokensStorage.ts index c34d7c04..1b9f7970 100644 --- a/src/logic/tokens/utils/tokensStorage.ts +++ b/src/logic/tokens/utils/tokensStorage.ts @@ -1,6 +1,7 @@ -import { List } from 'immutable' +import { Map } from 'immutable' import { loadFromStorage, saveToStorage } from 'src/utils/storage' +import { TokenProps, Token } from './../store/model/token' export const ACTIVE_TOKENS_KEY = 'ACTIVE_TOKENS' export const CUSTOM_TOKENS_KEY = 'CUSTOM_TOKENS' @@ -9,7 +10,7 @@ export const CUSTOM_TOKENS_KEY = 'CUSTOM_TOKENS' // to avoid iterating a large amount of data of tokens from the backend // Custom tokens should be saved too unless they're deleted (marking them as inactive doesn't count) -export const saveActiveTokens = async (tokens) => { +export const saveActiveTokens = async (tokens: Map): Promise => { try { await saveToStorage(ACTIVE_TOKENS_KEY, tokens.toJS()) } catch (err) { @@ -17,34 +18,8 @@ export const saveActiveTokens = async (tokens) => { } } -export const getActiveTokens = async () => { - const data = await loadFromStorage(ACTIVE_TOKENS_KEY) +export const getActiveTokens = async (): Promise | undefined> => { + const data = await loadFromStorage>(ACTIVE_TOKENS_KEY) - return data || {} -} - -export const getCustomTokens = async () => { - const data = await loadFromStorage(CUSTOM_TOKENS_KEY) - - return data ? List(data) : List() -} - -export const removeTokenFromStorage = async (safeAddress, token) => { - const data = await getCustomTokens() - - try { - const index = data.indexOf(token) - await saveToStorage(CUSTOM_TOKENS_KEY, data.remove(index)) - } catch (err) { - console.error('Error removing token in localstorage', err) - } -} - -export const removeFromActiveTokens = async (safeAddress, token) => { - const activeTokens = await getActiveTokens() - const index = activeTokens.findIndex((activeToken) => activeToken.name === token.name) - - if (index !== -1) { - await saveActiveTokens(safeAddress) - } + return data }