Skip to content

Commit

Permalink
add asyncOpenURL wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
luacmartins committed Jun 25, 2021
1 parent 12e9392 commit d741cff
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Onyx from 'react-native-onyx';
import {Linking} from 'react-native';
import _ from 'underscore';
import CONST from '../../CONST';
import ONYXKEYS from '../../ONYXKEYS';
import ROUTES from '../../ROUTES';
import * as API from '../API';
import {getSimplifiedIOUReport, fetchChatReportsByIDs, fetchIOUReportByIDAndUpdateChatReport} from './Report';
import Navigation from '../Navigation/Navigation';
import asyncOpenURL from '../asyncOpenURL';

/**
* @param {Object[]} requestParams
Expand Down Expand Up @@ -196,7 +196,8 @@ function payIOUReport({
const payIOUPromise = paymentMethodType === CONST.IOU.PAYMENT_TYPE.EXPENSIFY
? API.PayWithWallet({reportID})
: API.PayIOU({reportID, paymentMethodType});
payIOUPromise

asyncOpenURL(payIOUPromise
.then((response) => {
if (response.jsonCode !== 200) {
throw new Error(response.message);
Expand All @@ -213,16 +214,17 @@ function payIOUReport({
// Once we have successfully paid the IOU we will transfer the user to their platform of choice if they have
// selected something other than a manual settlement or Expensify Wallet e.g. Venmo or PayPal.me
if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.PAYPAL_ME) {
Linking.openURL(buildPayPalPaymentUrl(amount, submitterPayPalMeAddress, currency));
} else if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.VENMO) {
Linking.openURL(buildVenmoPaymentURL(amount, submitterPhoneNumber));
return buildPayPalPaymentUrl(amount, submitterPayPalMeAddress, currency);
}
if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.VENMO) {
return buildVenmoPaymentURL(amount, submitterPhoneNumber);
}
})
.catch((error) => {
console.error(`Error Paying iouReport: ${error}`);
Onyx.merge(ONYXKEYS.IOU, {error: true});
})
.finally(() => Onyx.merge(ONYXKEYS.IOU, {loading: false}));
.finally(() => Onyx.merge(ONYXKEYS.IOU, {loading: false})));
}

export {
Expand Down
10 changes: 10 additions & 0 deletions src/libs/asyncOpenURL/index.desktop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Linking} from 'react-native';

export default function asyncOpenURL(promise) {
promise.then((url) => {
if (!url) {
return;
}
Linking.openURL(url);
});
}
10 changes: 10 additions & 0 deletions src/libs/asyncOpenURL/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Linking} from 'react-native';

export default function asyncOpenURL(promise) {
promise.then((url) => {
if (!url) {
return;
}
Linking.openURL(url);
});
}
29 changes: 29 additions & 0 deletions src/libs/asyncOpenURL/index.website.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Linking} from 'react-native';

/**
* Prevents Safari from blocking pop-up window when opened within async call.
*
* @param {Promise} promise
*/
export default function asyncOpenURL(promise) {
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

if (!isSafari) {
promise.then((url) => {
if (!url) {
return;
}
Linking.openURL(url);
});
} else {
const windowRef = window.open();
promise
.then((url) => {
if (!url) {
return windowRef.close();
}
windowRef.location = url;
})
.catch(() => windowRef.close());
}
}

0 comments on commit d741cff

Please sign in to comment.