Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

george / rm72476 / account switching issue when using p2p/cash-deposit with crypto account #6247

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
98484f9
fix(cashier onboarding): fix switching from crypto to fiat accounts u…
heorhi-deriv Aug 19, 2022
f894fe1
test(cashier onboarding): fix tests
heorhi-deriv Aug 19, 2022
d2433e8
refactor(cashier onboarding): refactor code (apply memorization)
heorhi-deriv Aug 22, 2022
e87ccba
Merge branch 'master' of https://github.com/binary-com/deriv-app into…
heorhi-deriv Aug 22, 2022
d4ea81d
Merge branch 'master' into 72476_Account-switching-issue-when-using-P…
heorhi-deriv Aug 24, 2022
e993907
fix(account propmt dialog): fix dispaly proper currency in switch acc…
heorhi-deriv Aug 25, 2022
8438379
Merge branch '72476_Account-switching-issue-when-using-P2P-with-crypt…
heorhi-deriv Aug 25, 2022
3152601
test(account propmt dialog): fix failed test
heorhi-deriv Aug 25, 2022
c4e8a03
perf(account propmt dialog): add check for non_crypto_currency variable
heorhi-deriv Aug 26, 2022
6e0ee76
Merge branch 'master' into 72476_Account-switching-issue-when-using-P…
heorhi-deriv Aug 26, 2022
0a63e16
Merge branch 'master' of https://github.com/binary-com/deriv-app into…
heorhi-deriv Sep 1, 2022
47deb12
Merge branch '72476_Account-switching-issue-when-using-P2P-with-crypt…
heorhi-deriv Sep 1, 2022
0c2499d
Merge branch 'master' into 72476_Account-switching-issue-when-using-P…
heorhi-deriv Sep 2, 2022
c44bb75
Merge branch 'master' into 72476_Account-switching-issue-when-using-P…
heorhi-deriv Sep 9, 2022
0af3088
Merge branch 'master' into 72476_Account-switching-issue-when-using-P…
carolsachdeva Sep 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,10 @@ jest.mock('@deriv/components', () => ({

describe('<AccountPromptDialog />', () => {
const props = {
accounts_list: [
{
balance: '10000.00',
currency: 'USD',
is_crypto: false,
is_dxtrade: false,
is_mt: false,
text: 'USD',
value: 'CR90000195',
},
],
accounts: {
CR90000001: { is_virtual: 0, currency: 'USD' },
CR90000002: { is_virtual: 0, currency: 'BTC' },
},
currency: 'USD',
continueRoute: jest.fn(),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import PropTypes from 'prop-types';
import React from 'react';
import { localize, Localize } from '@deriv/translations';
import { Dialog } from '@deriv/components';
import { isCryptocurrency } from '@deriv/shared';
import { localize, Localize } from '@deriv/translations';
import { connect } from 'Stores/connect';

const AccountPromptDialog = ({
accounts_list,
accounts,
continueRoute,
is_confirmed,
last_location,
Expand All @@ -15,8 +16,15 @@ const AccountPromptDialog = ({
}) => {
React.useEffect(continueRoute, [is_confirmed, last_location, continueRoute]);

const non_crypto_accounts = accounts_list.filter(x => !x.is_crypto);
const non_crypto_currency = non_crypto_accounts.map(x => x.currency)[0];
const non_crypto_account_loginid = React.useMemo(
() =>
Object.entries(accounts).reduce((initial_value, [loginid, settings]) => {
return !settings.is_virtual && !isCryptocurrency(settings.currency) ? loginid : initial_value;
}, ''),
[accounts]
);

const non_crypto_currency = non_crypto_account_loginid && accounts[non_crypto_account_loginid].currency;

return (
<Dialog
Expand All @@ -39,7 +47,7 @@ const AccountPromptDialog = ({
};

AccountPromptDialog.propTypes = {
accounts_list: PropTypes.array,
accounts: PropTypes.object,
continueRoute: PropTypes.func,
is_confirmed: PropTypes.bool,
last_location: PropTypes.string,
Expand All @@ -48,8 +56,8 @@ AccountPromptDialog.propTypes = {
should_show: PropTypes.bool,
};

export default connect(({ modules }) => ({
accounts_list: modules.cashier.account_transfer.accounts_list,
export default connect(({ modules, client }) => ({
accounts: client.accounts,
continueRoute: modules.cashier.account_prompt_dialog.continueRoute,
is_confirmed: modules.cashier.account_prompt_dialog.is_confirmed,
last_location: modules.cashier.account_prompt_dialog.last_location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('<CashierOnboarding />', () => {

it('should show the proper messages when <CashierOnboarding /> is rendered with fiat account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: false }];
props.accounts = { CR90000001: { is_virtual: 0, currency: 'USD' } };
render(
<CashierOnboarding
{...props}
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('<CashierOnboarding />', () => {

it('should show the proper message when <CashierOnboarding /> is rendered with crypto account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: true }];
props.accounts = { CR90000002: { is_virtual: 0, currency: 'BTC' } };
render(
<CashierOnboarding
{...props}
Expand All @@ -83,7 +83,7 @@ describe('<CashierOnboarding />', () => {

it('should trigger proper callbacks when the client chooses "Deposit via bank wire, credit card, and e-wallet" section from his fiat account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: false }];
props.accounts = { CR90000001: { is_virtual: 0, currency: 'USD' } };
const { container } = render(<CashierOnboarding {...props} currency='USD' />);

const node_list = container.querySelectorAll('.cashier-onboarding-detail__div');
Expand All @@ -98,7 +98,7 @@ describe('<CashierOnboarding />', () => {

it('should trigger proper callbacks when the client chooses "Deposit via bank wire, credit card, and e-wallet" section from his crypto account, not having the fiat account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: true }];
props.accounts = { CR90000002: { is_virtual: 0, currency: 'BTC' } };
const { container } = render(
<CashierOnboarding {...props} currency='BTC' available_crypto_currencies={['BTC', 'ETH']} />
);
Expand All @@ -115,7 +115,10 @@ describe('<CashierOnboarding />', () => {

it('should trigger proper callbacks when the client chooses "Deposit via bank wire, credit card, and e-wallet" section from his crypto account, having the fiat account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: true }, { is_crypto: false }];
props.accounts = {
CR90000001: { is_virtual: 0, currency: 'USD' },
CR90000002: { is_virtual: 0, currency: 'BTC' },
};
const { container } = render(
<CashierOnboarding {...props} currency='BTC' available_crypto_currencies={['BTC', 'ETH']} />
);
Expand All @@ -132,7 +135,7 @@ describe('<CashierOnboarding />', () => {

it('should trigger proper callbacks when the client chooses "Deposit cryptocurrencies" section from his fiat account, not having the crypto account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: false }];
props.accounts = { CR90000001: { is_virtual: 0, currency: 'USD' } };
const { container } = render(<CashierOnboarding {...props} currency='USD' />);

const node_list = container.querySelectorAll('.cashier-onboarding-detail__div');
Expand All @@ -147,7 +150,7 @@ describe('<CashierOnboarding />', () => {

it('should trigger proper callbacks when the client chooses "Deposit cryptocurrencies" section from his crypto account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: true }];
props.accounts = { CR90000002: { is_virtual: 0, currency: 'BTC' } };
const { container } = render(
<CashierOnboarding {...props} available_crypto_currencies={['BTC', 'ETH']} currency='BTC' />
);
Expand All @@ -165,7 +168,7 @@ describe('<CashierOnboarding />', () => {

it('should trigger proper callbacks when the client chooses "Buy cryptocurrencies via fiat onramp" section from his fiat account, not having the crypto account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: false }];
props.accounts = { CR90000001: { is_virtual: 0, currency: 'USD' } };
const { container } = render(<CashierOnboarding {...props} currency='USD' />);

const node_list = container.querySelectorAll('.cashier-onboarding-detail__div');
Expand All @@ -180,7 +183,7 @@ describe('<CashierOnboarding />', () => {

it('should trigger proper callbacks when the client chooses "Buy cryptocurrencies" section from his crypto account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: true }];
props.accounts = { CR90000002: { is_virtual: 0, currency: 'BTC' } };
const { container } = render(
<CashierOnboarding {...props} available_crypto_currencies={['BTC', 'ETH']} currency='BTC' />
);
Expand All @@ -198,7 +201,10 @@ describe('<CashierOnboarding />', () => {

it('should trigger proper callbacks when the client chooses "Deposit via payment agents" section', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: false }, { is_crypto: true }];
props.accounts = {
CR90000001: { is_virtual: 0, currency: 'USD' },
CR90000002: { is_virtual: 0, currency: 'BTC' },
};
const { container } = render(
<CashierOnboarding {...props} currency='USD' is_payment_agent_visible_in_onboarding />
);
Expand All @@ -219,7 +225,10 @@ describe('<CashierOnboarding />', () => {
it('should trigger proper callbacks when the client chooses "Deposit with Deriv P2P" section from his fiat account', () => {
const props = mockProps();
const history = createBrowserHistory();
props.accounts_list = [{ is_crypto: false }, { is_crypto: true }];
props.accounts = {
CR90000001: { is_virtual: 0, currency: 'USD' },
CR90000002: { is_virtual: 0, currency: 'BTC' },
};
const { container } = render(
<Router history={history}>
<CashierOnboarding {...props} currency='USD' show_p2p_in_cashier_onboarding />
Expand All @@ -240,7 +249,10 @@ describe('<CashierOnboarding />', () => {

it('should trigger proper callbacks when the client chooses "Deposit with Deriv P2P" section from his crypto account, already having the fiat account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: false }, { is_crypto: true }];
props.accounts = {
CR90000001: { is_virtual: 0, currency: 'USD' },
CR90000002: { is_virtual: 0, currency: 'BTC' },
};
const { container } = render(
<CashierOnboarding
{...props}
Expand All @@ -264,7 +276,7 @@ describe('<CashierOnboarding />', () => {

it('should trigger proper callbacks when the client chooses "Deposit with Deriv P2P" section from his crypto account, not having the fiat account', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: true }];
props.accounts = { CR90000002: { is_virtual: 0, currency: 'BTC' } };
const { container } = render(
<CashierOnboarding
{...props}
Expand All @@ -288,15 +300,15 @@ describe('<CashierOnboarding />', () => {

it('should show the "Learn more about payment methods" message in Mobile mode', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: false }];
props.accounts = { CR90000001: { is_virtual: 0, currency: 'USD' } };
render(<CashierOnboarding {...props} is_mobile />);

expect(screen.getByText('Learn more about payment methods')).toBeInTheDocument();
});

it('should trigger onClick callback when the user clicks "Learn more about payment methods" message in Mobile mode', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: false }];
props.accounts = { CR90000001: { is_virtual: 0, currency: 'USD' } };
window.open = jest.fn();
const { container } = render(<CashierOnboarding {...props} is_mobile />);
const link = container.querySelector('.cashier-onboarding-header-learn-more');
Expand All @@ -307,23 +319,23 @@ describe('<CashierOnboarding />', () => {

it('should not show "Choose a way to fund your account" message if is_switching is true', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: false }];
props.accounts = { CR90000001: { is_virtual: 0, currency: 'USD' } };
render(<CashierOnboarding {...props} is_switching />);

expect(screen.queryByText('Choose a way to fund your account')).not.toBeInTheDocument();
});

it('should not show "Choose a way to fund your account" message if accounts_list is an empty array', () => {
const props = mockProps();
props.accounts_list = [];
props.accounts = [];
render(<CashierOnboarding {...props} />);

expect(screen.queryByText('Choose a way to fund your account')).not.toBeInTheDocument();
});

it('should not show "Choose a way to fund your account" message if is_landing_company_loaded is false', () => {
const props = mockProps();
props.accounts_list = [{ is_crypto: false }];
props.accounts = { CR90000001: { is_virtual: 0, currency: 'USD' } };
render(<CashierOnboarding {...props} is_landing_company_loaded={false} />);

expect(screen.queryByText('Choose a way to fund your account')).not.toBeInTheDocument();
Expand All @@ -332,7 +344,10 @@ describe('<CashierOnboarding />', () => {
it('should redirect to "routes.trade" when the component will unmount', () => {
const props = mockProps();
const history = createBrowserHistory();
props.accounts_list = [{ is_crypto: false }, { is_crypto: true }];
props.accounts = {
CR90000001: { is_virtual: 0, currency: 'USD' },
CR90000002: { is_virtual: 0, currency: 'BTC' },
};
props.has_set_currency = false;
const { unmount } = render(
<Router history={history}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import CashierOnboardingSideNote from './cashier-onboarding-side-note.jsx';
import SideNote from 'Components/side-note';

const CashierOnboarding = ({
accounts,
available_crypto_currencies,
accounts_list,
can_change_fiat_currency,
currency,
has_set_currency,
Expand All @@ -37,8 +37,18 @@ const CashierOnboarding = ({
}) => {
const history = useHistory();
const is_crypto = !!currency && isCryptocurrency(currency);
const has_crypto_account = accounts_list.some(x => x.is_crypto);
const has_fiat_account = accounts_list.some(x => !x.is_crypto);
const has_crypto_account = React.useMemo(
() => Object.values(accounts).some(acc_settings => isCryptocurrency(acc_settings.currency)),
[accounts]
);
const has_fiat_account = React.useMemo(
() =>
Object.values(accounts).some(
acc_settings => !acc_settings.is_virtual && !isCryptocurrency(acc_settings.currency)
),
[accounts]
);

const is_currency_banner_visible =
(!is_crypto && !can_change_fiat_currency) || (is_crypto && available_crypto_currencies.length > 0);

Expand All @@ -58,14 +68,14 @@ const CashierOnboarding = ({
if (
typeof setSideNotes === 'function' &&
!is_switching &&
accounts_list.length > 0 &&
Object.keys(accounts).length > 0 &&
is_landing_company_loaded &&
is_currency_banner_visible
) {
setSideNotes([<CashierOnboardingSideNote key={0} is_crypto={is_crypto} />]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [is_switching, accounts_list, is_landing_company_loaded]);
}, [is_switching, accounts, is_landing_company_loaded]);

const openRealAccount = target => {
openRealAccountSignup('choose');
Expand Down Expand Up @@ -139,7 +149,7 @@ const CashierOnboarding = ({
return options;
};

if (is_switching || accounts_list.length === 0 || !is_landing_company_loaded)
if (is_switching || Object.keys(accounts).length === 0 || !is_landing_company_loaded)
return <Loading className='cashier-onboarding__loader' is_fullscreen />;

return (
Expand Down Expand Up @@ -189,7 +199,7 @@ const CashierOnboarding = ({
};

CashierOnboarding.propTypes = {
accounts_list: PropTypes.array,
accounts: PropTypes.object,
available_crypto_currencies: PropTypes.array,
can_change_fiat_currency: PropTypes.bool,
currency: PropTypes.string,
Expand All @@ -215,7 +225,7 @@ CashierOnboarding.propTypes = {
};

export default connect(({ client, common, modules, ui }) => ({
accounts_list: modules.cashier.account_transfer.accounts_list,
accounts: client.accounts,
available_crypto_currencies: client.available_crypto_currencies,
can_change_fiat_currency: client.can_change_fiat_currency,
currency: client.currency,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ describe('AccountPromptDialogStore', () => {
routeTo: jest.fn(),
},
client: {
accounts: {
CR90000001: {
is_virtual: 0,
currency: 'USD',
},
CR90000002: {
is_virtual: 0,
currency: 'BTC',
},
},
currency: 'BTC',
switchAccount: jest.fn(),
},
modules: {
cashier: {
account_transfer: {
accounts_list: [
{ currency: 'USD', is_crypto: false, text: 'USD', value: 'CR90000001' },
{ currency: 'BTC', is_crypto: true, text: 'BTC', value: 'CR90000002' },
],
},
general_store: {
setIsDeposit: jest.fn(),
},
Expand Down
19 changes: 12 additions & 7 deletions packages/cashier/src/stores/account-prompt-dialog-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,28 @@ export default class AccountPromptDialogStore {

@action.bound
async onConfirm() {
const { client, modules } = this.root_store;
const { accounts_list } = modules.cashier.account_transfer;
const { client } = this.root_store;

this.should_show = false;
this.is_confirmed = true;

const has_fiat_account = accounts_list.some(x => !x.is_crypto);
const has_fiat_account = Object.values(client.accounts).some(
acc_settings => !acc_settings.is_virtual && !isCryptocurrency(acc_settings.currency)
);
if (isCryptocurrency(client?.currency) && has_fiat_account) await this.doSwitch();
}

async doSwitch() {
const { client, modules } = this.root_store;
const { account_transfer, general_store } = modules.cashier;
const { general_store } = modules.cashier;

const non_crypto_accounts = account_transfer.accounts_list.filter(x => !x.is_crypto);
const loginid = non_crypto_accounts.map(x => x.value)[0];
await client.switchAccount(loginid);
const non_crypto_account_loginid = Object.entries(client.accounts).reduce(
(initial_value, [loginid, settings]) => {
return !settings.is_virtual && !isCryptocurrency(settings.currency) ? loginid : initial_value;
},
''
);
await client.switchAccount(non_crypto_account_loginid);

if (this.current_location === 'deposit') {
general_store.setIsDeposit(true);
Expand Down