1
- import { useCallback , useRef , useMemo , useEffect , useState , useId } from 'react' ;
1
+ import { useCallback , useRef , useMemo , useEffect , useState } from 'react' ;
2
2
import useOnfidoServiceToken from './useOnfidoServiceToken' ;
3
3
import { ALPHA_2_TO_ALPHA_3 , ONFIDO_PHRASES } from '../constants' ;
4
4
import useSettings from './useSettings' ;
@@ -24,8 +24,6 @@ import { v4 as uuidv4 } from 'uuid';
24
24
* ```
25
25
*/
26
26
const useOnfido = ( ) => {
27
- // used to check that we only initialize and load the onfido script once
28
- const [ isOnfidoLoaded , setIsOnfidoLoaded ] = useState ( false ) ;
29
27
// use to check that we do not re-attempt to reload the onfido script while its still loading
30
28
const [ isOnfidoLoading , setIsOnfidoLoading ] = useState ( false ) ;
31
29
const [ hasSubmitted , setHasSubmitted ] = useState ( false ) ;
@@ -89,33 +87,7 @@ const useOnfido = () => {
89
87
[ submitDocuments ]
90
88
) ;
91
89
92
- const loadOnfidoSdkScript = ( ) => {
93
- const onfidoScriptNode = document . getElementById ( 'onfido_sdk' ) ;
94
- // check if the onfido sdk script has been loaded, and if its still loading the onfido script, don't re-attempt to load the script again
95
- if ( ! onfidoScriptNode || ! isOnfidoLoading ) {
96
- setIsOnfidoLoading ( true ) ;
97
- const scriptNode = document . createElement ( 'script' ) ;
98
- const linkNode = document . createElement ( 'link' ) ;
99
-
100
- scriptNode . id = 'onfido_sdk' ;
101
- scriptNode . src = 'https://assets.onfido.com/web-sdk-releases/latest/onfido.min.js' ;
102
- linkNode . href = 'https://assets.onfido.com/web-sdk-releases/latest/style.css' ;
103
- linkNode . rel = 'stylesheet' ;
104
-
105
- document . body . appendChild ( scriptNode ) ;
106
- document . body . appendChild ( linkNode ) ;
107
-
108
- scriptNode . addEventListener ( 'load' , ( ) => {
109
- setIsOnfidoLoading ( false ) ;
110
- setIsOnfidoLoaded ( true ) ;
111
- initOnfido ( ) ;
112
- } ) ;
113
- } else {
114
- initOnfido ( ) ;
115
- }
116
- } ;
117
-
118
- const initOnfido = async ( ) => {
90
+ const initOnfido = useCallback ( async ( ) => {
119
91
const i18NLanguage = window . localStorage . getItem ( 'i18n_language' ) ?. toLowerCase ( ) || 'en' ;
120
92
const onfidoCountryCode =
121
93
countryCode . length !== 3 ? ALPHA_2_TO_ALPHA_3 [ countryCode . toUpperCase ( ) ] : settings ?. country_code ;
@@ -156,13 +128,38 @@ const useOnfido = () => {
156
128
'face' ,
157
129
] ,
158
130
} ) ;
159
- } ;
131
+ } , [ countryCode , onComplete , onfidoContainerId , settings ?. country_code , supportedDocuments , token ] ) ;
132
+
133
+ const loadOnfidoSdkScript = useCallback ( ( ) => {
134
+ const hasOnfidoScriptNode = ! ! document . getElementById ( 'onfido_sdk' ) ;
135
+ // check if the onfido sdk script has been loaded, and if its still loading the onfido script, don't re-attempt to load the script again
136
+ if ( hasOnfidoScriptNode ) {
137
+ if ( ! isOnfidoLoading ) initOnfido ( ) ;
138
+ } else {
139
+ setIsOnfidoLoading ( true ) ;
140
+ const scriptNode = document . createElement ( 'script' ) ;
141
+ const linkNode = document . createElement ( 'link' ) ;
142
+
143
+ scriptNode . id = 'onfido_sdk' ;
144
+ scriptNode . src = 'https://assets.onfido.com/web-sdk-releases/latest/onfido.min.js' ;
145
+ linkNode . href = 'https://assets.onfido.com/web-sdk-releases/latest/style.css' ;
146
+ linkNode . rel = 'stylesheet' ;
147
+
148
+ document . body . appendChild ( scriptNode ) ;
149
+ document . body . appendChild ( linkNode ) ;
150
+
151
+ scriptNode . addEventListener ( 'load' , ( ) => {
152
+ initOnfido ( ) ;
153
+ setIsOnfidoLoading ( false ) ;
154
+ } ) ;
155
+ }
156
+ } , [ initOnfido , isOnfidoLoading ] ) ;
160
157
161
158
useEffect ( ( ) => {
162
- if ( token && supportedDocuments . length && ! isOnfidoLoaded && countryCode ) {
159
+ if ( token && supportedDocuments . length && countryCode ) {
163
160
loadOnfidoSdkScript ( ) ;
164
161
}
165
- } , [ token , supportedDocuments , countryCode ] ) ;
162
+ } , [ token , supportedDocuments , countryCode , loadOnfidoSdkScript ] ) ;
166
163
167
164
return {
168
165
data : {
0 commit comments