Skip to content

Commit

Permalink
Merge pull request #166 from ahmed-deriv/ahmed/DAPI-775/fix--currency…
Browse files Browse the repository at this point in the history
…-on-account-switcher

Ahmed/dapi 775/fix  currency on account switcher
  • Loading branch information
sandeep-deriv authored Nov 8, 2024
2 parents 25f22ed + c607527 commit eae2f2f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
33 changes: 31 additions & 2 deletions src/components/AccountSwitcher/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useRef } from 'react';
import React, { useState, useRef, useCallback } from 'react';
import { InputDropdown } from '@deriv-com/quill-ui';
import { translate } from '@docusaurus/Translate';
import useAuthContext from '@site/src/hooks/useAuthContext';
Expand All @@ -15,10 +15,39 @@ interface AccountSwitcherProps {
const AccountSwitcher = ({ onChange }: AccountSwitcherProps) => {
const { onSelectAccount } = useAccountSelector();
const [isToggleDropdown, setToggleDropdown] = useState(false);
const { loginAccounts, currentLoginAccount } = useAuthContext();
const {
loginAccounts,
currentLoginAccount,
userAccounts,
updateCurrentLoginAccount,
updateLoginAccounts,
} = useAuthContext();
const dropdownRef = useRef(null);
useOnClickOutside(dropdownRef, () => setToggleDropdown(false));

const handleLoginAccounts = useCallback(() => {
const isNonCurrencyAccounts = loginAccounts.filter((account) => account.currency === '');
if (isNonCurrencyAccounts.length > 0) {
const updatedAccountList = loginAccounts.map((account) => {
const userAccount = userAccounts.find(
(userAccount) => userAccount.loginid === account.name,
);
if (userAccount) {
const updatedAccountItem = { ...account, currency: userAccount.currency };
if (currentLoginAccount.name === account.name)
updateCurrentLoginAccount(updatedAccountItem, false);
return updatedAccountItem;
}
return account;
});
updateLoginAccounts(updatedAccountList, false);
}
}, [userAccounts]);

React.useEffect(() => {
handleLoginAccounts();
}, [handleLoginAccounts]);

const options = loginAccounts.map((accountItem) => ({
text: (
<div
Expand Down
4 changes: 2 additions & 2 deletions src/contexts/auth/auth.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export interface IAuthContext {
is_logged_in: boolean;
is_authorized: boolean;
loginAccounts: IUserLoginAccount[];
updateLoginAccounts: (userAccounts: IUserLoginAccount[]) => void;
updateLoginAccounts: (userAccounts: IUserLoginAccount[], updateCurrentAccount?: boolean) => void;
currentLoginAccount: IUserLoginAccount;
updateCurrentLoginAccount: (userAccount: IUserLoginAccount) => void;
updateCurrentLoginAccount: (userAccount: IUserLoginAccount, isValidateAccount?: boolean) => void;
userAccounts: IUserAccounts;
user: IUser;
}
Expand Down
13 changes: 9 additions & 4 deletions src/contexts/auth/auth.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
}, [is_authorized, is_connected, updateAuthorize]);

const updateLoginAccounts = useCallback(
(loginAccounts: IUserLoginAccount[]) => {
(loginAccounts: IUserLoginAccount[], updateCurrentAccount = true) => {

setLoginAccounts(loginAccounts);
if (!updateCurrentAccount) return;

if (loginAccounts.length) {
const virtualAccount = findVirtualAccount(loginAccounts);
if (virtualAccount) {
Expand All @@ -81,9 +84,11 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
);

const updateCurrentLoginAccount = useCallback(
(account: IUserLoginAccount) => {
setIsAuthorized(false);
setisSwitchingAccount(true);
(account: IUserLoginAccount, isValidateAccount = true) => {
if (isValidateAccount) {
setIsAuthorized(false);
setisSwitchingAccount(true);
}
setCurrentLoginAccount(account);
},
[setCurrentLoginAccount],
Expand Down
27 changes: 1 addition & 26 deletions src/utils/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,33 +126,8 @@ describe('Get Accounts from Search Params', () => {
},
]);
});

it('Should not create account object for malformed query params', () => {
const test_search_params =
'?acct1=CR111111&token1=first_token&cur1=USD&acct2=CR2222222&token2=second_token';
const accounts = getAccountsFromSearchParams(test_search_params);
expect(accounts.length).toBe(1);
expect(accounts).not.toStrictEqual([
{
currency: 'USD',
name: 'CR111111',
token: 'first_token',
},
{
currency: 'ETH',
name: 'CR2222222',
token: 'second_token',
},
]);
expect(accounts).toStrictEqual([
{
currency: 'USD',
name: 'CR111111',
token: 'first_token',
},
]);
});
});

describe('Get Server Config', () => {
afterEach(() => {
jest.clearAllMocks();
Expand Down
5 changes: 2 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,12 @@ export const getAccountsFromSearchParams = (searchParams: string) => {
// we should check each account in the search params, this is some kind of validation for the URL search params
if (
params.has(`acct${queryIndex}`) &&
params.has(`token${queryIndex}`) &&
params.has(`cur${queryIndex}`)
params.has(`token${queryIndex}`)
) {
accounts.push({
name: params.get(`acct${queryIndex}`),
token: params.get(`token${queryIndex}`),
currency: params.get(`cur${queryIndex}`),
currency: params.get(`cur${queryIndex}`) || '',
});
}
}
Expand Down

0 comments on commit eae2f2f

Please sign in to comment.