From 6fa56874b34478f7210bfd2f59e22653c9b04e8d Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 3 Nov 2023 17:51:51 +0700 Subject: [PATCH 1/2] using callback to handle upload file error --- src/libs/fileDownload/FileUtils.js | 9 +++++--- src/pages/iou/ReceiptSelector/index.native.js | 9 ++++---- .../iou/steps/MoneyRequestConfirmPage.js | 23 ++++++++++--------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/libs/fileDownload/FileUtils.js b/src/libs/fileDownload/FileUtils.js index fab014de302f..b17cfa68397e 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(); }); }); diff --git a/src/pages/iou/ReceiptSelector/index.native.js b/src/pages/iou/ReceiptSelector/index.native.js index d47a2c7739a2..6a88f5236a1b 100644 --- a/src/pages/iou/ReceiptSelector/index.native.js +++ b/src/pages/iou/ReceiptSelector/index.native.js @@ -138,11 +138,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..1b541b5a9ed9 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; } From 97c2b331cee847176bd976637692a9d432e4d417 Mon Sep 17 00:00:00 2001 From: Dylan Date: Mon, 6 Nov 2023 14:09:20 +0700 Subject: [PATCH 2/2] default params --- src/libs/fileDownload/FileUtils.js | 4 ++-- src/pages/iou/ReceiptSelector/index.native.js | 2 +- src/pages/iou/steps/MoneyRequestConfirmPage.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/fileDownload/FileUtils.js b/src/libs/fileDownload/FileUtils.js index b17cfa68397e..b838a81ea550 100644 --- a/src/libs/fileDownload/FileUtils.js +++ b/src/libs/fileDownload/FileUtils.js @@ -164,7 +164,7 @@ function appendTimeToFileName(fileName) { * * @returns {Promise} */ -const readFileAsync = (path, fileName, onSuccess, onFailure) => +const readFileAsync = (path, fileName, onSuccess, onFailure = () => {}) => new Promise((resolve) => { if (!path) { resolve(); @@ -190,7 +190,7 @@ const readFileAsync = (path, fileName, onSuccess, onFailure) => }) .catch((e) => { console.debug('[FileUtils] Could not read uploaded file', e); - onFailure(); + onFailure(e); }); }); diff --git a/src/pages/iou/ReceiptSelector/index.native.js b/src/pages/iou/ReceiptSelector/index.native.js index 6a88f5236a1b..4de7fffa5aa4 100644 --- a/src/pages/iou/ReceiptSelector/index.native.js +++ b/src/pages/iou/ReceiptSelector/index.native.js @@ -143,7 +143,7 @@ function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator, s }; if (transactionID) { - FileUtils.readFileAsync(filePath, photo.path, onSuccess, () => {}); + 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 1b541b5a9ed9..3a12a4da4e59 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -227,7 +227,7 @@ function MoneyRequestConfirmPage(props) { existingSplitChatReportID, ); }; - FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptFilename, onSuccess, () => {}); + FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptFilename, onSuccess); return; }