11import { Dispatch , FC , SetStateAction , useEffect , useState } from 'react'
22import { toast } from 'react-toastify'
33import { KeyedMutator } from 'swr'
4- import { noop } from 'lodash'
54
6- import { Button , Collapsible , FormToggleSwitch , IconSolid , Tooltip } from '~/libs/ui'
5+ import { Button , Collapsible , FormToggleSwitch } from '~/libs/ui'
76import { diceIdLogo , MFAImage , SettingSection , triggerSurvey } from '~/apps/accounts/src/lib'
87import { MemberMFAStatus , updateMemberMFAStatusAsync , useMemberMFAStatus , UserProfile } from '~/libs/core'
98
@@ -23,10 +22,12 @@ const Security: FC<SecurityProps> = (props: SecurityProps) => {
2322 } = useMemberMFAStatus ( props . profile . userId )
2423
2524 const [ mfaEnabled , setMFAEnabled ] : [ boolean , Dispatch < SetStateAction < boolean > > ] = useState ( false )
25+ const [ diceEnabled , setDiceEnabled ] : [ boolean , Dispatch < SetStateAction < boolean > > ] = useState ( false )
2626
2727 useEffect ( ( ) => {
2828 if ( mfaStatusData ) {
2929 setMFAEnabled ( mfaStatusData . mfaEnabled )
30+ setDiceEnabled ( mfaStatusData . diceEnabled )
3031 }
3132 } , [ mfaStatusData ] )
3233
@@ -36,8 +37,9 @@ const Security: FC<SecurityProps> = (props: SecurityProps) => {
3637 mfaEnabled : ! mfaEnabled ,
3738 } ,
3839 } )
39- . then ( ( ) => {
40- setMFAEnabled ( ! mfaEnabled )
40+ . then ( response => {
41+ setMFAEnabled ( response . result . content . mfaEnabled )
42+ setDiceEnabled ( response . result . content . diceEnabled )
4143 toast . success ( 'Your Multi Factor Authentication (MFA) status was updated.' )
4244 triggerSurvey ( )
4345 } )
@@ -46,6 +48,27 @@ const Security: FC<SecurityProps> = (props: SecurityProps) => {
4648 } )
4749 }
4850
51+ function handleUserDiceChange ( ) : void {
52+ if ( ! diceEnabled ) {
53+ return
54+ }
55+
56+ updateMemberMFAStatusAsync ( props . profile . userId , {
57+ param : {
58+ diceEnabled : ! diceEnabled ,
59+ } ,
60+ } )
61+ . then ( response => {
62+ setMFAEnabled ( response . result . content . mfaEnabled )
63+ setDiceEnabled ( response . result . content . diceEnabled )
64+ toast . success ( 'Your DICE credential was disabled.' )
65+ triggerSurvey ( )
66+ } )
67+ . catch ( ( ) => {
68+ toast . error ( 'Something went wrong. Please try again later.' )
69+ } )
70+ }
71+
4972 function handleDiceModalStatus ( ) : void {
5073 setSetupDiceModalOpen ( ! setupDiceModalOpen )
5174 }
@@ -70,7 +93,6 @@ const Security: FC<SecurityProps> = (props: SecurityProps) => {
7093 name = 'mfaStatus'
7194 onChange = { handleUserMFAChange }
7295 value = { mfaEnabled }
73- disabled = { mfaStatusData ?. diceEnabled }
7496 />
7597 ) }
7698 />
@@ -86,12 +108,11 @@ const Security: FC<SecurityProps> = (props: SecurityProps) => {
86108 actionElement = { (
87109 < div className = { styles . diceBtnWrap } >
88110 {
89- mfaStatusData ?. diceEnabled ? (
111+ diceEnabled ? (
90112 < FormToggleSwitch
91113 name = 'diceEnabled'
92- onChange = { noop }
114+ onChange = { handleUserDiceChange }
93115 value = { mfaStatusData ?. diceEnabled as boolean }
94- disabled = { mfaStatusData ?. diceEnabled }
95116 />
96117 ) : (
97118 < Button
@@ -104,15 +125,6 @@ const Security: FC<SecurityProps> = (props: SecurityProps) => {
104125 />
105126 )
106127 }
107- {
108- mfaStatusData ?. diceEnabled && (
109- < Tooltip
110- content = 'Please reach out to support@topcoder.com for deactivating Dice ID.'
111- >
112- < IconSolid . InformationCircleIcon />
113- </ Tooltip >
114- )
115- }
116128 </ div >
117129 ) }
118130 />
0 commit comments