From 571afcaab8c6489dea490569a98e51447dcd62c7 Mon Sep 17 00:00:00 2001 From: Alex Junior Date: Tue, 22 Mar 2022 10:44:07 -0300 Subject: [PATCH] Chore: Clean TwoFactor - Typescript (#3912) * chore: clean TwoFactor * minor tweak * minor refactor * chore: removing console log --- app/containers/TwoFactor/index.tsx | 62 +++++++++++++++++------------- app/views/RoomView/JoinCode.tsx | 3 +- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/app/containers/TwoFactor/index.tsx b/app/containers/TwoFactor/index.tsx index 5bb27774c..200095b47 100644 --- a/app/containers/TwoFactor/index.tsx +++ b/app/containers/TwoFactor/index.tsx @@ -9,21 +9,36 @@ import { connect } from 'react-redux'; import TextInput from '../TextInput'; import I18n from '../../i18n'; import EventEmitter from '../../utils/events'; -import { withTheme } from '../../theme'; +import { useTheme } from '../../theme'; import { themes } from '../../constants/colors'; import Button from '../Button'; import sharedStyles from '../../views/Styles'; import RocketChat from '../../lib/rocketchat'; import styles from './styles'; +import { IApplicationState } from '../../definitions'; export const TWO_FACTOR = 'TWO_FACTOR'; -interface ITwoFactor { - theme?: string; - isMasterDetail: boolean; +interface IMethodsProp { + text: string; + keyboardType: 'numeric' | 'default'; + title?: string; + secureTextEntry?: boolean; +} +interface IMethods { + totp: IMethodsProp; + email: IMethodsProp; + password: IMethodsProp; } -const methods: any = { +interface EventListenerMethod { + method?: keyof IMethods; + submit?: (param: string) => void; + cancel?: () => void; + invalid?: boolean; +} + +const methods: IMethods = { totp: { text: 'Open_your_authentication_app_and_enter_the_code', keyboardType: 'numeric' @@ -40,14 +55,14 @@ const methods: any = { } }; -const TwoFactor = React.memo(({ theme, isMasterDetail }: ITwoFactor) => { +const TwoFactor = React.memo(({ isMasterDetail }: { isMasterDetail: boolean }) => { + const { theme } = useTheme(); const [visible, setVisible] = useState(false); - const [data, setData] = useState({}); - const [code, setCode] = useState(''); + const [data, setData] = useState({}); + const [code, setCode] = useState(''); - const method = methods[data.method]; + const method = data.method ? methods[data.method] : null; const isEmail = data.method === 'email'; - const sendEmail = () => RocketChat.sendEmailCode(); useDeepCompareEffect(() => { @@ -59,7 +74,7 @@ const TwoFactor = React.memo(({ theme, isMasterDetail }: ITwoFactor) => { } }, [data]); - const showTwoFactor = (args: any) => setData(args); + const showTwoFactor = (args: EventListenerMethod) => setData(args); useEffect(() => { const listener = EventEmitter.addEventListener(TWO_FACTOR, showTwoFactor); @@ -87,26 +102,19 @@ const TwoFactor = React.memo(({ theme, isMasterDetail }: ITwoFactor) => { setData({}); }; - const color = themes[theme!].titleText; + const color = themes[theme].titleText; return ( - + {I18n.t(method?.title || 'Two_Factor_Authentication')} {method?.text ? {I18n.t(method.text)} : null} InteractionManager.runAfterInteractions(() => e?.getNativeRef()?.focus())} @@ -116,19 +124,19 @@ const TwoFactor = React.memo(({ theme, isMasterDetail }: ITwoFactor) => { onSubmitEditing={onSubmit} keyboardType={method?.keyboardType} secureTextEntry={method?.secureTextEntry} - error={data.invalid && { error: 'totp-invalid', reason: I18n.t('Code_or_password_invalid') }} + error={data.invalid ? { error: 'totp-invalid', reason: I18n.t('Code_or_password_invalid') } : undefined} testID='two-factor-input' /> - {isEmail && ( + {isEmail ? ( {I18n.t('Send_me_the_code_again')} - )} + ) : null}