Skip to content

Commit

Permalink
Refactor token selectors
Browse files Browse the repository at this point in the history
Unused token selectors have been removed, and the remaining token
selector has been moved to the `metamask` Redux store.
  • Loading branch information
Gudahtt committed May 28, 2020
1 parent 7ff3b4c commit 49066f3
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 68 deletions.
2 changes: 2 additions & 0 deletions ui/app/ducks/metamask/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,5 @@ export const getSwitchToConnectedAlertEnabledness = (state) => getAlertEnabledne
export const getUnconnectedAccountAlertEnabledness = (state) => getAlertEnabledness(state)[ALERT_TYPES.unconnectedAccount]

export const getSwitchToConnectedAlertShown = (state) => state.metamask.switchToConnectedAlertShown

export const getTokens = (state) => state.metamask.tokens
5 changes: 3 additions & 2 deletions ui/app/hooks/tests/useTransactionDisplayData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { renderHook } from '@testing-library/react-hooks'
import sinon from 'sinon'
import transactions from '../../../../test/data/transaction-data.json'
import { useTransactionDisplayData } from '../useTransactionDisplayData'
import { tokenSelector, getPreferences, getShouldShowFiat, getNativeCurrency, getCurrentCurrency } from '../../selectors'
import { getPreferences, getShouldShowFiat, getNativeCurrency, getCurrentCurrency } from '../../selectors'
import { getTokens } from '../../ducks/metamask/metamask'
import * as i18nhooks from '../useI18nContext'
import { getMessage } from '../../helpers/utils/i18n-helper'
import messages from '../../../../app/_locales/en/messages.json'
Expand Down Expand Up @@ -81,7 +82,7 @@ describe('useTransactionDisplayData', function () {
useI18nContext = sinon.stub(i18nhooks, 'useI18nContext')
useI18nContext.returns((key, variables) => getMessage('en', messages, key, variables))
useSelector.callsFake((selector) => {
if (selector === tokenSelector) {
if (selector === getTokens) {
return []
} else if (selector === getPreferences) {
return {
Expand Down
4 changes: 2 additions & 2 deletions ui/app/hooks/useTransactionDisplayData.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import { useCurrencyDisplay } from './useCurrencyDisplay'
import { useTokenDisplayValue } from './useTokenDisplayValue'
import { useTokenData } from './useTokenData'
import { tokenSelector } from '../selectors'
import { getTokens } from '../ducks/metamask/metamask'

/**
* @typedef {Object} TransactionDisplayData
Expand All @@ -51,7 +51,7 @@ import { tokenSelector } from '../selectors'
* @return {TransactionDisplayData}
*/
export function useTransactionDisplayData (transactionGroup) {
const knownTokens = useSelector(tokenSelector)
const knownTokens = useSelector(getTokens)
const t = useI18nContext()
const { initialTransaction, primaryTransaction } = transactionGroup
// initialTransaction contains the data we need to derive the primary purpose of this transaction group
Expand Down
4 changes: 2 additions & 2 deletions ui/app/pages/confirm-approve/confirm-approve.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { compose } from 'redux'
import { withRouter } from 'react-router-dom'
import {
contractExchangeRateSelector,
tokenSelector,
transactionFeeSelector,
} from '../../selectors'
import { getTokens } from '../../ducks/metamask/metamask'
import { showModal } from '../../store/actions'
import {
getTokenData,
Expand Down Expand Up @@ -45,7 +45,7 @@ const mapStateToProps = (state, ownProps) => {
ethTransactionTotal,
fiatTransactionTotal,
} = transactionFeeSelector(state, transaction)
const tokens = tokenSelector(state)
const tokens = getTokens(state)
const currentToken = tokens && tokens.find(({ address }) => tokenAddress === address)
const { decimals, symbol: tokenSymbol } = currentToken || {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { withRouter } from 'react-router-dom'
import ConfirmTokenTransactionBase from './confirm-token-transaction-base.component'
import {
contractExchangeRateSelector,
tokenSelector,
transactionFeeSelector,
} from '../../selectors'
import { getTokens } from '../../ducks/metamask/metamask'
import {
getTokenData,
} from '../../helpers/utils/transactions.util'
Expand Down Expand Up @@ -38,7 +38,7 @@ const mapStateToProps = (state, ownProps) => {
ethTransactionTotal,
fiatTransactionTotal,
} = transactionFeeSelector(state, transaction)
const tokens = tokenSelector(state)
const tokens = getTokens(state)
const currentToken = tokens && tokens.find(({ address }) => tokenAddress === address)
const { decimals, symbol: tokenSymbol } = currentToken || {}

Expand Down
2 changes: 1 addition & 1 deletion ui/app/pages/send/send.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
getSendToNickname,
getTokenBalance,
getQrCodeData,
getTokens,
getSelectedAddress,
getAddressBook,
} from '../../selectors'
Expand All @@ -43,6 +42,7 @@ import {
import {
fetchBasicGasEstimates,
} from '../../ducks/gas/gas.duck'
import { getTokens } from '../../ducks/metamask/metamask'
import {
calcGasTotal,
} from './send.utils.js'
Expand Down
1 change: 0 additions & 1 deletion ui/app/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ export * from './first-time-flow'
export * from './permissions'
export * from './selectors'
export * from './send'
export * from './tokens'
export * from './transactions'
4 changes: 0 additions & 4 deletions ui/app/selectors/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ export function getGasButtonGroupShown (state) {
return state.send.gasButtonGroupShown
}

export function getTokens (state) {
return state.metamask.tokens
}

export function getTitleKey (state) {
const isEditing = Boolean(getSendEditingTransactionId(state))
const isToken = Boolean(getSelectedToken(state))
Expand Down
15 changes: 0 additions & 15 deletions ui/app/selectors/tests/send.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
gasFeeIsInError,
getGasLoadingError,
getGasButtonGroupShown,
getTokens,
getTitleKey,
isSendFormInError,
} from '../send'
Expand Down Expand Up @@ -510,20 +509,6 @@ describe('send selectors', function () {
})
})

describe('add-recipient selectors', function () {
describe('getTokens()', function () {
it('should return empty array if no tokens in state', function () {
const state = {
metamask: {
tokens: [],
},
}

assert.deepStrictEqual(getTokens(state), [])
})
})
})

describe('send-header selectors', function () {

const getMetamaskSendMockState = (send) => {
Expand Down
28 changes: 0 additions & 28 deletions ui/app/selectors/tests/tokens.test.js

This file was deleted.

11 changes: 0 additions & 11 deletions ui/app/selectors/tokens.js

This file was deleted.

0 comments on commit 49066f3

Please sign in to comment.