Skip to content

Commit

Permalink
chore: switching logic in real demo dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
thisyahlen-deriv committed Jan 5, 2024
1 parent 38956f9 commit a04979c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/api/src/hooks/useTotalAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ const useTotalAssets = () => {

const demoMT5AccountBalance = cfdAccount?.mt5.find(account => account.is_virtual)?.converted_balance ?? 0;

const realMT5AccountBalance = cfdAccount?.mt5.reduce((total, account) => total + account.converted_balance, 0) ?? 0;
const realMT5AccountBalance =
cfdAccount?.mt5
.filter(account => !account.is_virtual)
.reduce((total, account) => total + account.converted_balance, 0) ?? 0;

const demoDxtradeAccountBalance = cfdAccount?.dxtrade.find(account => account.is_virtual)?.converted_balance ?? 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { useCallback, useEffect, useState } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { useOnClickOutside } from 'usehooks-ts';
import { useActiveTradingAccount, useAuthorize, useTradingAccountsList } from '@deriv/api';
import { Button, qtMerge, Text } from '@deriv/quill-design';
import { LabelPairedChevronDownSmRegularIcon } from '@deriv/quill-icons';

Expand All @@ -13,10 +15,24 @@ const accountTypes = [
];

const DemoRealSwitcher = () => {
const { data: tradingAccountsList } = useTradingAccountsList();
const { data: activeTradingAccount } = useActiveTradingAccount();
const { switchAccount } = useAuthorize();
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const [selected, setSelected] = useState(accountTypes[0]);
const { label, value } = selected;

const ref = useRef(null);
useOnClickOutside(ref, () => setIsDropdownOpen(false));

useEffect(() => {
const activeAccountType = activeTradingAccount?.is_virtual ? 'demo' : 'real';
const activeAccount = accountTypes.find(account => account.value === activeAccountType);
if (activeAccount) {
setSelected(activeAccount);
}
}, [activeTradingAccount]);

useEffect(() => {
setIsDropdownOpen(false);
}, [selected]);
Expand All @@ -25,12 +41,21 @@ const DemoRealSwitcher = () => {
setIsDropdownOpen(prevState => !prevState);
}, []);

const selectAccount = useCallback((account: TAccount) => {
const firstRealLoginId = tradingAccountsList?.find(acc => !acc.is_virtual)?.loginid;

const demoLoginId = tradingAccountsList?.find(acc => acc.is_virtual)?.loginid;

const selectAccount = (account: TAccount) => {
setSelected(account);
}, []);

const loginId = account.value === 'demo' ? demoLoginId : firstRealLoginId;
if (loginId) {
switchAccount(loginId);
}
};

return (
<div className='relative inline-block w-auto'>
<div className='relative inline-block w-auto' ref={ref}>
<Button
className={qtMerge(
'cursor-pointer w-full py-[3px] px-400 border-75 rounded-200 [&>span]:flex [&>span]:items-center [&>span]:text-[14px]',
Expand All @@ -54,11 +79,11 @@ const DemoRealSwitcher = () => {
/>
</Button>
{isDropdownOpen && (
<div className='absolute top-1400 z-10 rounded-200 bg-system-light-primary-background shadow-320 w-full'>
<div className='absolute z-10 w-full top-1400 rounded-200 bg-system-light-primary-background shadow-320'>
{accountTypes.map(account => (
<div
className={qtMerge(
'cursor-pointer hover:bg-system-light-hover-background',
'cursor-pointer hover:bg-system-light-hover-background rounded-200',
account.value === value && 'bg-system-light-active-background'
)}
key={account.value}
Expand All @@ -70,7 +95,7 @@ const DemoRealSwitcher = () => {
}}
role='button'
>
<Text bold={account.value === value} className='px-800 py-300 text-center' size='sm'>
<Text bold={account.value === value} className='text-center px-800 py-300' size='sm'>
{account.label}
</Text>
</div>
Expand Down

0 comments on commit a04979c

Please sign in to comment.