Skip to content

Commit

Permalink
[TRAH5324] Suisin/chore: fix redirection issue during staging (#18017)
Browse files Browse the repository at this point in the history
* chore: fix redirection issue during staging

* chore: empty commit

* chore: empty commit
  • Loading branch information
suisin-deriv authored Jan 24, 2025
1 parent 6e7c330 commit fe0226b
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 88 deletions.
6 changes: 3 additions & 3 deletions packages/api-v2/src/hooks/useIsHubRedirectionEnabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ const useIsHubRedirectionEnabled = () => {
});
const { data: clientCountry } = useClientCountry();
const { data: accountSettings } = useSettings();
const { citizen } = accountSettings;
const { country_code: countryCode } = accountSettings;

const isHubRedirectionEnabled =
typeof hubEnabledCountryList === 'object' &&
hubEnabledCountryList !== null &&
Array.isArray((hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list) &&
citizen &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(citizen);
countryCode &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(countryCode);

const isChangingToHubAppId =
typeof hubEnabledCountryList === 'object' &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ const PlatformDropdown = ({ app_routing_history, closeDrawer, platform_config, s
const { isDesktop } = useDevice();
const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled();
const { client } = useStore();
const { account_settings, has_wallet } = client;
const { trading_hub } = account_settings;
const { has_wallet } = client;

const TradersHubRedirect = () => {
return (
<div className='platform-dropdown__cta'>
<BinaryLink
onClick={() => {
if (isHubRedirectionEnabled && has_wallet && !!trading_hub) {
if (isHubRedirectionEnabled && has_wallet) {
window.location.assign(platforms.tradershub_os.url);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { platforms, routes } from '@deriv/shared';
import { Localize } from '@deriv/translations';
import { AccountSwitcherWalletList } from './account-switcher-wallet-list';
import { useIsHubRedirectionEnabled, useIsRtl, useStoreWalletAccountsList } from '@deriv/hooks';
import { observer, useStore } from '@deriv/stores';
import { observer } from '@deriv/stores';
import './account-switcher-wallet-mobile.scss';

type TAccountSwitcherWalletMobile = {
Expand All @@ -18,9 +18,6 @@ export const AccountSwitcherWalletMobile = observer(({ is_visible, toggle, login
const history = useHistory();
const isRtl = useIsRtl();
const { data: wallet_list } = useStoreWalletAccountsList();
const { client } = useStore();
const { account_settings } = client;
const { trading_hub } = account_settings;

const dtrade_account_wallets = wallet_list?.filter(wallet => wallet.dtrade_loginid);
const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled();
Expand All @@ -30,7 +27,7 @@ export const AccountSwitcherWalletMobile = observer(({ is_visible, toggle, login
}, [toggle]);

const handleTradersHubRedirect = () => {
if (isHubRedirectionEnabled && !!trading_hub) {
if (isHubRedirectionEnabled) {
window.location.assign(platforms.tradershub_os.url);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { useHistory } from 'react-router';
import { Icon, Text, ThemedScrollbars, useOnClickOutside } from '@deriv/components';
import { platforms, routes } from '@deriv/shared';
import { observer, useStore } from '@deriv/stores';
import { observer } from '@deriv/stores';
import { Localize } from '@deriv/translations';
import { useIsHubRedirectionEnabled, useStoreWalletAccountsList } from '@deriv/hooks';
import { AccountSwitcherWalletList } from './account-switcher-wallet-list';
Expand All @@ -18,9 +18,6 @@ export const AccountSwitcherWallet = observer(({ is_visible, toggle }: TAccountS
const dtrade_account_wallets = wallet_list?.filter(wallet => wallet.dtrade_loginid);

const history = useHistory();
const { client } = useStore();
const { account_settings } = client;
const { trading_hub } = account_settings;

const wrapper_ref = React.useRef<HTMLDivElement>(null);
const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled();
Expand All @@ -43,7 +40,7 @@ export const AccountSwitcherWallet = observer(({ is_visible, toggle }: TAccountS
useOnClickOutside(wrapper_ref, closeAccountsDialog, validateClickOutside);

const handleTradersHubRedirect = async () => {
if (isHubRedirectionEnabled && !!trading_hub) {
if (isHubRedirectionEnabled) {
window.location.assign(platforms.tradershub_os.url);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ const RootComponent = observer(props => {
setIsWalletsOnboardingTourGuideVisible,
notification_messages_ui,
} = ui;
const { has_wallet, logout, account_settings, prevent_redirect_to_hub } = client;
const { trading_hub } = account_settings;
const { has_wallet, logout, prevent_redirect_to_hub } = client;

const { oAuthLogout } = useOauth2({ handleLogout: logout });

Expand All @@ -39,11 +38,11 @@ const RootComponent = observer(props => {
const STAGING_REDIRECT_URL = 'https://staging-hub.deriv.com/tradershub/options';

useEffect(() => {
if (isHubRedirectionEnabled && has_wallet && !!trading_hub && !prevent_redirect_to_hub) {
if (isHubRedirectionEnabled && has_wallet && !prevent_redirect_to_hub) {
const redirectUrl = process.env.NODE_ENV === 'production' ? PRODUCTION_REDIRECT_URL : STAGING_REDIRECT_URL;
window.location.assign(redirectUrl);
}
}, [isHubRedirectionEnabled, has_wallet, trading_hub, prevent_redirect_to_hub]);
}, [isHubRedirectionEnabled, has_wallet, prevent_redirect_to_hub]);

return has_wallet ? (
<Wallets
Expand All @@ -53,7 +52,7 @@ const RootComponent = observer(props => {
}}
notificationMessagesUi={notification_messages_ui}
onWalletsOnboardingTourGuideCloseHandler={onWalletsOnboardingTourGuideCloseHandler}
isHubRedirectionEnabled={isHubRedirectionEnabled && !!trading_hub}
isHubRedirectionEnabled={isHubRedirectionEnabled}
/>
) : (
<AppStore {...props} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ jest.mock('../useGrowthbookGetFeatureValue', () =>
describe('useIsHubRedirectionEnabled', () => {
const mock_store = mockStore({
client: {
account_settings: { citizen: 'US' },
account_settings: { country_code: 'US' },
clients_country: 'US',
},
});

beforeEach(() => {
mock_store.client.clients_country = 'US';
mock_store.client.account_settings.citizen = 'US';
mock_store.client.account_settings.country_code = 'US';
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
hub_enabled_country_list: ['AU'],
Expand All @@ -43,7 +43,7 @@ describe('useIsHubRedirectionEnabled', () => {
});

it('should return false if client country is not in the hub enabled list', () => {
mock_store.client.account_settings.citizen = 'UK';
mock_store.client.account_settings.country_code = 'UK';
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
hub_enabled_country_list: ['US', 'AU'],
Expand All @@ -64,7 +64,7 @@ describe('useIsHubRedirectionEnabled', () => {
expect(result.current.isHubRedirectionEnabled).toBe(true);
});

it('should return isChangingToHubAppId true if client country is in the hub enabled list but not in the citizen list', () => {
it('should return isChangingToHubAppId true if client country is in the hub enabled list but not in the country_code list', () => {
mock_store.client.clients_country = 'UK';
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
Expand Down
6 changes: 4 additions & 2 deletions packages/hooks/src/useIsHubRedirectionEnabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ const useIsHubRedirectionEnabled = () => {
typeof hubEnabledCountryList === 'object' &&
hubEnabledCountryList !== null &&
Array.isArray((hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list) &&
account_settings.citizen &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(account_settings.citizen);
account_settings.country_code &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(
account_settings.country_code
);

const isChangingToHubAppId =
typeof hubEnabledCountryList === 'object' &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useHistory } from 'react-router-dom';
import { useOnClickOutside } from 'usehooks-ts';
import { useDevice } from '@deriv-com/ui';
import { THooks, TMarketTypes, TPlatforms } from '../../types';
import { useIsHubRedirectionEnabled, useSettings } from '@deriv/api-v2';
import { useIsHubRedirectionEnabled } from '@deriv/api-v2';

type TModalState = {
accountId?: string;
Expand Down Expand Up @@ -48,8 +48,6 @@ const ModalProvider = ({ children }: React.PropsWithChildren<unknown>) => {
const { isDesktop } = useDevice();
const history = useHistory();
const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled();
const { data: accountSettings } = useSettings();
const { trading_hub: tradingHub } = accountSettings;

const rootRef = useRef<HTMLElement>(document.getElementById('wallets_modal_root'));
const rootHeaderRef = useRef<HTMLElement | null>(document.getElementById('wallets_modal_show_header_root'));
Expand Down Expand Up @@ -89,7 +87,7 @@ const ModalProvider = ({ children }: React.PropsWithChildren<unknown>) => {
}));
// We need to add this check here because wallets account is coming from Low-Code tradershub.
// This condition is to reload the page when the modal is closed.
if (isHubRedirectionEnabled && !!tradingHub) {
if (isHubRedirectionEnabled) {
window.location.reload();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
useLandingCompany,
useWalletAccountsList,
useIsHubRedirectionEnabled,
useSettings,
} from '@deriv/api-v2';
import { Analytics } from '@deriv-com/analytics';
import { useDevice } from '@deriv-com/ui';
Expand Down Expand Up @@ -44,11 +43,6 @@ jest.mock('@deriv/api-v2', () => ({
financial_company: { shortcode: 'svg' },
},
})),
useSettings: jest.fn(() => ({
data: {
trading_hub: 0,
},
})),
useWalletAccountsList: jest.fn(),
}));

Expand Down Expand Up @@ -136,11 +130,6 @@ describe('WalletsAddMoreCardBanner', () => {
(useIsHubRedirectionEnabled as jest.Mock).mockReturnValue({
isHubRedirectionEnabled: false,
});
(useSettings as jest.Mock).mockReturnValue({
data: {
trading_hub: 0,
},
});
(useWalletAccountSwitcher as jest.Mock).mockReturnValue(mockSwitchWalletAccount);
(useHistory as jest.Mock).mockReturnValue({ push: mockHistoryPush });
$root = document.createElement('div');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ jest.mock('@deriv/api-v2', () => ({
useIsHubRedirectionEnabled: jest.fn(() => ({
isHubRedirectionEnabled: false,
})),
useSettings: jest.fn(() => ({
data: {
trading_hub: 0,
},
})),
}));

const mockModalHide = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ jest.mock('@deriv/api-v2', () => ({
useIsHubRedirectionEnabled: jest.fn(() => ({
isHubRedirectionEnabled: false,
})),
useSettings: jest.fn(() => ({
data: {
trading_hub: 0,
},
})),
}));

jest.mock('react-router-dom', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ jest.mock('@deriv/api-v2', () => ({
useIsHubRedirectionEnabled: jest.fn(() => ({
isHubRedirectionEnabled: false,
})),
useSettings: jest.fn(() => ({
data: {
trading_hub: 0,
},
})),
}));

jest.mock('../../../../modals', () => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React from 'react';
import { createMemoryHistory } from 'history';
import { Router } from 'react-router-dom';
import {
useActiveWalletAccount,
useCreateOtherCFDAccount,
useIsHubRedirectionEnabled,
useSettings,
} from '@deriv/api-v2';
import { useActiveWalletAccount, useCreateOtherCFDAccount, useIsHubRedirectionEnabled } from '@deriv/api-v2';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ModalProvider } from '../../../../../../components/ModalProvider';
Expand Down Expand Up @@ -52,11 +47,6 @@ describe('AvailableCTraderAccountsList', () => {
(useIsHubRedirectionEnabled as jest.Mock).mockReturnValue({
isHubRedirectionEnabled: false,
});
(useSettings as jest.Mock).mockReturnValue({
data: {
trading_hub: 0,
},
});
(useIsRtl as jest.Mock).mockReturnValue(false);
});

Expand All @@ -81,7 +71,7 @@ describe('AvailableCTraderAccountsList', () => {
expect(screen.getByText('CFDs on financial and derived instruments with copy trading.')).toBeInTheDocument();
});

it('calls mutate function when card is clicked', () => {
it('calls mutate function when card is clicked', async () => {
const mutateMock = jest.fn();
(useCreateOtherCFDAccount as jest.Mock).mockReturnValue({
isLoading: false,
Expand All @@ -95,7 +85,7 @@ describe('AvailableCTraderAccountsList', () => {
<AvailableCTraderAccountsList />
</ModalProvider>
);
userEvent.click(screen.getByTestId('dt_wallets_trading_account_card'));
await userEvent.click(screen.getByTestId('dt_wallets_trading_account_card'));

expect(mutateMock).toHaveBeenCalledWith({
payload: {
Expand Down Expand Up @@ -126,7 +116,7 @@ describe('AvailableCTraderAccountsList', () => {
expect(screen.getByTestId('dt_ctrader_success_modal')).toBeInTheDocument();
});

it('shows error modal when account creation fails', () => {
it('shows error modal when account creation fails', async () => {
(useCreateOtherCFDAccount as jest.Mock).mockReturnValue({
error: { error: { message: 'Test error' } },
isLoading: false,
Expand All @@ -147,7 +137,7 @@ describe('AvailableCTraderAccountsList', () => {
expect(screen.getAllByText('Test error')[0]).toBeInTheDocument();
expect(screen.getByText('Try again')).toBeInTheDocument();

userEvent.click(screen.getByText('Try again'));
await userEvent.click(screen.getByText('Try again'));
expect(screen.queryByTestId('dt_wallet_error')).not.toBeInTheDocument();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ jest.mock('@deriv/api-v2', () => ({
useIsHubRedirectionEnabled: jest.fn(() => ({
isHubRedirectionEnabled: false,
})),
useSettings: jest.fn(() => ({
data: {
trading_hub: 0,
},
})),
}));

jest.mock('../../../../../modals/MT5TradeModal', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ jest.mock('@deriv/api-v2', () => ({
useIsHubRedirectionEnabled: jest.fn(() => ({
isHubRedirectionEnabled: false,
})),
useSettings: jest.fn(() => ({
data: {
trading_hub: 0,
},
})),
}));

jest.mock('@deriv-com/ui', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
useCreateOtherCFDAccount,
useDxtradeAccountsList,
useIsHubRedirectionEnabled,
useSettings,
} from '@deriv/api-v2';
import { useDevice } from '@deriv-com/ui';
import { render, screen, waitFor } from '@testing-library/react';
Expand Down Expand Up @@ -136,11 +135,6 @@ describe('DxtradeEnterPasswordModal', () => {
(useIsHubRedirectionEnabled as jest.Mock).mockReturnValue({
isHubRedirectionEnabled: false,
});
(useSettings as jest.Mock).mockReturnValue({
data: {
trading_hub: 0,
},
});
});

afterEach(() => {
Expand Down

0 comments on commit fe0226b

Please sign in to comment.