import { Middleware } from '@reduxjs/toolkit'; import { CheckoutResult, MiddlewareParams } from '../../../types'; import { checkoutApi } from '../../../data/client/checkout'; export const redirectionMiddleware: Middleware = ({ dispatch }: MiddlewareParams) => { return (next) => (action) => { const result = next(action) as CheckoutResult; const preOrder = result?.payload?.pre_order; if (!preOrder) { return result; } if (preOrder?.is_redirected) { const contextList = result?.payload?.context_list; if ( contextList?.find( (ctx) => ctx.page_name === 'RedirectionPaymentSelectedPage' ) ) { dispatch( checkoutApi.endpoints.setPaymentOption.initiate( preOrder.payment_option?.pk ) ); return null; } } return result; }; }; Object.defineProperty(redirectionMiddleware, 'name', { value: 'redirectionMiddleware' });