Skip to content

Commit

Permalink
chore(trading): add more specific selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive committed Feb 11, 2025
1 parent 7422381 commit 7f4d77f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
26 changes: 16 additions & 10 deletions suite-common/trading/src/middlewares/tradingMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@ import { tradingBuyActions } from '../actions/buyActions';
import { tradingActions } from '../actions/tradingActions';
import { INVITY_API_RELOAD_DATA_AFTER_MS } from '../constants';
import { invityAPI } from '../invityAPI';
import { TradingRootState, selectState } from '../selectors/tradingSelectors';
import {
TradingRootState,
selectTradingBuy,
selectTradingInfo,
selectTradingLoadingAndTimestamp,
selectTradingSelectedAccount,
selectTradingSettingEnviroment,
} from '../selectors/tradingSelectors';
import { buyThunks } from '../thunks/buyThunks';

/**
* In the Sell and Swap section an account can be changed by a user in the select
*/
export const getAccountAccordingToRoute = (state: TradingRootState) => {
export const getAccountAccordingToSection = (state: TradingRootState) => {
const {
selectedAccount: { account },
account,
// accounts,
} = state.wallet;
} = selectTradingSelectedAccount(state);

return account;
};

export const tradingMiddleware = createMiddleware(async (action, { dispatch, next, getState }) => {
const state = selectState(getState());
const { isLoading, lastLoadedTimestamp } = state.wallet.trading;
const { isLoading, lastLoadedTimestamp } = selectTradingLoadingAndTimestamp(getState());

if (action.type === tradingActions.loadInvityData.type) {
const account = getAccountAccordingToRoute(state);
const { platforms, coins } = state.wallet.trading.info;
const { buyInfo } = state.wallet.trading.buy;
const account = getAccountAccordingToSection(getState());
const { platforms, coins } = selectTradingInfo(getState());
const { buyInfo } = selectTradingBuy(getState());

const currentAccountDescriptor = invityAPI.getCurrentAccountDescriptor();
const isDifferentAccount = currentAccountDescriptor !== account?.descriptor;
Expand All @@ -39,7 +45,7 @@ export const tradingMiddleware = createMiddleware(async (action, { dispatch, nex
) {
dispatch(tradingActions.setLoading(true));

const { invityServerEnvironment } = state.suite.settings.debug;
const invityServerEnvironment = selectTradingSettingEnviroment(getState());
if (invityServerEnvironment) {
invityAPI.setInvityServersEnvironment(invityServerEnvironment);
}
Expand Down
26 changes: 17 additions & 9 deletions suite-common/trading/src/selectors/tradingSelectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Route } from '@suite-common/suite-types/src/route';
import { Account, SelectedAccountStatus } from '@suite-common/wallet-types';
import { AddressDisplayOptions } from '@suite-common/wallet-types/src/settings';

Expand All @@ -20,13 +19,22 @@ export type TradingRootState = {
};
};
};
router: {
url: string;
pathname: string;
route: {
name: Route['name'];
};
};
};

export const selectState = (state: TradingRootState) => state;
export const selectTradingLoadingAndTimestamp = (state: TradingRootState) => ({
isLoading: state.wallet.trading.isLoading,
lastLoadedTimestamp: state.wallet.trading.lastLoadedTimestamp,
});

export const selectTradingInfo = (state: TradingRootState) => state.wallet.trading.info;

export const selectTradingBuy = (state: TradingRootState) => state.wallet.trading.buy;

export const selectTradingSelectedAccount = (state: TradingRootState) =>
state.wallet.selectedAccount;

export const selectTradingSettingEnviroment = (state: TradingRootState) =>
state.suite.settings.debug.invityServerEnvironment;

export const selectTradingSettingAddressDisplayType = (state: TradingRootState) =>
state.suite.settings.addressDisplayType;
4 changes: 2 additions & 2 deletions suite-common/trading/src/thunks/tradingThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Account, AddressDisplayOptions } from '@suite-common/wallet-types';

import { tradingBuyActions } from '../actions/buyActions';
import { tradingActions } from '../actions/tradingActions';
import { selectState } from '../selectors/tradingSelectors';
import { selectTradingSettingAddressDisplayType } from '../selectors/tradingSelectors';
import { getUnusedAddressFromAccount } from '../utils';

const TRADING_COMMON_PREFIX = '@trading-common/thunk';
Expand Down Expand Up @@ -33,7 +33,7 @@ const verifyAddressThunk = createThunk(

dispatch(tradingActions.setModalAccountKey(account.key));

const { addressDisplayType } = selectState(getState()).suite.settings;
const addressDisplayType = selectTradingSettingAddressDisplayType(getState());

const { useEmptyPassphrase, connected, available } = device;

Expand Down

0 comments on commit 7f4d77f

Please sign in to comment.