Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.
46 changes: 0 additions & 46 deletions src/logic/currencyValues/store/actions/fetchCurrencyValues.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/logic/currencyValues/store/actions/fetchSelectedCurrency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { setCurrencyBalances } from 'src/logic/currencyValues/store/actions/setCurrencyBalances'
import { setCurrencyRate } from 'src/logic/currencyValues/store/actions/setCurrencyRate'
import { setSelectedCurrency } from 'src/logic/currencyValues/store/actions/setSelectedCurrency'
import { AVAILABLE_CURRENCIES } from 'src/logic/currencyValues/store/model/currencyValues'
import { loadSelectedCurrency } from 'src/logic/currencyValues/store/utils/currencyValuesStorage'
import { Dispatch } from 'redux'

export const fetchSelectedCurrency = (safeAddress: string) => async (
dispatch: Dispatch<typeof setCurrencyBalances | typeof setSelectedCurrency | typeof setCurrencyRate>,
): Promise<void> => {
try {
const storedSelectedCurrency = await loadSelectedCurrency()

dispatch(setSelectedCurrency(safeAddress, storedSelectedCurrency || AVAILABLE_CURRENCIES.USD))
} catch (err) {
console.error('Error fetching currency values', err)
}
return Promise.resolve()
}
13 changes: 12 additions & 1 deletion src/logic/currencyValues/store/actions/setSelectedCurrency.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { createAction } from 'redux-actions'
import { ThunkDispatch } from 'redux-thunk'
import { AnyAction } from 'redux'
import { AppReduxState } from 'src/store'
import { AVAILABLE_CURRENCIES } from '../model/currencyValues'
import fetchCurrencyRate from 'src/logic/currencyValues/store/actions/fetchCurrencyRate'

export const SET_CURRENT_CURRENCY = 'SET_CURRENT_CURRENCY'

export const setSelectedCurrency = createAction(
const setCurrentCurrency = createAction(
SET_CURRENT_CURRENCY,
(safeAddress: string, selectedCurrency: AVAILABLE_CURRENCIES) => ({
safeAddress,
selectedCurrency,
}),
)

export const setSelectedCurrency = (safeAddress: string, selectedCurrency: AVAILABLE_CURRENCIES) => (
dispatch: ThunkDispatch<AppReduxState, undefined, AnyAction>,
): void => {
dispatch(setCurrentCurrency(safeAddress, selectedCurrency))
dispatch(fetchCurrencyRate(safeAddress, selectedCurrency))
}
10 changes: 5 additions & 5 deletions src/logic/currencyValues/store/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import fetchCurrencyRate from 'src/logic/currencyValues/store/actions/fetchCurrencyRate'
import { SET_CURRENT_CURRENCY } from 'src/logic/currencyValues/store/actions/setSelectedCurrency'
import { saveSelectedCurrency } from 'src/logic/currencyValues/store/utils/currencyValuesStorage'

const watchedActions = [SET_CURRENT_CURRENCY]

const currencyValuesStorageMiddleware = (store) => (next) => async (action) => {
const currencyValuesStorageMiddleware = () => (next) => async (action) => {
const handledAction = next(action)
if (watchedActions.includes(action.type)) {
const { dispatch } = store
switch (action.type) {
case SET_CURRENT_CURRENCY: {
const { safeAddress, selectedCurrency } = action.payload
dispatch(fetchCurrencyRate(safeAddress, selectedCurrency))
const { selectedCurrency } = action.payload

saveSelectedCurrency(selectedCurrency)
break
}

Expand Down
13 changes: 6 additions & 7 deletions src/logic/currencyValues/store/utils/currencyValuesStorage.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { loadFromStorage, saveToStorage } from 'src/utils/storage'
import { CurrencyRateValue } from '../model/currencyValues'
import { Map } from 'immutable'
import { AVAILABLE_CURRENCIES } from '../model/currencyValues'

const CURRENCY_VALUES_STORAGE_KEY = 'CURRENCY_VALUES_STORAGE_KEY'
export const saveCurrencyValues = async (currencyValues: Map<string, CurrencyRateValue>): Promise<void> => {
const SELECTED_CURRENCY_STORAGE_KEY = 'SELECTED_CURRENCY'
export const saveSelectedCurrency = async (selectedCurrency: AVAILABLE_CURRENCIES): Promise<void> => {
try {
await saveToStorage(CURRENCY_VALUES_STORAGE_KEY, currencyValues)
await saveToStorage(SELECTED_CURRENCY_STORAGE_KEY, selectedCurrency)
} catch (err) {
console.error('Error storing currency values info in localstorage', err)
}
}

export const loadCurrencyValues = async (): Promise<Record<string, CurrencyRateValue>> => {
return (await loadFromStorage(CURRENCY_VALUES_STORAGE_KEY)) || {}
export const loadSelectedCurrency = async (): Promise<AVAILABLE_CURRENCIES | undefined> => {
return await loadFromStorage(SELECTED_CURRENCY_STORAGE_KEY)
}
4 changes: 2 additions & 2 deletions src/logic/safe/hooks/useFetchTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { batch, useDispatch } from 'react-redux'
import { useLocation } from 'react-router-dom'

import fetchCollectibles from 'src/logic/collectibles/store/actions/fetchCollectibles'
import { fetchCurrencyValues } from 'src/logic/currencyValues/store/actions/fetchCurrencyValues'
import { fetchSelectedCurrency } from 'src/logic/currencyValues/store/actions/fetchSelectedCurrency'
import activateAssetsByBalance from 'src/logic/tokens/store/actions/activateAssetsByBalance'
import fetchSafeTokens from 'src/logic/tokens/store/actions/fetchSafeTokens'
import { fetchTokens } from 'src/logic/tokens/store/actions/fetchTokens'
Expand All @@ -19,7 +19,7 @@ export const useFetchTokens = (safeAddress: string): void => {
batch(() => {
// fetch tokens there to get symbols for tokens in TXs list
dispatch(fetchTokens())
dispatch(fetchCurrencyValues(safeAddress))
dispatch(fetchSelectedCurrency(safeAddress))
dispatch(fetchSafeTokens(safeAddress))
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/logic/safe/store/reducer/safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default handleActions(
const safeActiveTokens = map.getIn(['safes', safeAddress, 'activeTokens'])
const activeTokens = safeActiveTokens.add(tokenAddress)

map.updateIn(['safes', safeAddress], (prevSafe) => prevSafe.merge({ activeTokens }))
map.updateIn(['safes', safeAddress], (prevSafe) => prevSafe.mergeDeep({ activeTokens }))
})
})
},
Expand All @@ -77,7 +77,7 @@ export default handleActions(
// with initial props and it would overwrite existing ones

if (state.hasIn(['safes', safe.address])) {
return state.updateIn(['safes', safe.address], (prevSafe) => prevSafe.mergeDeep(safe))
return state
}

return state.setIn(['safes', safe.address], makeSafe(safe))
Expand Down
2 changes: 1 addition & 1 deletion src/routes/safe/components/CurrencyDropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import etherIcon from 'src/assets/icons/icon_etherTokens.svg'

const CurrencyDropdown = (): React.ReactElement | null => {
const currenciesList = Object.values(AVAILABLE_CURRENCIES)
const safeAddress = useSelector(safeParamAddressFromStateSelector)
const safeAddress = useSelector(safeParamAddressFromStateSelector) as string
const dispatch = useDispatch()
const [anchorEl, setAnchorEl] = useState(null)
const selectedCurrency = useSelector(currentCurrencySelector)
Expand Down