react-native-unit-components
Version:
Unit React Native components
49 lines (41 loc) • 1.9 kB
text/typescript
import axios from 'axios';
import { Share, Platform } from 'react-native';
import AppInfo from '../utils/AppInfo';
import type { EventToContinueEvent, RequestDownloadEvent } from '../messages/webMessages/unitComponentsMessages';
import { UnitComponentsSDK } from '../unitComponentsSdkManager/UnitComponentsSdkManager';
import WebView from 'react-native-webview';
import RNShareFile from '../nativeModulesHelpers/RNShareFile/RNShareFile';
export const getInfoParams = async () => {
const appVersion = await AppInfo.getVersion();
const appBuildNumber = await AppInfo.getBuildNumber();
const appIdentifier = await AppInfo.getAppIdentifier();
const infoParams: { [key: string]: string } = {};
infoParams['sdkVersion'] = UnitComponentsSDK.getSdkVersion();
infoParams['sdkPlatform'] = 'React Native';
infoParams['os'] = Platform.OS == 'android' ? 'Android' : 'iOS';
infoParams['hostedAppVersion'] = appVersion;
infoParams['hostedAppBuildNumber'] = appBuildNumber;
infoParams['hostedAppIdentifier'] = appIdentifier;
return infoParams;
};
export const handleRequestDownload = async (details: RequestDownloadEvent, onReadyToShare: () => void) => {
if (details.fileType === 'application/pdf') {
const res = await axios.get(details.url, {
headers: {
'Authorization': `Bearer ${details.accessToken}`
},
responseEncoding: 'base64',
responseType: 'arraybuffer'
});
const base64Data = res.request._response;
onReadyToShare();
if (Platform.OS === 'ios') {
Share.share({ url: `data:application/pdf;base64,${base64Data}` });
} else {
RNShareFile.share(base64Data, details.fileName);
}
}
};
export const injectEventToContinue = (currentWeb: WebView | null, eventToContinueEvent: EventToContinueEvent) => {
currentWeb?.injectJavaScript(`dispatchEventToContinue('${JSON.stringify(eventToContinueEvent)}')`);
};