Fix token storage type errors, remove unused functions
This commit is contained in:
parent
e7c012d5d1
commit
8f0cccdb59
|
@ -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)
|
||||
|
|
|
@ -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
|
|
@ -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(),
|
||||
)
|
||||
|
|
|
@ -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<string, Token>): Promise<void> => {
|
||||
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<Record<string, TokenProps> | undefined> => {
|
||||
const data = await loadFromStorage<Record<string, TokenProps>>(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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue