Skip to content

Commit eae2f2f

Browse files
Merge pull request #166 from ahmed-deriv/ahmed/DAPI-775/fix--currency-on-account-switcher
Ahmed/dapi 775/fix currency on account switcher
2 parents 25f22ed + c607527 commit eae2f2f

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

src/components/AccountSwitcher/index.tsx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useRef } from 'react';
1+
import React, { useState, useRef, useCallback } from 'react';
22
import { InputDropdown } from '@deriv-com/quill-ui';
33
import { translate } from '@docusaurus/Translate';
44
import useAuthContext from '@site/src/hooks/useAuthContext';
@@ -15,10 +15,39 @@ interface AccountSwitcherProps {
1515
const AccountSwitcher = ({ onChange }: AccountSwitcherProps) => {
1616
const { onSelectAccount } = useAccountSelector();
1717
const [isToggleDropdown, setToggleDropdown] = useState(false);
18-
const { loginAccounts, currentLoginAccount } = useAuthContext();
18+
const {
19+
loginAccounts,
20+
currentLoginAccount,
21+
userAccounts,
22+
updateCurrentLoginAccount,
23+
updateLoginAccounts,
24+
} = useAuthContext();
1925
const dropdownRef = useRef(null);
2026
useOnClickOutside(dropdownRef, () => setToggleDropdown(false));
2127

28+
const handleLoginAccounts = useCallback(() => {
29+
const isNonCurrencyAccounts = loginAccounts.filter((account) => account.currency === '');
30+
if (isNonCurrencyAccounts.length > 0) {
31+
const updatedAccountList = loginAccounts.map((account) => {
32+
const userAccount = userAccounts.find(
33+
(userAccount) => userAccount.loginid === account.name,
34+
);
35+
if (userAccount) {
36+
const updatedAccountItem = { ...account, currency: userAccount.currency };
37+
if (currentLoginAccount.name === account.name)
38+
updateCurrentLoginAccount(updatedAccountItem, false);
39+
return updatedAccountItem;
40+
}
41+
return account;
42+
});
43+
updateLoginAccounts(updatedAccountList, false);
44+
}
45+
}, [userAccounts]);
46+
47+
React.useEffect(() => {
48+
handleLoginAccounts();
49+
}, [handleLoginAccounts]);
50+
2251
const options = loginAccounts.map((accountItem) => ({
2352
text: (
2453
<div

src/contexts/auth/auth.context.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ export interface IAuthContext {
1414
is_logged_in: boolean;
1515
is_authorized: boolean;
1616
loginAccounts: IUserLoginAccount[];
17-
updateLoginAccounts: (userAccounts: IUserLoginAccount[]) => void;
17+
updateLoginAccounts: (userAccounts: IUserLoginAccount[], updateCurrentAccount?: boolean) => void;
1818
currentLoginAccount: IUserLoginAccount;
19-
updateCurrentLoginAccount: (userAccount: IUserLoginAccount) => void;
19+
updateCurrentLoginAccount: (userAccount: IUserLoginAccount, isValidateAccount?: boolean) => void;
2020
userAccounts: IUserAccounts;
2121
user: IUser;
2222
}

src/contexts/auth/auth.provider.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
6666
}, [is_authorized, is_connected, updateAuthorize]);
6767

6868
const updateLoginAccounts = useCallback(
69-
(loginAccounts: IUserLoginAccount[]) => {
69+
(loginAccounts: IUserLoginAccount[], updateCurrentAccount = true) => {
70+
7071
setLoginAccounts(loginAccounts);
72+
if (!updateCurrentAccount) return;
73+
7174
if (loginAccounts.length) {
7275
const virtualAccount = findVirtualAccount(loginAccounts);
7376
if (virtualAccount) {
@@ -81,9 +84,11 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
8184
);
8285

8386
const updateCurrentLoginAccount = useCallback(
84-
(account: IUserLoginAccount) => {
85-
setIsAuthorized(false);
86-
setisSwitchingAccount(true);
87+
(account: IUserLoginAccount, isValidateAccount = true) => {
88+
if (isValidateAccount) {
89+
setIsAuthorized(false);
90+
setisSwitchingAccount(true);
91+
}
8792
setCurrentLoginAccount(account);
8893
},
8994
[setCurrentLoginAccount],

src/utils/__tests__/utils.test.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,33 +126,8 @@ describe('Get Accounts from Search Params', () => {
126126
},
127127
]);
128128
});
129-
130-
it('Should not create account object for malformed query params', () => {
131-
const test_search_params =
132-
'?acct1=CR111111&token1=first_token&cur1=USD&acct2=CR2222222&token2=second_token';
133-
const accounts = getAccountsFromSearchParams(test_search_params);
134-
expect(accounts.length).toBe(1);
135-
expect(accounts).not.toStrictEqual([
136-
{
137-
currency: 'USD',
138-
name: 'CR111111',
139-
token: 'first_token',
140-
},
141-
{
142-
currency: 'ETH',
143-
name: 'CR2222222',
144-
token: 'second_token',
145-
},
146-
]);
147-
expect(accounts).toStrictEqual([
148-
{
149-
currency: 'USD',
150-
name: 'CR111111',
151-
token: 'first_token',
152-
},
153-
]);
154-
});
155129
});
130+
156131
describe('Get Server Config', () => {
157132
afterEach(() => {
158133
jest.clearAllMocks();

src/utils/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,12 @@ export const getAccountsFromSearchParams = (searchParams: string) => {
120120
// we should check each account in the search params, this is some kind of validation for the URL search params
121121
if (
122122
params.has(`acct${queryIndex}`) &&
123-
params.has(`token${queryIndex}`) &&
124-
params.has(`cur${queryIndex}`)
123+
params.has(`token${queryIndex}`)
125124
) {
126125
accounts.push({
127126
name: params.get(`acct${queryIndex}`),
128127
token: params.get(`token${queryIndex}`),
129-
currency: params.get(`cur${queryIndex}`),
128+
currency: params.get(`cur${queryIndex}`) || '',
130129
});
131130
}
132131
}

0 commit comments

Comments
 (0)