From cb0eec30d6212ae75dd82dd44ef703791d92f8bd Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 14 Dec 2023 16:48:25 +0100 Subject: [PATCH 1/6] clear next steps on actions --- src/libs/actions/IOU.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 7513989d08c4..690b7a7e7cae 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2791,6 +2791,11 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD, value: {[iouReport.policyID]: paymentMethodType}, }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`, + value: null, + }, ]; const successData = [ @@ -2910,7 +2915,12 @@ function approveMoneyRequest(expenseReport) { statusNum: CONST.REPORT.STATUS.APPROVED, }, }; - const optimisticData = [optimisticIOUReportData, optimisticReportActionsData]; + const optimisticNextStepsData = { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, + value: null, + }; + const optimisticData = [optimisticIOUReportData, optimisticReportActionsData, optimisticNextStepsData]; const successData = [ { @@ -2969,6 +2979,11 @@ function submitReport(expenseReport) { statusNum: CONST.REPORT.STATUS.SUBMITTED, }, }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, + value: null, + }, ...(parentReport.reportID ? [ { From ef38cc0f0a52eeac05a26a51227734bc59a7bf50 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 14 Dec 2023 18:41:44 +0100 Subject: [PATCH 2/6] set prev state when failure --- src/libs/actions/IOU.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 690b7a7e7cae..794a75662833 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -119,6 +119,18 @@ Onyx.connect({ }, }); +const nextSteps = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.NEXT_STEP, + callback: (val, key) => { + if (!key || !val) { + return; + } + + nextSteps[key] = val; + }, +}); + /** * Initialize money request info * @param {String} reportID to attach the transaction to @@ -2751,6 +2763,8 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho optimisticReportPreviewAction = ReportUtils.updateReportPreview(iouReport, reportPreviewAction, true); } + const currentNextStep = lodashGet(nextSteps, `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`, null); + const optimisticData = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -2820,6 +2834,11 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`, + value: currentNextStep, + }, ]; // In case the report preview action is loaded locally, let's update it. @@ -2892,6 +2911,8 @@ function sendMoneyWithWallet(report, amount, currency, comment, managerID, recip } function approveMoneyRequest(expenseReport) { + const currentNextStep = lodashGet(nextSteps, `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, null); + const optimisticApprovedReportAction = ReportUtils.buildOptimisticApprovedReportAction(expenseReport.total, expenseReport.currency, expenseReport.reportID); const optimisticReportActionsData = { @@ -2944,6 +2965,11 @@ function approveMoneyRequest(expenseReport) { }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, + value: currentNextStep, + }, ]; API.write('ApproveMoneyRequest', {reportID: expenseReport.reportID, approvedReportActionID: optimisticApprovedReportAction.reportActionID}, {optimisticData, successData, failureData}); @@ -2953,6 +2979,8 @@ function approveMoneyRequest(expenseReport) { * @param {Object} expenseReport */ function submitReport(expenseReport) { + const currentNextStep = lodashGet(nextSteps, `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, null); + const optimisticSubmittedReportAction = ReportUtils.buildOptimisticSubmittedReportAction(expenseReport.total, expenseReport.currency, expenseReport.reportID); const parentReport = ReportUtils.getReport(expenseReport.parentReportID); @@ -3029,6 +3057,11 @@ function submitReport(expenseReport) { stateNum: CONST.REPORT.STATE_NUM.OPEN, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, + value: currentNextStep, + }, ...(parentReport.reportID ? [ { From 345e928bd3d9b24e009575ad4b80ad95ecbff3dc Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 15 Dec 2023 16:40:22 +0100 Subject: [PATCH 3/6] add waitForCollectionCallback config --- src/libs/actions/IOU.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 794a75662833..6c63e1eaaacc 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -122,6 +122,7 @@ Onyx.connect({ const nextSteps = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.NEXT_STEP, + waitForCollectionCallback: true, callback: (val, key) => { if (!key || !val) { return; From 18a7ccd3cd702422b6d908c81ef55d37f4abf2e2 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 15 Dec 2023 16:58:36 +0100 Subject: [PATCH 4/6] handle null next step --- src/libs/actions/IOU.js | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 6c63e1eaaacc..30cdeb5fe76a 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -119,16 +119,12 @@ Onyx.connect({ }, }); -const nextSteps = {}; +let nextSteps = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.NEXT_STEP, waitForCollectionCallback: true, - callback: (val, key) => { - if (!key || !val) { - return; - } - - nextSteps[key] = val; + callback: (val) => { + nextSteps = val || {}; }, }); @@ -2835,12 +2831,15 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho }, }, }, - { + ]; + + if (currentNextStep !== null) { + failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`, value: currentNextStep, - }, - ]; + }); + } // In case the report preview action is loaded locally, let's update it. if (optimisticReportPreviewAction) { @@ -2966,12 +2965,15 @@ function approveMoneyRequest(expenseReport) { }, }, }, - { + ]; + + if (currentNextStep !== null) { + failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, value: currentNextStep, - }, - ]; + }); + } API.write('ApproveMoneyRequest', {reportID: expenseReport.reportID, approvedReportActionID: optimisticApprovedReportAction.reportActionID}, {optimisticData, successData, failureData}); } @@ -3058,11 +3060,6 @@ function submitReport(expenseReport) { stateNum: CONST.REPORT.STATE_NUM.OPEN, }, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, - value: currentNextStep, - }, ...(parentReport.reportID ? [ { @@ -3077,6 +3074,14 @@ function submitReport(expenseReport) { : []), ]; + if (currentNextStep !== null) { + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, + value: currentNextStep, + }); + } + API.write( 'SubmitReport', { From 6f52ee7a9de304632e18134166f3fad1adc999ab Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 15 Dec 2023 17:13:24 +0100 Subject: [PATCH 5/6] improve performance --- src/libs/actions/IOU.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 30cdeb5fe76a..9a02c16024ce 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2802,11 +2802,6 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD, value: {[iouReport.policyID]: paymentMethodType}, }, - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`, - value: null, - }, ]; const successData = [ @@ -2833,7 +2828,12 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho }, ]; - if (currentNextStep !== null) { + if (!_.isNull(currentNextStep)) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`, + value: null, + }); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`, @@ -2941,7 +2941,7 @@ function approveMoneyRequest(expenseReport) { key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, value: null, }; - const optimisticData = [optimisticIOUReportData, optimisticReportActionsData, optimisticNextStepsData]; + const optimisticData = [optimisticIOUReportData, optimisticReportActionsData]; const successData = [ { @@ -2967,7 +2967,8 @@ function approveMoneyRequest(expenseReport) { }, ]; - if (currentNextStep !== null) { + if (!_.isNull(currentNextStep)) { + optimisticData.push(optimisticNextStepsData); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, @@ -3010,11 +3011,6 @@ function submitReport(expenseReport) { statusNum: CONST.REPORT.STATUS.SUBMITTED, }, }, - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, - value: null, - }, ...(parentReport.reportID ? [ { @@ -3074,7 +3070,12 @@ function submitReport(expenseReport) { : []), ]; - if (currentNextStep !== null) { + if (!_.isNull(currentNextStep)) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, + value: null, + }); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, From 5b6700cd644d54e119b891e91513136b5561a1d8 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 15 Dec 2023 17:23:58 +0100 Subject: [PATCH 6/6] remove variable --- src/libs/actions/IOU.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 9a02c16024ce..1e69bd236f0f 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2936,11 +2936,6 @@ function approveMoneyRequest(expenseReport) { statusNum: CONST.REPORT.STATUS.APPROVED, }, }; - const optimisticNextStepsData = { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, - value: null, - }; const optimisticData = [optimisticIOUReportData, optimisticReportActionsData]; const successData = [ @@ -2968,7 +2963,11 @@ function approveMoneyRequest(expenseReport) { ]; if (!_.isNull(currentNextStep)) { - optimisticData.push(optimisticNextStepsData); + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, + value: null, + }); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`,