From 62c0f3b588e1259b211297d4c10c49f2747adcb4 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 22 Apr 2021 10:13:56 -0600 Subject: [PATCH 1/9] Fix a propType warning --- src/components/IOUConfirmationList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/IOUConfirmationList.js b/src/components/IOUConfirmationList.js index 8bcf8837736b..379e4b704b35 100644 --- a/src/components/IOUConfirmationList.js +++ b/src/components/IOUConfirmationList.js @@ -208,7 +208,7 @@ class IOUConfirmationList extends Component { forceTextUnreadStyle canSelectMultipleOptions={this.props.hasMultipleParticipants} disableFocusOptions - selectedOptions={this.props.hasMultipleParticipants && this.getAllOptionsAsSelected()} + selectedOptions={this.props.hasMultipleParticipants ? this.getAllOptionsAsSelected() : []} /> From 8bd18fa2ad8a81104f02deb3c016b4488c20b812 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 22 Apr 2021 10:32:50 -0600 Subject: [PATCH 2/9] DRY code, improve and clean up params --- src/components/IOUConfirmationList.js | 14 +++---- src/libs/actions/IOU.js | 57 +++++++++------------------ src/pages/iou/IOUModal.js | 21 +++++----- 3 files changed, 36 insertions(+), 56 deletions(-) diff --git a/src/components/IOUConfirmationList.js b/src/components/IOUConfirmationList.js index 379e4b704b35..eb750758f863 100644 --- a/src/components/IOUConfirmationList.js +++ b/src/components/IOUConfirmationList.js @@ -135,6 +135,12 @@ class IOUConfirmationList extends Component { * @returns {Array} */ getSplits() { + // There can only be splits when there are multiple participants, so return early when there are not + // multiple participants + if (!this.props.hasMultipleParticipants) { + return []; + } + const splits = this.props.participants.map(participant => ({ email: participant.login, @@ -229,13 +235,7 @@ class IOUConfirmationList extends Component { { - if (this.props.hasMultipleParticipants) { - this.props.onConfirm({splits: this.getSplits()}); - } else { - this.props.onConfirm({}); - } - }} + onClick={() => this.props.onConfirm(this.getSplits())} /> diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index f56871642da9..129706c9a49e 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -53,58 +53,39 @@ function getIOUReportsForNewTransaction(reportIds) { /** * Creates IOUSplit Transaction - * @param {Object} parameters - * @param {String} parameters.amount - * @param {String} parameters.comment - * @param {String} parameters.currency - * @param {String} parameters.debtorEmail + * @param {Object} params + * @param {String} params.amount + * @param {String} params.comment + * @param {String} params.currency + * @param {String} params.debtorEmail */ -function createIOUTransaction({ - comment, amount, currency, debtorEmail, -}) { +function createIOUTransaction(params) { Onyx.merge(ONYXKEYS.IOU, {loading: true, creatingIOUTransaction: true, error: false}); - API.CreateIOUTransaction({ - comment, - amount, - currency, - debtorEmail, - }) + API.CreateIOUTransaction(params) .then(data => data.reportID) .then(reportID => getIOUReportsForNewTransaction([reportID])); } /** * Creates IOUSplit Transaction - * @param {Object} parameters - * @param {Array} parameters.splits - * @param {String} parameters.comment - * @param {String} parameters.amount - * @param {String} parameters.currency + * @param {Object} params + * @param {Array} params.splits + * @param {String} params.comment + * @param {String} params.amount + * @param {String} params.currency */ -function createIOUSplit({ - comment, - amount, - currency, - splits, -}) { +function createIOUSplit(params) { Onyx.merge(ONYXKEYS.IOU, {loading: true, creatingIOUTransaction: true, error: false}); API.CreateChatReport({ - emailList: splits.map(participant => participant.email).join(','), + emailList: params.splits.map(participant => participant.email).join(','), }) - .then((data) => { - console.debug(data); - return data.reportID; - }) - .then(reportID => API.CreateIOUSplit({ - splits: JSON.stringify(splits), - currency, - amount, - comment, - reportID, + .then(data => API.CreateIOUSplit({ + ...params, + splits: JSON.stringify(params.splits), + reportID: data.data, })) - .then(data => data.reportIDList) - .then(reportIDList => getIOUReportsForNewTransaction(reportIDList)); + .then(data => getIOUReportsForNewTransaction(data.reportIDList)); } export { diff --git a/src/pages/iou/IOUModal.js b/src/pages/iou/IOUModal.js index 25f8b82e2a9c..14b4a4315b48 100644 --- a/src/pages/iou/IOUModal.js +++ b/src/pages/iou/IOUModal.js @@ -146,9 +146,12 @@ class IOUModal extends Component { this.setState({selectedCurrency}); } - createTransaction({splits}) { + /** + * @param {Array} [splits] + */ + createTransaction(splits) { if (splits) { - return createIOUSplit({ + createIOUSplit({ comment: this.state.comment, // should send in cents to API @@ -156,25 +159,21 @@ class IOUModal extends Component { currency: this.state.selectedCurrency, splits, }); + return; } - console.debug({ + const params = { comment: this.state.comment, // should send in cents to API amount: this.state.amount * 100, currency: this.state.selectedCurrency, debtorEmail: this.state.participants[0].login, - }); + }; - return createIOUTransaction({ - comment: this.state.comment, + console.debug(params); - // should send in cents to API - amount: this.state.amount * 100, - currency: this.state.selectedCurrency, - debtorEmail: this.state.participants[0].login, - }); + createIOUTransaction(params); } render() { From 4def951ab0dd211d79b12c7440248e68e45906a3 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 22 Apr 2021 15:53:50 -0600 Subject: [PATCH 3/9] Make sure that splits still work --- src/components/IOUConfirmationList.js | 4 +- src/libs/actions/IOU.js | 68 ++++++++++++++++++--------- src/libs/actions/Report.js | 5 +- src/pages/iou/IOUModal.js | 1 - 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/components/IOUConfirmationList.js b/src/components/IOUConfirmationList.js index eb750758f863..cd193c0bd090 100644 --- a/src/components/IOUConfirmationList.js +++ b/src/components/IOUConfirmationList.js @@ -132,13 +132,13 @@ class IOUConfirmationList extends Component { /** * Gets splits for the transaction * - * @returns {Array} + * @returns {Array|null} */ getSplits() { // There can only be splits when there are multiple participants, so return early when there are not // multiple participants if (!this.props.hasMultipleParticipants) { - return []; + return null; } const splits = this.props.participants.map(participant => ({ diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 129706c9a49e..9d08fdbbbeb4 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -17,35 +17,45 @@ function getPreferredCurrency() { } /** - * @param {Array} reportIds + * @param {Object[]} requestParams + * @param {Number} requestParams.reportID the ID of the IOU report + * @param {Number} requestParams.chatReportID the ID of the chat report that the IOU report belongs to * @returns {Promise} * Gets the IOU Reports for new transaction */ -function getIOUReportsForNewTransaction(reportIds) { +function getIOUReportsForNewTransaction(requestParams) { return API.Get({ returnValueList: 'reportStuff', - reportIDList: reportIds, + reportIDList: _.pluck(requestParams, 'reportID'), shouldLoadOptionalKeys: true, includePinnedReports: true, }) - .then(({reports}) => _.map(reports, getSimplifiedIOUReport)) - .then((iouReportObjects) => { - const reportIOUData = {}; + .then(({reportsData}) => { + const chatReportsToUpdate = {}; + const iouReportsToUpdate = {}; - if (iouReportObjects.length === 1) { - const iouReportKey = `${ONYXKEYS.COLLECTION.REPORT_IOUS}${iouReportObjects[0].reportID}`; - return Onyx.merge(iouReportKey, - getSimplifiedIOUReport(iouReportObjects[0])); - } + _.each(reportsData, (reportData) => { + // First, the existing chat report needs updated with the details about the new IOU + const chatReportIDForReport = _.chain(requestParams) + .findWhere(requestParams, {reportID: reportData.reportID}) + .pluck('chatReportID') + .value(); + const chatReportKey = `${ONYXKEYS.COLLECTION.REPORT}${chatReportIDForReport}`; + chatReportsToUpdate[chatReportKey] = { + iouReportID: reportData.reportID, + total: reportData.total, + stateNum: reportData.stateNum, + hasOutstandingIOU: reportData.hasOutstandingIOU, + }; - _.each(iouReportObjects, (iouReportObject) => { - if (!iouReportObject) { - return; - } - const iouReportKey = `${ONYXKEYS.COLLECTION.REPORT_IOUS}${iouReportObject.reportID}`; - reportIOUData[iouReportKey] = iouReportObject; + // Second, the IOU report needs updated with the new IOU details too + const iouReportKey = `${ONYXKEYS.COLLECTION.REPORT_IOUS}${reportData.reportID}`; + iouReportsToUpdate[iouReportKey] = getSimplifiedIOUReport(reportData, reportData.reportID); }); - return Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT_IOUS, {...reportIOUData}); + + // Now, merge the updated objects into our store + Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, chatReportsToUpdate); + return Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT_IOUS, iouReportsToUpdate); }) .catch(() => Onyx.merge(ONYXKEYS.IOU, {loading: false, creatingIOUTransaction: false, error: true})) .finally(() => Onyx.merge(ONYXKEYS.IOU, {loading: false, creatingIOUTransaction: false})); @@ -62,8 +72,7 @@ function getIOUReportsForNewTransaction(reportIds) { function createIOUTransaction(params) { Onyx.merge(ONYXKEYS.IOU, {loading: true, creatingIOUTransaction: true, error: false}); API.CreateIOUTransaction(params) - .then(data => data.reportID) - .then(reportID => getIOUReportsForNewTransaction([reportID])); + .then(data => getIOUReportsForNewTransaction([data])); } /** @@ -83,9 +92,24 @@ function createIOUSplit(params) { .then(data => API.CreateIOUSplit({ ...params, splits: JSON.stringify(params.splits), - reportID: data.data, + reportID: data.reportID, })) - .then(data => getIOUReportsForNewTransaction(data.reportIDList)); + .then((data) => { + // This data needs to go from this: + // {reportIDList: [1, 2], chatReportIDList: [3, 4]} + // to this: + // [{reportID: 1, chatReportID: 3}, {reportID: 2, chatReportID: 4}] + // in order for getIOUReportsForNewTransaction to know which IOU reports are associated with which + // chat reports + const reportParams = []; + for (let i = 0; i < data.reportIDList.length; i++) { + reportParams.push({ + reportID: data.reportIDList[i], + chatReportID: data.chatReportIDList[i], + }); + } + getIOUReportsForNewTransaction(reportParams); + }); } export { diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index cd3e7cfbe6fe..d7ee8bc4efb2 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -167,7 +167,7 @@ function getSimplifiedReportObject(report) { * @param {String} reportData.ownerEmail * @param {String} reportData.managerEmail * @param {Number} reportData.reportID - * @param {Number} chatReportID + * @param {Number|String} chatReportID * @returns {Object} */ function getSimplifiedIOUReport(reportData, chatReportID) { @@ -185,12 +185,13 @@ function getSimplifiedIOUReport(reportData, chatReportID) { managerEmail: reportData.managerEmail, currency: reportData.currency, transactions, - chatReportID, + chatReportID: Number(chatReportID), state: reportData.state, cachedTotal: reportData.cachedTotal, total: reportData.total, status: reportData.status, stateNum: reportData.stateNum, + hasOutstandingIOU: reportData.stateNum === 1 && reportData.total !== 0, }; } diff --git a/src/pages/iou/IOUModal.js b/src/pages/iou/IOUModal.js index 14b4a4315b48..bf5accfddfef 100644 --- a/src/pages/iou/IOUModal.js +++ b/src/pages/iou/IOUModal.js @@ -172,7 +172,6 @@ class IOUModal extends Component { }; console.debug(params); - createIOUTransaction(params); } From 62adbe4ace0a6d4485f9dd2199b7e3b85f0b7cdf Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 22 Apr 2021 16:02:12 -0600 Subject: [PATCH 4/9] Fix the PR template --- .github/PULL_REQUEST_TEMPLATE.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index d4a851edda6f..595152e18d45 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,10 +1,10 @@ - + ### Details - + ### Fixed Issues - + Fixes GH_LINK ### Tests From 6e024b06e8b74bd003edf3362198163afea5fb53 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Fri, 23 Apr 2021 14:18:29 -0600 Subject: [PATCH 5/9] Fix a react prop warning --- src/components/IOUConfirmationList.js | 13 +++++++++---- src/libs/OptionsListUtils.js | 11 ++++------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/components/IOUConfirmationList.js b/src/components/IOUConfirmationList.js index cd193c0bd090..188e93b44bf6 100644 --- a/src/components/IOUConfirmationList.js +++ b/src/components/IOUConfirmationList.js @@ -104,7 +104,7 @@ class IOUConfirmationList extends Component { sections.push({ title: 'WHO PAID?', - data: formattedMyPersonalDetails, + data: [formattedMyPersonalDetails], shouldShow: true, indexOffset: 0, }); @@ -175,8 +175,13 @@ class IOUConfirmationList extends Component { * @returns {Array} */ getAllOptionsAsSelected() { - return [...this.props.participants, - getIOUConfirmationOptionsFromMyPersonalDetail(this.props.myPersonalDetails)]; + if (!this.props.hasMultipleParticipants) { + return []; + } + return [ + ...this.props.participants, + getIOUConfirmationOptionsFromMyPersonalDetail(this.props.myPersonalDetails), + ]; } /** @@ -214,7 +219,7 @@ class IOUConfirmationList extends Component { forceTextUnreadStyle canSelectMultipleOptions={this.props.hasMultipleParticipants} disableFocusOptions - selectedOptions={this.props.hasMultipleParticipants ? this.getAllOptionsAsSelected() : []} + selectedOptions={this.getAllOptionsAsSelected()} /> diff --git a/src/libs/OptionsListUtils.js b/src/libs/OptionsListUtils.js index c970e7666aa5..0aac23555bc2 100644 --- a/src/libs/OptionsListUtils.js +++ b/src/libs/OptionsListUtils.js @@ -357,18 +357,15 @@ function getNewChatOptions( * * @param {Object} myPersonalDetail * @param {String} amountText - * @returns {Array} + * @returns {Object} */ -function getIOUConfirmationOptionsFromMyPersonalDetail( - myPersonalDetail, - amountText, -) { - return [{ +function getIOUConfirmationOptionsFromMyPersonalDetail(myPersonalDetail, amountText) { + return { text: myPersonalDetail.displayName, alternateText: myPersonalDetail.login, icons: [myPersonalDetail.avatar], descriptiveText: amountText, - }]; + }; } /** From 0f278f393ebf601378e8b99db324a74b703df826 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Fri, 23 Apr 2021 14:20:07 -0600 Subject: [PATCH 6/9] Removing debug log --- src/pages/iou/IOUModal.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/pages/iou/IOUModal.js b/src/pages/iou/IOUModal.js index bf5accfddfef..f836801dbb22 100644 --- a/src/pages/iou/IOUModal.js +++ b/src/pages/iou/IOUModal.js @@ -162,17 +162,14 @@ class IOUModal extends Component { return; } - const params = { + createIOUTransaction({ comment: this.state.comment, // should send in cents to API amount: this.state.amount * 100, currency: this.state.selectedCurrency, debtorEmail: this.state.participants[0].login, - }; - - console.debug(params); - createIOUTransaction(params); + }); } render() { From 827c7b7d707217979b8b9b7f96575444d6400c72 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Mon, 26 Apr 2021 10:39:11 -0600 Subject: [PATCH 7/9] Only update reports if it can locate the proper ID --- src/libs/actions/IOU.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 9d08fdbbbeb4..488e683f77e8 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -30,27 +30,28 @@ function getIOUReportsForNewTransaction(requestParams) { shouldLoadOptionalKeys: true, includePinnedReports: true, }) - .then(({reportsData}) => { + .then(({reports}) => { const chatReportsToUpdate = {}; const iouReportsToUpdate = {}; - _.each(reportsData, (reportData) => { + _.each(reports, (reportData) => { // First, the existing chat report needs updated with the details about the new IOU - const chatReportIDForReport = _.chain(requestParams) - .findWhere(requestParams, {reportID: reportData.reportID}) - .pluck('chatReportID') - .value(); - const chatReportKey = `${ONYXKEYS.COLLECTION.REPORT}${chatReportIDForReport}`; - chatReportsToUpdate[chatReportKey] = { - iouReportID: reportData.reportID, - total: reportData.total, - stateNum: reportData.stateNum, - hasOutstandingIOU: reportData.hasOutstandingIOU, - }; + const paramsForIOUReport = _.findWhere(requestParams, {reportID: reportData.reportID}); + if (paramsForIOUReport && paramsForIOUReport.chatReportID) { + const chatReportKey = `${ONYXKEYS.COLLECTION.REPORT}${paramsForIOUReport.chatReportID}`; + chatReportsToUpdate[chatReportKey] = { + iouReportID: reportData.reportID, + total: reportData.total, + stateNum: reportData.stateNum, + hasOutstandingIOU: true, + }; + console.log(JSON.stringify(chatReportsToUpdate)); - // Second, the IOU report needs updated with the new IOU details too - const iouReportKey = `${ONYXKEYS.COLLECTION.REPORT_IOUS}${reportData.reportID}`; - iouReportsToUpdate[iouReportKey] = getSimplifiedIOUReport(reportData, reportData.reportID); + // Second, the IOU report needs updated with the new IOU details too + const iouReportKey = `${ONYXKEYS.COLLECTION.REPORT_IOUS}${reportData.reportID}`; + iouReportsToUpdate[iouReportKey] = getSimplifiedIOUReport(reportData, reportData.reportID); + console.log(JSON.stringify(iouReportsToUpdate)); + } }); // Now, merge the updated objects into our store From 7fef891e910b444a9fa672d633bf22436a13185a Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Mon, 26 Apr 2021 13:51:16 -0600 Subject: [PATCH 8/9] Update Onyx to latest version --- package-lock.json | 10 +++++----- package.json | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6894a4dd8fec..97db0b7c45ff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21813,19 +21813,19 @@ } }, "react-native-onyx": { - "version": "git+https://github.com/Expensify/react-native-onyx.git#3635fc34f00c8c8f97500b97fd42251ab4d22ab5", - "from": "git+https://github.com/Expensify/react-native-onyx.git#3635fc34f00c8c8f97500b97fd42251ab4d22ab5", + "version": "git+https://github.com/Expensify/react-native-onyx.git#accabbd24b5d9a4556b9619a47ba081325622c46", + "from": "git+https://github.com/Expensify/react-native-onyx.git#accabbd24b5d9a4556b9619a47ba081325622c46", "requires": { "@react-native-community/async-storage": "^1.12.1", - "expensify-common": "git+https://github.com/Expensify/expensify-common.git#3d8fc7500ddd24cd4a543e6e160d4f1ad97cc145", + "expensify-common": "git+https://github.com/Expensify/expensify-common.git#679fc86cfc4f9bc701e3757d583d74057edbbe28", "lodash": "4.17.21", "react": "^16.13.1", "underscore": "^1.11.0" }, "dependencies": { "expensify-common": { - "version": "git+https://github.com/Expensify/expensify-common.git#3d8fc7500ddd24cd4a543e6e160d4f1ad97cc145", - "from": "git+https://github.com/Expensify/expensify-common.git#3d8fc7500ddd24cd4a543e6e160d4f1ad97cc145", + "version": "git+https://github.com/Expensify/expensify-common.git#679fc86cfc4f9bc701e3757d583d74057edbbe28", + "from": "git+https://github.com/Expensify/expensify-common.git#679fc86cfc4f9bc701e3757d583d74057edbbe28", "requires": { "classnames": "2.2.5", "clipboard": "2.0.4", diff --git a/package.json b/package.json index 5d5f8842bed2..40adb056cb68 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "react-native-image-picker": "^2.3.3", "react-native-keyboard-spacer": "^0.4.1", "react-native-modal": "^11.5.6", - "react-native-onyx": "git+https://github.com/Expensify/react-native-onyx.git#3635fc34f00c8c8f97500b97fd42251ab4d22ab5", + "react-native-onyx": "git+https://github.com/Expensify/react-native-onyx.git#accabbd24b5d9a4556b9619a47ba081325622c46", "react-native-pdf": "^6.2.2", "react-native-picker-select": "8.0.4", "react-native-reanimated": "1.13.2", From c67ca96dde7bfafbf95e29f13331ff9915a8e473 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Mon, 26 Apr 2021 14:00:05 -0600 Subject: [PATCH 9/9] Remove debug statements --- src/libs/actions/IOU.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 488e683f77e8..4915654ccc21 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -45,12 +45,10 @@ function getIOUReportsForNewTransaction(requestParams) { stateNum: reportData.stateNum, hasOutstandingIOU: true, }; - console.log(JSON.stringify(chatReportsToUpdate)); // Second, the IOU report needs updated with the new IOU details too const iouReportKey = `${ONYXKEYS.COLLECTION.REPORT_IOUS}${reportData.reportID}`; iouReportsToUpdate[iouReportKey] = getSimplifiedIOUReport(reportData, reportData.reportID); - console.log(JSON.stringify(iouReportsToUpdate)); } });