-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[CP Staging] add validate code modal #48628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f9a20e3
add validate code modal
hungvu193 778c68b
replace ValidateContactActionPage
hungvu193 4582516
remove console.log
hungvu193 541f282
add ValidateCodeModal into ExpensifyCardPage
hungvu193 ae40d6a
lint
hungvu193 2cc3b95
update translation
hungvu193 5022fa8
update contact method translations for NewContactMethodPage
hungvu193 35d064c
update docs
hungvu193 7578c92
merge main
hungvu193 cdcd287
Update src/components/ValidateCodeActionModal/type.ts
hungvu193 3915b4b
Update src/components/ValidateCodeActionModal/type.ts
hungvu193 e1d133b
update js docs and remove FullScreenLoadingIndicator
hungvu193 d88b0c3
Merge branch 'feat/validate-code-modal' of https://github.com/hungvu1…
hungvu193 5296333
Merge remote-tracking branch 'origin/main' into feat/validate-code-modal
hungvu193 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
257 changes: 257 additions & 0 deletions
257
src/components/ValidateCodeActionModal/ValidateCodeForm/BaseValidateCodeForm.tsx
This file contains hidden or 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,257 @@ | ||
import {useFocusEffect} from '@react-navigation/native'; | ||
import type {ForwardedRef} from 'react'; | ||
import React, {useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react'; | ||
import {View} from 'react-native'; | ||
import type {OnyxEntry} from 'react-native-onyx'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import Button from '@components/Button'; | ||
import DotIndicatorMessage from '@components/DotIndicatorMessage'; | ||
import MagicCodeInput from '@components/MagicCodeInput'; | ||
import type {AutoCompleteVariant, MagicCodeInputHandle} from '@components/MagicCodeInput'; | ||
import OfflineWithFeedback from '@components/OfflineWithFeedback'; | ||
import PressableWithFeedback from '@components/Pressable/PressableWithFeedback'; | ||
import Text from '@components/Text'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useNetwork from '@hooks/useNetwork'; | ||
import useStyleUtils from '@hooks/useStyleUtils'; | ||
import useTheme from '@hooks/useTheme'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import * as ErrorUtils from '@libs/ErrorUtils'; | ||
import * as ValidationUtils from '@libs/ValidationUtils'; | ||
import * as User from '@userActions/User'; | ||
import CONST from '@src/CONST'; | ||
import type {TranslationPaths} from '@src/languages/types'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type {Account, ValidateMagicCodeAction} from '@src/types/onyx'; | ||
import type {Errors, PendingAction} from '@src/types/onyx/OnyxCommon'; | ||
import {isEmptyObject} from '@src/types/utils/EmptyObject'; | ||
|
||
type ValidateCodeFormHandle = { | ||
focus: () => void; | ||
focusLastSelected: () => void; | ||
}; | ||
|
||
type ValidateCodeFormError = { | ||
validateCode?: TranslationPaths; | ||
}; | ||
|
||
type BaseValidateCodeFormOnyxProps = { | ||
/** The details about the account that the user is signing in with */ | ||
account: OnyxEntry<Account>; | ||
}; | ||
|
||
type ValidateCodeFormProps = { | ||
/** If the magic code has been resent previously */ | ||
hasMagicCodeBeenSent?: boolean; | ||
|
||
/** Specifies autocomplete hints for the system, so it can provide autofill */ | ||
autoComplete?: AutoCompleteVariant; | ||
|
||
/** Forwarded inner ref */ | ||
innerRef?: ForwardedRef<ValidateCodeFormHandle>; | ||
|
||
/** The state of magic code that being sent */ | ||
validateCodeAction?: ValidateMagicCodeAction; | ||
|
||
/** The pending action for submitting form */ | ||
validatePendingAction?: PendingAction | null; | ||
|
||
/** The error of submitting */ | ||
validateError?: Errors; | ||
|
||
/** Function is called when submitting form */ | ||
handleSubmitForm: (validateCode: string) => void; | ||
|
||
/** Function to clear error of the form */ | ||
clearError: () => void; | ||
}; | ||
|
||
type BaseValidateCodeFormProps = BaseValidateCodeFormOnyxProps & ValidateCodeFormProps; | ||
|
||
function BaseValidateCodeForm({ | ||
account = {}, | ||
hasMagicCodeBeenSent, | ||
autoComplete = 'one-time-code', | ||
innerRef = () => {}, | ||
validateCodeAction, | ||
validatePendingAction, | ||
validateError, | ||
handleSubmitForm, | ||
clearError, | ||
}: BaseValidateCodeFormProps) { | ||
const {translate} = useLocalize(); | ||
const {isOffline} = useNetwork(); | ||
const theme = useTheme(); | ||
const styles = useThemeStyles(); | ||
const StyleUtils = useStyleUtils(); | ||
const [formError, setFormError] = useState<ValidateCodeFormError>({}); | ||
const [validateCode, setValidateCode] = useState(''); | ||
const inputValidateCodeRef = useRef<MagicCodeInputHandle>(null); | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- nullish coalescing doesn't achieve the same result in this case | ||
const shouldDisableResendValidateCode = !!isOffline || account?.isLoading; | ||
const focusTimeoutRef = useRef<NodeJS.Timeout | null>(null); | ||
|
||
useImperativeHandle(innerRef, () => ({ | ||
focus() { | ||
inputValidateCodeRef.current?.focus(); | ||
}, | ||
focusLastSelected() { | ||
if (!inputValidateCodeRef.current) { | ||
return; | ||
} | ||
if (focusTimeoutRef.current) { | ||
clearTimeout(focusTimeoutRef.current); | ||
} | ||
focusTimeoutRef.current = setTimeout(() => { | ||
inputValidateCodeRef.current?.focusLastSelected(); | ||
}, CONST.ANIMATED_TRANSITION); | ||
}, | ||
})); | ||
|
||
useFocusEffect( | ||
useCallback(() => { | ||
if (!inputValidateCodeRef.current) { | ||
return; | ||
} | ||
if (focusTimeoutRef.current) { | ||
clearTimeout(focusTimeoutRef.current); | ||
} | ||
focusTimeoutRef.current = setTimeout(() => { | ||
inputValidateCodeRef.current?.focusLastSelected(); | ||
}, CONST.ANIMATED_TRANSITION); | ||
return () => { | ||
if (!focusTimeoutRef.current) { | ||
return; | ||
} | ||
clearTimeout(focusTimeoutRef.current); | ||
}; | ||
}, []), | ||
); | ||
|
||
useEffect(() => { | ||
if (!validateError) { | ||
return; | ||
} | ||
clearError(); | ||
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps | ||
}, [clearError, validateError]); | ||
|
||
useEffect(() => { | ||
if (!hasMagicCodeBeenSent) { | ||
return; | ||
} | ||
inputValidateCodeRef.current?.clear(); | ||
}, [hasMagicCodeBeenSent]); | ||
|
||
/** | ||
* Request a validate code / magic code be sent to verify this contact method | ||
*/ | ||
const resendValidateCode = () => { | ||
User.requestValidateCodeAction(); | ||
inputValidateCodeRef.current?.clear(); | ||
}; | ||
|
||
/** | ||
* Handle text input and clear formError upon text change | ||
*/ | ||
const onTextInput = useCallback( | ||
(text: string) => { | ||
setValidateCode(text); | ||
setFormError({}); | ||
|
||
if (validateError) { | ||
clearError(); | ||
User.clearValidateCodeActionError('actionVerified'); | ||
} | ||
}, | ||
[validateError, clearError], | ||
); | ||
|
||
/** | ||
* Check that all the form fields are valid, then trigger the submit callback | ||
*/ | ||
const validateAndSubmitForm = useCallback(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coming from #54009, we should show the error when |
||
if (!validateCode.trim()) { | ||
setFormError({validateCode: 'validateCodeForm.error.pleaseFillMagicCode'}); | ||
return; | ||
} | ||
|
||
if (!ValidationUtils.isValidValidateCode(validateCode)) { | ||
setFormError({validateCode: 'validateCodeForm.error.incorrectMagicCode'}); | ||
return; | ||
} | ||
|
||
setFormError({}); | ||
handleSubmitForm(validateCode); | ||
}, [validateCode, handleSubmitForm]); | ||
|
||
return ( | ||
<> | ||
<MagicCodeInput | ||
autoComplete={autoComplete} | ||
ref={inputValidateCodeRef} | ||
name="validateCode" | ||
value={validateCode} | ||
onChangeText={onTextInput} | ||
errorText={formError?.validateCode ? translate(formError?.validateCode) : ErrorUtils.getLatestErrorMessage(account ?? {})} | ||
hasError={!isEmptyObject(validateError)} | ||
onFulfill={validateAndSubmitForm} | ||
autoFocus={false} | ||
/> | ||
<OfflineWithFeedback | ||
pendingAction={validateCodeAction?.pendingFields?.validateCodeSent} | ||
errors={ErrorUtils.getLatestErrorField(validateCodeAction, 'actionVerified')} | ||
errorRowStyles={[styles.mt2]} | ||
onClose={() => User.clearValidateCodeActionError('actionVerified')} | ||
> | ||
<View style={[styles.mt2, styles.dFlex, styles.flexColumn, styles.alignItemsStart]}> | ||
<PressableWithFeedback | ||
disabled={shouldDisableResendValidateCode} | ||
style={[styles.mr1]} | ||
onPress={resendValidateCode} | ||
underlayColor={theme.componentBG} | ||
hoverDimmingValue={1} | ||
pressDimmingValue={0.2} | ||
role={CONST.ROLE.BUTTON} | ||
accessibilityLabel={translate('validateCodeForm.magicCodeNotReceived')} | ||
> | ||
<Text style={[StyleUtils.getDisabledLinkStyles(shouldDisableResendValidateCode)]}>{translate('validateCodeForm.magicCodeNotReceived')}</Text> | ||
</PressableWithFeedback> | ||
{hasMagicCodeBeenSent && ( | ||
<DotIndicatorMessage | ||
type="success" | ||
style={[styles.mt6, styles.flex0]} | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
messages={{0: translate('validateCodeModal.successfulNewCodeRequest')}} | ||
/> | ||
)} | ||
</View> | ||
</OfflineWithFeedback> | ||
<OfflineWithFeedback | ||
pendingAction={validatePendingAction} | ||
errors={validateError} | ||
errorRowStyles={[styles.mt2]} | ||
onClose={() => clearError()} | ||
> | ||
<Button | ||
isDisabled={isOffline} | ||
text={translate('common.verify')} | ||
onPress={validateAndSubmitForm} | ||
style={[styles.mt4]} | ||
success | ||
pressOnEnter | ||
large | ||
isLoading={account?.isLoading} | ||
/> | ||
</OfflineWithFeedback> | ||
</> | ||
); | ||
} | ||
|
||
BaseValidateCodeForm.displayName = 'BaseValidateCodeForm'; | ||
|
||
export type {ValidateCodeFormProps, ValidateCodeFormHandle}; | ||
|
||
export default withOnyx<BaseValidateCodeFormProps, BaseValidateCodeFormOnyxProps>({ | ||
account: {key: ONYXKEYS.ACCOUNT}, | ||
})(BaseValidateCodeForm); |
14 changes: 14 additions & 0 deletions
14
src/components/ValidateCodeActionModal/ValidateCodeForm/index.android.tsx
This file contains hidden or 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,14 @@ | ||
import React, {forwardRef} from 'react'; | ||
import BaseValidateCodeForm from './BaseValidateCodeForm'; | ||
import type {ValidateCodeFormHandle, ValidateCodeFormProps} from './BaseValidateCodeForm'; | ||
|
||
const ValidateCodeForm = forwardRef<ValidateCodeFormHandle, ValidateCodeFormProps>((props, ref) => ( | ||
<BaseValidateCodeForm | ||
autoComplete="sms-otp" | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
innerRef={ref} | ||
/> | ||
)); | ||
|
||
export default ValidateCodeForm; |
14 changes: 14 additions & 0 deletions
14
src/components/ValidateCodeActionModal/ValidateCodeForm/index.tsx
This file contains hidden or 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,14 @@ | ||
import React, {forwardRef} from 'react'; | ||
import BaseValidateCodeForm from './BaseValidateCodeForm'; | ||
import type {ValidateCodeFormHandle, ValidateCodeFormProps} from './BaseValidateCodeForm'; | ||
|
||
const ValidateCodeForm = forwardRef<ValidateCodeFormHandle, ValidateCodeFormProps>((props, ref) => ( | ||
<BaseValidateCodeForm | ||
autoComplete="one-time-code" | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
innerRef={ref} | ||
/> | ||
)); | ||
|
||
export default ValidateCodeForm; |
This file contains hidden or 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,79 @@ | ||
import React, {useCallback, useEffect, useRef} from 'react'; | ||
import {View} from 'react-native'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; | ||
import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
import Modal from '@components/Modal'; | ||
import ScreenWrapper from '@components/ScreenWrapper'; | ||
import Text from '@components/Text'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import * as User from '@libs/actions/User'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type {ValidateCodeActionModalProps} from './type'; | ||
import ValidateCodeForm from './ValidateCodeForm'; | ||
import type {ValidateCodeFormHandle} from './ValidateCodeForm/BaseValidateCodeForm'; | ||
|
||
function ValidateCodeActionModal({isVisible, title, description, onClose, validatePendingAction, validateError, handleSubmitForm, clearError}: ValidateCodeActionModalProps) { | ||
const themeStyles = useThemeStyles(); | ||
const firstRenderRef = useRef(true); | ||
const validateCodeFormRef = useRef<ValidateCodeFormHandle>(null); | ||
|
||
const [validateCodeAction] = useOnyx(ONYXKEYS.VALIDATE_ACTION_CODE); | ||
|
||
const hide = useCallback(() => { | ||
clearError(); | ||
onClose(); | ||
}, [onClose, clearError]); | ||
|
||
useEffect(() => { | ||
if (!firstRenderRef.current || !isVisible) { | ||
return; | ||
} | ||
firstRenderRef.current = false; | ||
User.requestValidateCodeAction(); | ||
}, [isVisible]); | ||
|
||
return ( | ||
<Modal | ||
type={CONST.MODAL.MODAL_TYPE.RIGHT_DOCKED} | ||
isVisible={isVisible} | ||
onClose={hide} | ||
onModalHide={hide} | ||
hideModalContentWhileAnimating | ||
useNativeDriver | ||
shouldUseModalPaddingStyle={false} | ||
> | ||
<ScreenWrapper | ||
includeSafeAreaPaddingBottom={false} | ||
shouldEnableMaxHeight | ||
testID={ValidateCodeActionModal.displayName} | ||
offlineIndicatorStyle={themeStyles.mtAuto} | ||
> | ||
<HeaderWithBackButton | ||
title={title} | ||
onBackButtonPress={hide} | ||
/> | ||
{validateCodeAction?.isLoading ? ( | ||
hungvu193 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<FullScreenLoadingIndicator style={[themeStyles.flex1, themeStyles.pRelative]} /> | ||
) : ( | ||
<View style={[themeStyles.ph5, themeStyles.mt3, themeStyles.mb7]}> | ||
<Text style={[themeStyles.mb3]}>{description}</Text> | ||
<ValidateCodeForm | ||
validateCodeAction={validateCodeAction} | ||
validatePendingAction={validatePendingAction} | ||
validateError={validateError} | ||
handleSubmitForm={handleSubmitForm} | ||
clearError={clearError} | ||
ref={validateCodeFormRef} | ||
/> | ||
</View> | ||
)} | ||
</ScreenWrapper> | ||
</Modal> | ||
); | ||
} | ||
|
||
ValidateCodeActionModal.displayName = 'ValidateCodeActionModal'; | ||
|
||
export default ValidateCodeActionModal; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have also cleared this when user has
actionVerified
error as improvement. #55490