Skip to content

Commit

Permalink
FEN-69 fix android crash (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
dima-bezugly committed Aug 1, 2023
1 parent 278446a commit cb797a5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"main": "src/components/LinkSDK/index.js",
"name": "lean-react-native",
"version": "0.1.8",
"version": "0.1.9",
"description": "A React Native wrapper for Lean's LinkSDK",
"repository": {
"type": "git",
Expand Down
58 changes: 24 additions & 34 deletions src/components/LinkSDK/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ const COUNTRY__SA = 'SaudiArabia';

const LinkSDK = forwardRef((props, ref) => {
// create a ref for injectJavaScript to use
const [injectedJavascript, setInjectedJavascript] = useState('');
const SDK = useRef(null);

// create state to manage SDK visibility
const [isOpen, setIsOpen] = useState(false);
const openLinkSDK = (flowScript) => {
setInjectedJavascript(flowScript);
setIsOpen(true);
};

// useImperativeHandle allows the methods to be called outside of the component
useImperativeHandle(ref, () => ({
Expand All @@ -18,10 +23,8 @@ const LinkSDK = forwardRef((props, ref) => {

// initialise connect flow
connect(opts) {
setIsOpen(true);
const keys = Object.keys(opts);

const call = `
const flowScript = `
function postResponse(status) {
status.method = "CONNECT"
window.ReactNativeWebView.postMessage(JSON.stringify(status))
Expand All @@ -41,15 +44,13 @@ const LinkSDK = forwardRef((props, ref) => {
}
`;

SDK.current.injectJavaScript(call);
openLinkSDK(flowScript);
},

// initialise link flow
link(opts) {
setIsOpen(true);
const keys = Object.keys(opts);

const call = `
const flowScript = `
function postResponse(status) {
status.method = "LINK"
window.ReactNativeWebView.postMessage(JSON.stringify(status))
Expand All @@ -69,15 +70,13 @@ const LinkSDK = forwardRef((props, ref) => {
}
`;

SDK.current.injectJavaScript(call);
openLinkSDK(flowScript);
},

// initialise reconnect flow
reconnect(opts) {
setIsOpen(true);
const keys = Object.keys(opts);

const call = `
const flowScript = `
function postResponse(status) {
status.method = "RECONNECT"
window.ReactNativeWebView.postMessage(JSON.stringify(status))
Expand All @@ -97,15 +96,13 @@ const LinkSDK = forwardRef((props, ref) => {
}
`;

SDK.current.injectJavaScript(call);
openLinkSDK(flowScript);
},

// initialise CPS flow
createPaymentSource(opts) {
setIsOpen(true);
const keys = Object.keys(opts);

const call = `
const flowScript = `
function postResponse(status) {
status.method = "CREATE_PAYMENT_SOURCE"
window.ReactNativeWebView.postMessage(JSON.stringify(status))
Expand All @@ -124,15 +121,14 @@ const LinkSDK = forwardRef((props, ref) => {
postResponse({ method: "CREATE_PAYMENT_SOURCE", status: "ERROR", message: "Lean not initialized" })
}
`;
SDK.current.injectJavaScript(call);

openLinkSDK(flowScript);
},

// initialise pay flow
pay(opts) {
setIsOpen(true);
const keys = Object.keys(opts);

const call = `
const flowScript = `
function postResponse(status) {
status.method = "PAY"
window.ReactNativeWebView.postMessage(JSON.stringify(status))
Expand All @@ -152,15 +148,13 @@ const LinkSDK = forwardRef((props, ref) => {
}
`;

SDK.current.injectJavaScript(call);
openLinkSDK(flowScript);
},

// updatePaymentSource flow
updatePaymentSource(opts) {
setIsOpen(true);
const keys = Object.keys(opts);

const call = `
const flowScript = `
function postResponse(status) {
status.method = "UPDATE_PAYMENT_SOURCE"
window.ReactNativeWebView.postMessage(JSON.stringify(status))
Expand All @@ -180,7 +174,7 @@ const LinkSDK = forwardRef((props, ref) => {
}
`;

SDK.current.injectJavaScript(call);
openLinkSDK(flowScript);
},
}));

Expand All @@ -192,9 +186,13 @@ const LinkSDK = forwardRef((props, ref) => {
}
};

if (!isOpen) {
return null;
}

return (
<View
style={isOpen ? styles.container : styles.containerClosed}
style={styles.container}
height={Dimensions.get('window').height}
width={Dimensions.get('window').width}>
<WebView
Expand All @@ -204,6 +202,7 @@ const LinkSDK = forwardRef((props, ref) => {
originWhitelist={['*']}
allowsInlineMediaPlayback={true}
mediaPlaybackRequiresUserAction={false}
injectedJavaScript={injectedJavascript}
source={{
baseUrl: 'https://leantech.me',
html: require('./base.js')({
Expand Down Expand Up @@ -246,15 +245,6 @@ const styles = StyleSheet.create({
height: '100%',
zIndex: 2,
},
containerClosed: {
display: 'none',
position: 'relative',
left: 0,
top: 0,
width: '100%',
height: '100%',
zIndex: 2,
},
WebView: {
position: 'absolute',
top: 0,
Expand Down

0 comments on commit cb797a5

Please sign in to comment.