-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
thisyahlen/chore: use ui library for tabs (#13182)
* chore: use ui library for tabs * chore: remove test stuff * fix: latest * chore: update logic * chore: komen
- Loading branch information
1 parent
9ff800e
commit 0be857a
Showing
8 changed files
with
109 additions
and
91 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
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
28 changes: 18 additions & 10 deletions
28
packages/tradershub/src/components/RegulationSwitcher/RegulationSwitcherMobile.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useEffect } from 'react'; | ||
import { useActiveTradingAccount, useAuthorize, useTradingAccountsList } from '@deriv/api'; | ||
import { useUIContext } from '../components'; | ||
import { Regulation } from '../constants/constants'; | ||
import useRegulationFlags from './useRegulationFlags'; | ||
|
||
/** | ||
* @description This hook contains the logic that is used to switch between EU and non-EU accounts | ||
* @returns {buttons: {label: string}[], handleButtonClick: (label: string) => void} | ||
* @example | ||
* const { buttons, handleButtonClick } = useRegulationSwitcher(); | ||
*/ | ||
export const useRegulationSwitcher = () => { | ||
const { switchAccount } = useAuthorize(); | ||
const { data: tradingAccountsList } = useTradingAccountsList(); | ||
const { getUIState, setUIState } = useUIContext(); | ||
|
||
const currentRegulation = getUIState('regulation'); | ||
const accountType = getUIState('accountType'); | ||
const { isEU, isHighRisk } = useRegulationFlags(currentRegulation, accountType); | ||
|
||
const realCRAccount = tradingAccountsList?.find(account => account.loginid.startsWith('CR'))?.loginid ?? ''; | ||
const realMFAccount = tradingAccountsList?.find(account => account.loginid.startsWith('MF'))?.loginid ?? ''; | ||
|
||
const { data: activeTrading } = useActiveTradingAccount(); | ||
|
||
const buttons = [{ label: Regulation.NonEU }, { label: Regulation.EU }]; | ||
|
||
const handleButtonClick = (label: string) => { | ||
if (label !== currentRegulation) { | ||
if (label === Regulation.NonEU) { | ||
setUIState('regulation', Regulation.NonEU); | ||
if (realCRAccount) { | ||
switchAccount(realCRAccount); | ||
} | ||
} else { | ||
setUIState('regulation', Regulation.EU); | ||
if (realMFAccount) { | ||
switchAccount(realMFAccount); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (activeTrading?.loginid.startsWith('CR') || isHighRisk) { | ||
setUIState('regulation', Regulation.NonEU); | ||
} else if (activeTrading?.loginid.startsWith('MF') || isEU) { | ||
setUIState('regulation', Regulation.EU); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return { | ||
// Contains the array of buttons to be rendered in the switcher E.g. [{label: 'EU'}, {label: 'Non-EU'}] | ||
buttons, | ||
// Contains the function to be called when a button is clicked and to update the state E.g. (label: string) => void | ||
handleButtonClick, | ||
}; | ||
}; |
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