From 53cba4e526d98cf28bae675b708f1d593c30b598 Mon Sep 17 00:00:00 2001 From: Shachar Udi Date: Wed, 1 May 2024 11:36:38 -0400 Subject: [PATCH] MOB-1000 #comment implemented response handler events --- README.md | 3 ++- src/index.tsx | 32 +++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 82857e8..faff823 100644 --- a/README.md +++ b/README.md @@ -32,4 +32,5 @@ Made with [create-react-native-library](https://github.com/callstack/react-nativ -add transmit plist file to ios and json file/manifest for android \ No newline at end of file +add transmit plist file to ios and json file/manifest for android +must set response handler diff --git a/src/index.tsx b/src/index.tsx index 2b0922a..5615682 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,4 +1,4 @@ -import { NativeModules, Platform } from 'react-native'; +import { NativeModules, Platform, NativeEventEmitter } from 'react-native'; const LINKING_ERROR = `The package 'react-native-ts-identity-orchestration' doesn't seem to be linked. Make sure: \n\n` + @@ -17,6 +17,7 @@ const TsIdentityOrchestration = NativeModules.TsIdentityOrchestration } ); +const eventEmitter = new NativeEventEmitter(TsIdentityOrchestration); export namespace TSIDOModule { @@ -89,11 +90,22 @@ export namespace TSIDOModule { - data: The client response data object. Mandatory in ClientInput response option type, populate with data for the Journey step to process. Optional in Cancel and Custom as an additional parameters for the branch. */ submitClientResponse: (clientResponseOptionId: TSIDOModule.ClientResponseOptionType | string, data?: { [key: string]: any; } | null | undefined) => void; + + /** + This method will expose a success and error handlers to your application. + + - Parameters: + - responseHandler : The response handler object with success and error callbacks. + */ + setResponseHandler: (responseHandler: TSIDOModule.ResponseHandler) => void; } } class RNTSIdentityOrchestration implements TSIDOModule.API { + private responseHandler?: TSIDOModule.ResponseHandler; + private static kResponseHandlerEventname = "tsido_response_handler_event"; + initializeSDK = async (): Promise => { return await TsIdentityOrchestration.initializeSDK(); } @@ -109,5 +121,23 @@ class RNTSIdentityOrchestration implements TSIDOModule.API { TsIdentityOrchestration.submitClientResponse(clientResponseOptionId, data); } + setResponseHandler = (responseHandler: TSIDOModule.ResponseHandler): void => { + this.responseHandler = responseHandler; + eventEmitter.addListener( + RNTSIdentityOrchestration.kResponseHandlerEventname, + this.onResponseReceived + ); + } + + private onResponseReceived = async (params: any) => { + const success: boolean = params["success"]; + const additionalData: { [key: string]: any } = params["additionalData"]; + + if (success) { + this.responseHandler?.success(additionalData); + } else { + this.responseHandler?.error(additionalData.errorCode); + } + } } export default new RNTSIdentityOrchestration();