forked from binary-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
thisyahlen/TRAH-2999/chore: cleanup and minor refactoring (binary-com…
…#13775) * chore: cleanup and minor refactoring * chore: komen * chore: komen v2
- Loading branch information
1 parent
708a427
commit ef7e2a5
Showing
8 changed files
with
91 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 4 additions & 12 deletions
16
packages/tradershub/src/components/TradersHubContent/TradersHubContent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useUIContext } from '@/components'; | ||
import { useActiveTradingAccount, useAuthorize, useTradingAccountsList } from '@deriv/api'; | ||
|
||
const accountTypes = [ | ||
{ label: 'Demo', value: 'demo' }, | ||
{ label: 'Real', value: 'real' }, | ||
] as const; | ||
|
||
type TAccountType = typeof accountTypes[number]; | ||
|
||
/** | ||
* @description This hook contains the logic that is used to switch between demo and real accounts | ||
* @returns {selected: {label: string, value: string}, selectAccount: (account: TAccount) => void} | ||
* @example | ||
* const { selected, selectAccount } = useAccountSwitcher(); | ||
* selectAccount({ label: 'Demo', value: 'demo' }); | ||
*/ | ||
const useAccountSwitcher = () => { | ||
const { data: tradingAccountsList } = useTradingAccountsList(); | ||
const { data: activeTradingAccount } = useActiveTradingAccount(); | ||
const { switchAccount } = useAuthorize(); | ||
const { setUIState } = useUIContext(); | ||
const activeAccountType = activeTradingAccount?.is_virtual ? 'demo' : 'real'; | ||
const activeType = accountTypes.find(account => account.value === activeAccountType); | ||
const [selectedAccount, setSelected] = useState(activeType); | ||
const firstRealLoginId = tradingAccountsList?.find(acc => !acc.is_virtual)?.loginid; | ||
const demoLoginId = tradingAccountsList?.find(acc => acc.is_virtual)?.loginid; | ||
|
||
useEffect(() => { | ||
if (activeType) { | ||
setSelected(activeType); | ||
setUIState({ | ||
accountType: activeAccountType, | ||
}); | ||
} | ||
}, [activeAccountType, activeType, setUIState]); | ||
|
||
const setSelectedAccount = useCallback( | ||
(account: TAccountType) => { | ||
setSelected(account); | ||
setUIState({ | ||
accountType: account.value, | ||
}); | ||
|
||
const loginId = account.value === 'demo' ? demoLoginId : firstRealLoginId; | ||
if (loginId) { | ||
switchAccount(loginId); | ||
} | ||
}, | ||
[demoLoginId, firstRealLoginId, setUIState, switchAccount] | ||
); | ||
|
||
return { | ||
// selected: {label: string, value: string} | ||
selectedAccount, | ||
// selectAccount: (account: TAccount) => void | ||
setSelectedAccount, | ||
// accountTypes: {label: Demo | Real, value: demo | real}[] | ||
accountTypes, | ||
}; | ||
}; | ||
|
||
export default useAccountSwitcher; |