import { View, StyleSheet, Button, Text } from 'react-native'; import React, { memo, useCallback, useEffect, useState } from 'react'; import HintMessage from './HintMessage'; import { type GetIdSdk, GetidSdkTypes } from '@get-id/react-native-sdk'; import { DebugContext, DebugHelper } from '../../../src/contexts/debug-context'; import { useCropToBase64 } from '../helpers/debug'; type DocScanProps = { sdk: GetIdSdk; docType: 'front' | 'back'; onNextStep?: () => void; onComplete?: () => void; onError?: (error: any) => void; }; type DocScanFunctions = { takePicture?: Function; capture?: Function; uploadDocument?: () => Promise; }; const GetidFrontDocScanWrapper = ({ sdk, onNextStep, onComplete, onError, docType, }: DocScanProps) => { const [GetIdDocScanCmp, setGetIdDocScanCmp] = useState(); const [hintMessage, setHintMessage] = useState(''); const [sdkFunctions, setSdkFunctions] = useState({}); const [uploadingDoc, setUploadingDoc] = useState(false); const [captureClicked, setCaptureClicked] = useState(false); const [localError, setLocalError] = useState(null); const [isCameraReady, setIsCameraReady] = useState(false); const onCameraReady = useCallback(() => { setIsCameraReady(true); }, []); const onUserHintChange = useCallback((hint: any) => { console.log(`onUserHintChange: ${hint}`); setHintMessage(hint); }, []); const onDocumentScanServerValidationStarted = useCallback(() => { console.log( 'onDocumentScanServerValidationStarted: Document scan server validation started' ); }, []); const handleScannedDocument = ( scannedDocument: GetidSdkTypes.ScannedDocument ) => { console.log('Scanned Document:', scannedDocument); if ( docType === 'front' && scannedDocument.conclusion === 'back-side-missing' ) { return onNextStep?.(); } if (scannedDocument.conclusion === 'ok') { // Once the document conclusion is ok, you can submit the document for verification return onComplete?.(); } onUserHintChange(scannedDocument.conclusion); setUploadingDoc(false); }; useEffect(() => { const initDocScan = async () => { const initialiseDocumentScan = docType === 'front' ? sdk.initialiseFrontDocumentScan : sdk.initialiseBackDocumentScan; try { const { component, ...functions } = await initialiseDocumentScan({ onDocumentScanServerValidationStarted, onUserHintChange, onCameraReady, useAutoCapture: true, debugMode: false, }); setGetIdDocScanCmp(() => component); setSdkFunctions(functions); } catch (e: any) { console.error('Error initialising face scan', e); onError?.(e); } }; initDocScan(); }, [ sdk, onDocumentScanServerValidationStarted, onUserHintChange, onCameraReady, docType, onError, ]); const debugHelper = new DebugHelper(); debugHelper.cropToBase64 = useCropToBase64(debugHelper.DEBUG_CROP_SIZE); return ( <> {!isCameraReady && ( Loading... )} {GetIdDocScanCmp} Please scan your {docType} document {uploadingDoc && Scanning...} {!uploadingDoc && !localError && } {!uploadingDoc && localError && }