diff --git a/src/libs/fileDownload/FileUtils.js b/src/libs/fileDownload/FileUtils.js index fab014de302f..b838a81ea550 100644 --- a/src/libs/fileDownload/FileUtils.js +++ b/src/libs/fileDownload/FileUtils.js @@ -159,9 +159,12 @@ function appendTimeToFileName(fileName) { * * @param {String} path - the blob url of the locally uplodaded file * @param {String} fileName + * @param {Function} onSuccess + * @param {Function} onFailure + * * @returns {Promise} */ -const readFileAsync = (path, fileName) => +const readFileAsync = (path, fileName, onSuccess, onFailure = () => {}) => new Promise((resolve) => { if (!path) { resolve(); @@ -183,11 +186,11 @@ const readFileAsync = (path, fileName) => // For some reason, the File object on iOS does not have a uri property // so images aren't uploaded correctly to the backend file.uri = path; - resolve(file); + onSuccess(file); }) .catch((e) => { console.debug('[FileUtils] Could not read uploaded file', e); - resolve(); + onFailure(e); }); }); diff --git a/src/pages/iou/ReceiptSelector/index.native.js b/src/pages/iou/ReceiptSelector/index.native.js index 1add5ba17908..824c242cf02f 100644 --- a/src/pages/iou/ReceiptSelector/index.native.js +++ b/src/pages/iou/ReceiptSelector/index.native.js @@ -157,11 +157,12 @@ function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator, s const filePath = `file://${photo.path}`; IOU.setMoneyRequestReceipt(filePath, photo.path); - if (transactionID) { - FileUtils.readFileAsync(filePath, photo.path).then((receipt) => { - IOU.replaceReceipt(transactionID, receipt, filePath); - }); + const onSuccess = (receipt) => { + IOU.replaceReceipt(transactionID, receipt, filePath); + }; + if (transactionID) { + FileUtils.readFileAsync(filePath, photo.path, onSuccess); Navigation.dismissModal(); return; } diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index 6b570ee872c3..3a12a4da4e59 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -103,15 +103,15 @@ function MoneyRequestConfirmPage(props) { if (!props.iou.receiptPath || !props.iou.receiptFilename) { return; } - FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptFilename).then((file) => { - if (!file) { - Navigation.goBack(ROUTES.MONEY_REQUEST.getRoute(iouType, reportID)); - } else { - const receipt = file; - receipt.state = file && isManualRequestDM ? CONST.IOU.RECEIPT_STATE.OPEN : CONST.IOU.RECEIPT_STATE.SCANREADY; - setReceiptFile(receipt); - } - }); + const onSuccess = (file) => { + const receipt = file; + receipt.state = file && isManualRequestDM ? CONST.IOU.RECEIPT_STATE.OPEN : CONST.IOU.RECEIPT_STATE.SCANREADY; + setReceiptFile(receipt); + }; + const onFailure = () => { + Navigation.goBack(ROUTES.MONEY_REQUEST.getRoute(iouType, reportID)); + }; + FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptFilename, onSuccess, onFailure); }, [props.iou.receiptPath, props.iou.receiptFilename, isManualRequestDM, iouType, reportID]); useEffect(() => { @@ -217,7 +217,7 @@ function MoneyRequestConfirmPage(props) { // If we have a receipt let's start the split bill by creating only the action, the transaction, and the group DM if needed if (iouType === CONST.IOU.TYPE.SPLIT && props.iou.receiptPath) { const existingSplitChatReportID = CONST.REGEX.NUMBER.test(reportID) ? reportID : ''; - FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptFilename).then((receipt) => { + const onSuccess = (receipt) => { IOU.startSplitBill( selectedParticipants, props.currentUserPersonalDetails.login, @@ -226,7 +226,8 @@ function MoneyRequestConfirmPage(props) { receipt, existingSplitChatReportID, ); - }); + }; + FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptFilename, onSuccess); return; }