Skip to content

Commit

Permalink
fixup! chore(trading): add buy middleware and thunks
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive committed Feb 11, 2025
1 parent f4ad791 commit 0f780bb
Showing 1 changed file with 1 addition and 40 deletions.
41 changes: 1 addition & 40 deletions suite-common/trading/src/middlewares/tradingMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import { INVITY_API_RELOAD_DATA_AFTER_MS } from '../constants';
import { invityAPI } from '../invityAPI';
import { TradingRootState, selectState } from '../selectors/tradingSelectors';
import { buyThunks } from '../thunks/buyThunks';
import { getTradeTypeByRoute } from '../utils';

const LOCATION_CHANGE = '@router/location-change';

/**
* In the Sell and Swap section an account can be changed by a user in the select
Expand All @@ -25,7 +22,6 @@ export const getAccountAccordingToRoute = (state: TradingRootState) => {
export const tradingMiddleware = createMiddleware(async (action, { dispatch, next, getState }) => {
const state = selectState(getState());
const { isLoading, lastLoadedTimestamp } = state.wallet.trading;
const isRouteChange = action.type === LOCATION_CHANGE;

if (action.type === tradingActions.LOAD_DATA) {
const account = getAccountAccordingToRoute(state);
Expand All @@ -48,11 +44,6 @@ export const tradingMiddleware = createMiddleware(async (action, { dispatch, nex
invityAPI.setInvityServersEnvironment(invityServerEnvironment);
}

const tradeType = getTradeTypeByRoute(state.router.route?.name);
if (tradeType) {
dispatch(tradingActions.tradingActions.setTradingActiveSection(tradeType));
}

invityAPI.createInvityAPIKey(account.descriptor);

if (isDifferentAccount || !platforms || !coins) {
Expand All @@ -71,35 +62,5 @@ export const tradingMiddleware = createMiddleware(async (action, { dispatch, nex
}
}

next(action);

// get the new state after the action has been processed
const newState = selectState(getState());

// TODO: native route change control?
if (isRouteChange) {
const routeName = newState.router.route?.name;
const isBuy = routeName === 'wallet-trading-buy';
const isSell = routeName === 'wallet-trading-sell';
const isExchange = routeName === 'wallet-trading-exchange';

if (isBuy) {
dispatch(tradingActions.tradingActions.setTradingActiveSection('buy'));
}

const wasBuy = state.router.route?.name === 'wallet-trading-buy';
const wasSell = state.router.route?.name === 'wallet-trading-sell';
const isBuyToSell = wasBuy && isSell;
const isSellToBuy = wasSell && isBuy;

const cleanupPrefilledFromCryptoId =
!!newState.wallet.trading.prefilledFromCryptoId &&
((!isSell && !isExchange && !isBuy) || isBuyToSell || isSellToBuy);

if (cleanupPrefilledFromCryptoId) {
dispatch(tradingActions.tradingActions.setTradingFromPrefilledCryptoId(undefined));
}
}

return action;
return next(action);
});

0 comments on commit 0f780bb

Please sign in to comment.