-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[TS migration] Migrate 'Onfido' component to TypeScript #31378
Changes from all commits
6b6afd8
4ce8c30
cb7bfac
b7da355
d8d2ac4
8d82261
3794485
686e637
0993df9
b1d36a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,17 +1,28 @@ | ||||||||
import lodashGet from 'lodash/get'; | ||||||||
import {Onfido as OnfidoSDK} from 'onfido-sdk-ui'; | ||||||||
import React, {forwardRef, useEffect} from 'react'; | ||||||||
import _ from 'underscore'; | ||||||||
import type {ForwardedRef} from 'react'; | ||||||||
import type {LocaleContextProps} from '@components/LocaleContextProvider'; | ||||||||
import useLocalize from '@hooks/useLocalize'; | ||||||||
import useTheme from '@hooks/useTheme'; | ||||||||
import Log from '@libs/Log'; | ||||||||
import type {ThemeColors} from '@styles/theme/types'; | ||||||||
import FontUtils from '@styles/utils/FontUtils'; | ||||||||
import variables from '@styles/variables'; | ||||||||
import CONST from '@src/CONST'; | ||||||||
import {isEmptyObject} from '@src/types/utils/EmptyObject'; | ||||||||
import './index.css'; | ||||||||
import onfidoPropTypes from './onfidoPropTypes'; | ||||||||
import type {OnfidoElement, OnfidoProps} from './types'; | ||||||||
|
||||||||
function initializeOnfido({sdkToken, onSuccess, onError, onUserExit, preferredLocale, translate, theme}) { | ||||||||
type InitializeOnfidoProps = OnfidoProps & | ||||||||
Pick<LocaleContextProps, 'translate' | 'preferredLocale'> & { | ||||||||
theme: ThemeColors; | ||||||||
}; | ||||||||
|
||||||||
type OnfidoEvent = Event & { | ||||||||
detail?: Record<string, unknown>; | ||||||||
}; | ||||||||
|
||||||||
function initializeOnfido({sdkToken, onSuccess, onError, onUserExit, preferredLocale, translate, theme}: InitializeOnfidoProps) { | ||||||||
OnfidoSDK.init({ | ||||||||
token: sdkToken, | ||||||||
containerId: CONST.ONFIDO.CONTAINER_ID, | ||||||||
|
@@ -20,7 +31,7 @@ function initializeOnfido({sdkToken, onSuccess, onError, onUserExit, preferredLo | |||||||
fontFamilySubtitle: `${FontUtils.fontFamily.platform.EXP_NEUE}, -apple-system, serif`, | ||||||||
fontFamilyBody: `${FontUtils.fontFamily.platform.EXP_NEUE}, -apple-system, serif`, | ||||||||
fontSizeTitle: `${variables.fontSizeLarge}px`, | ||||||||
fontWeightTitle: FontUtils.fontWeight.bold, | ||||||||
fontWeightTitle: Number(FontUtils.fontWeight.bold), | ||||||||
fontWeightSubtitle: 400, | ||||||||
fontSizeSubtitle: `${variables.fontSizeNormal}px`, | ||||||||
colorContentTitle: theme.text, | ||||||||
|
@@ -45,10 +56,10 @@ function initializeOnfido({sdkToken, onSuccess, onError, onUserExit, preferredLo | |||||||
colorBorderLinkUnderline: theme.link, | ||||||||
colorBackgroundLinkHover: theme.link, | ||||||||
colorBackgroundLinkActive: theme.link, | ||||||||
authAccentColor: theme.link, | ||||||||
colorBackgroundInfoPill: theme.link, | ||||||||
colorBackgroundSelector: theme.appBG, | ||||||||
colorBackgroundDocTypeButton: theme.success, | ||||||||
borderWidthSurfaceModal: '0px', | ||||||||
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.
Suggested change
@VickyStash Could you help to add borderWidthSurfaceModal property to hide the black border (more information here) |
||||||||
colorBackgroundDocTypeButtonHover: theme.successHover, | ||||||||
colorBackgroundButtonIconHover: theme.transparent, | ||||||||
colorBackgroundButtonIconActive: theme.transparent, | ||||||||
|
@@ -57,11 +68,10 @@ function initializeOnfido({sdkToken, onSuccess, onError, onUserExit, preferredLo | |||||||
{ | ||||||||
type: CONST.ONFIDO.TYPE.DOCUMENT, | ||||||||
options: { | ||||||||
useLiveDocumentCapture: true, | ||||||||
forceCrossDevice: true, | ||||||||
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. It was removed from lib some time ago https://documentation.onfido.com/sdk/web/#no--1100 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. ok, but we should make sure isn't being used in Onfido SDK, because it can be an issue with TS typings but not the code itself. 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. From what I understood it was deprecated in 10.1.10, but we are at 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. @fabioh8010 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. Thanks for explanation @VickyStash ! |
||||||||
hideCountrySelection: true, | ||||||||
country: 'USA', | ||||||||
documentTypes: { | ||||||||
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. This param wasn't found by TS 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. ok, but we should make sure isn't being used in Onfido SDK, because it can be an issue with TS typings but not the code itself. 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. Since I don't have access to the source code it's hard to say if it's used. But based on the docs you can pass country to specific document, which is done here. |
||||||||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||||||||
driving_licence: { | ||||||||
country: 'USA', | ||||||||
}, | ||||||||
|
@@ -76,17 +86,15 @@ function initializeOnfido({sdkToken, onSuccess, onError, onUserExit, preferredLo | |||||||
}, | ||||||||
}, | ||||||||
], | ||||||||
smsNumberCountryCode: CONST.ONFIDO.SMS_NUMBER_COUNTRY_CODE.US, | ||||||||
showCountrySelection: false, | ||||||||
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. We don't have 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. and what about 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.
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. ohh right, thanks! |
||||||||
onComplete: (data) => { | ||||||||
VickyStash marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
if (_.isEmpty(data)) { | ||||||||
if (isEmptyObject(data)) { | ||||||||
Log.warn('Onfido completed with no data'); | ||||||||
} | ||||||||
onSuccess(data); | ||||||||
}, | ||||||||
onError: (error) => { | ||||||||
const errorType = lodashGet(error, 'type'); | ||||||||
const errorMessage = lodashGet(error, 'message', CONST.ERROR.UNKNOWN_ERROR); | ||||||||
const errorType = error.type; | ||||||||
const errorMessage = error.message ?? CONST.ERROR.UNKNOWN_ERROR; | ||||||||
Log.hmmm('Onfido error', {errorType, errorMessage}); | ||||||||
if (errorType === CONST.WALLET.ERROR.ONFIDO_USER_CONSENT_DENIED) { | ||||||||
onUserExit(); | ||||||||
|
@@ -103,26 +111,27 @@ function initializeOnfido({sdkToken, onSuccess, onError, onUserExit, preferredLo | |||||||
// https://github.com/Expensify/App/issues/17244 | ||||||||
// https://documentation.onfido.com/sdk/web/#custom-languages | ||||||||
phrases: { | ||||||||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||||||||
'generic.back': translate('common.back'), | ||||||||
}, | ||||||||
}, | ||||||||
}); | ||||||||
} | ||||||||
|
||||||||
function logOnFidoEvent(event) { | ||||||||
function logOnFidoEvent(event: OnfidoEvent) { | ||||||||
Log.hmmm('Receiving Onfido analytic event', event.detail); | ||||||||
} | ||||||||
|
||||||||
const Onfido = forwardRef((props, ref) => { | ||||||||
function Onfido({sdkToken, onSuccess, onError, onUserExit}: OnfidoProps, ref: ForwardedRef<OnfidoElement>) { | ||||||||
const {preferredLocale, translate} = useLocalize(); | ||||||||
const theme = useTheme(); | ||||||||
|
||||||||
useEffect(() => { | ||||||||
initializeOnfido({ | ||||||||
sdkToken: props.sdkToken, | ||||||||
onSuccess: props.onSuccess, | ||||||||
onError: props.onError, | ||||||||
onUserExit: props.onUserExit, | ||||||||
sdkToken, | ||||||||
onSuccess, | ||||||||
onError, | ||||||||
onUserExit, | ||||||||
preferredLocale, | ||||||||
translate, | ||||||||
theme, | ||||||||
|
@@ -140,8 +149,8 @@ const Onfido = forwardRef((props, ref) => { | |||||||
ref={ref} | ||||||||
/> | ||||||||
); | ||||||||
}); | ||||||||
} | ||||||||
|
||||||||
Onfido.displayName = 'Onfido'; | ||||||||
Onfido.propTypes = onfidoPropTypes; | ||||||||
export default Onfido; | ||||||||
|
||||||||
export default forwardRef(Onfido); |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
import {OnfidoCaptureType, OnfidoCountryCode, OnfidoDocumentType, Onfido as OnfidoSDK} from '@onfido/react-native-sdk'; | ||
import lodashGet from 'lodash/get'; | ||
import {OnfidoCaptureType, OnfidoCountryCode, OnfidoDocumentType, Onfido as OnfidoSDK, OnfidoTheme} from '@onfido/react-native-sdk'; | ||
import React, {useEffect} from 'react'; | ||
import {Alert, Linking} from 'react-native'; | ||
import {checkMultiple, PERMISSIONS, RESULTS} from 'react-native-permissions'; | ||
import _ from 'underscore'; | ||
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import getPlatform from '@libs/getPlatform'; | ||
import Log from '@libs/Log'; | ||
import CONST from '@src/CONST'; | ||
import onfidoPropTypes from './onfidoPropTypes'; | ||
import type {TranslationPaths} from '@src/languages/types'; | ||
import type {OnfidoProps} from './types'; | ||
|
||
function Onfido({sdkToken, onUserExit, onSuccess, onError}) { | ||
function Onfido({sdkToken, onUserExit, onSuccess, onError}: OnfidoProps) { | ||
const {translate} = useLocalize(); | ||
|
||
useEffect(() => { | ||
OnfidoSDK.start({ | ||
sdkToken, | ||
theme: OnfidoTheme.AUTOMATIC, | ||
flowSteps: { | ||
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.
|
||
welcome: true, | ||
captureFace: { | ||
|
@@ -30,24 +30,25 @@ function Onfido({sdkToken, onUserExit, onSuccess, onError}) { | |
}) | ||
.then(onSuccess) | ||
.catch((error) => { | ||
const errorMessage = lodashGet(error, 'message', CONST.ERROR.UNKNOWN_ERROR); | ||
const errorType = lodashGet(error, 'type'); | ||
const errorMessage = error.message ?? CONST.ERROR.UNKNOWN_ERROR; | ||
const errorType = error.type; | ||
|
||
Log.hmmm('Onfido error on native', {errorType, errorMessage}); | ||
|
||
// If the user cancels the Onfido flow we won't log this error as it's normal. In the React Native SDK the user exiting the flow will trigger this error which we can use as | ||
// our "user exited the flow" callback. On web, this event has it's own callback passed as a config so we don't need to bother with this there. | ||
if (_.contains([CONST.ONFIDO.ERROR.USER_CANCELLED, CONST.ONFIDO.ERROR.USER_TAPPED_BACK, CONST.ONFIDO.ERROR.USER_EXITED], errorMessage)) { | ||
if ([CONST.ONFIDO.ERROR.USER_CANCELLED, CONST.ONFIDO.ERROR.USER_TAPPED_BACK, CONST.ONFIDO.ERROR.USER_EXITED].includes(errorMessage)) { | ||
onUserExit(); | ||
return; | ||
} | ||
|
||
if (!_.isEmpty(errorMessage) && getPlatform() === CONST.PLATFORM.IOS) { | ||
if (!!errorMessage && getPlatform() === CONST.PLATFORM.IOS) { | ||
checkMultiple([PERMISSIONS.IOS.MICROPHONE, PERMISSIONS.IOS.CAMERA]) | ||
.then((statuses) => { | ||
const isMicAllowed = statuses[PERMISSIONS.IOS.MICROPHONE] === RESULTS.GRANTED; | ||
const isCameraAllowed = statuses[PERMISSIONS.IOS.CAMERA] === RESULTS.GRANTED; | ||
let alertTitle = ''; | ||
let alertMessage = ''; | ||
let alertTitle: TranslationPaths | '' = ''; | ||
let alertMessage: TranslationPaths | '' = ''; | ||
if (!isCameraAllowed) { | ||
alertTitle = 'onfidoStep.cameraPermissionsNotGranted'; | ||
alertMessage = 'onfidoStep.cameraRequestMessage'; | ||
|
@@ -56,7 +57,7 @@ function Onfido({sdkToken, onUserExit, onSuccess, onError}) { | |
alertMessage = 'onfidoStep.microphoneRequestMessage'; | ||
} | ||
|
||
if (!_.isEmpty(alertTitle) && !_.isEmpty(alertMessage)) { | ||
if (!!alertTitle && !!alertMessage) { | ||
Alert.alert( | ||
translate(alertTitle), | ||
translate(alertMessage), | ||
|
@@ -93,7 +94,6 @@ function Onfido({sdkToken, onUserExit, onSuccess, onError}) { | |
return <FullscreenLoadingIndicator />; | ||
} | ||
|
||
Onfido.propTypes = onfidoPropTypes; | ||
Onfido.displayName = 'Onfido'; | ||
|
||
export default Onfido; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type {OnfidoResult} from '@onfido/react-native-sdk'; | ||
import type {Handle} from 'onfido-sdk-ui/types/Onfido'; | ||
import type {CompleteData} from 'onfido-sdk-ui/types/Types'; | ||
import type {OnyxEntry} from 'react-native-onyx'; | ||
|
||
type OnfidoData = CompleteData | OnfidoResult; | ||
|
||
type OnfidoDataWithApplicantID = OnfidoData & { | ||
applicantID: OnyxEntry<string>; | ||
}; | ||
|
||
type OnfidoElement = HTMLDivElement & {onfidoOut?: Handle}; | ||
|
||
type OnfidoProps = { | ||
/** Token used to initialize the Onfido SDK */ | ||
sdkToken: string; | ||
|
||
/** Called when the user intentionally exits the flow without completing it */ | ||
onUserExit: () => void; | ||
|
||
/** Called when the user is totally done with Onfido */ | ||
onSuccess: (data: OnfidoData) => void; | ||
|
||
/** Called when Onfido throws an error */ | ||
onError: (error?: string) => void; | ||
}; | ||
|
||
export type {OnfidoProps, OnfidoElement, OnfidoData, OnfidoDataWithApplicantID}; |
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.
This param wasn't found by TS, I haven't found it in lib/docs as well.
Onfido SDK source code is not is not publicly available to check this param there, but I can see the last version this params was mentioned in Onfido docs is 10.1.0, after that it wasn't there anymore.