From ad42f5ac57fc0b96b9aacd36504c1aa377e4cd04 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Wed, 20 Sep 2023 11:43:39 +0200 Subject: [PATCH 01/28] re-test From d07017bbcf5de6c5933a2d91dc970840c0161bc7 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Wed, 20 Sep 2023 12:54:51 +0200 Subject: [PATCH 02/28] connect report of participants of MoneyRequestCategoryPage --- src/pages/iou/MoneyRequestCategoryPage.js | 26 ++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/pages/iou/MoneyRequestCategoryPage.js b/src/pages/iou/MoneyRequestCategoryPage.js index 80b88a762609..ac4cae65229c 100644 --- a/src/pages/iou/MoneyRequestCategoryPage.js +++ b/src/pages/iou/MoneyRequestCategoryPage.js @@ -4,6 +4,7 @@ import lodashGet from 'lodash/get'; import {withOnyx} from 'react-native-onyx'; import ROUTES from '../../ROUTES'; import Navigation from '../../libs/Navigation/Navigation'; +import compose from '../../libs/compose'; import useLocalize from '../../hooks/useLocalize'; import ScreenWrapper from '../../components/ScreenWrapper'; import HeaderWithBackButton from '../../components/HeaderWithBackButton'; @@ -65,8 +66,23 @@ MoneyRequestCategoryPage.displayName = 'MoneyRequestCategoryPage'; MoneyRequestCategoryPage.propTypes = propTypes; MoneyRequestCategoryPage.defaultProps = defaultProps; -export default withOnyx({ - report: { - key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${lodashGet(route, 'params.reportID', '')}`, - }, -})(MoneyRequestCategoryPage); +export default compose( + withOnyx({ + iou: { + key: ONYXKEYS.IOU, + }, + }), + withOnyx({ + report: { + key: ({route, iou}) => { + let reportID = lodashGet(route, 'params.reportID', ''); + if (!reportID) { + // When the money request creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. + // Get the report id from the participants list on the IOU object stored in Onyx. + reportID = lodashGet(iou, 'participants.0.reportID', ''); + } + return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; + }, + }, + }), +)(MoneyRequestCategoryPage); From 16a1bbaa488c1030bc7437564409e526e11d1e42 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Wed, 20 Sep 2023 16:01:45 +0200 Subject: [PATCH 03/28] attach category to split bill request --- src/libs/actions/IOU.js | 28 ++++++++++++++----- .../iou/steps/MoneyRequestConfirmPage.js | 3 ++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 9e8dadbd53b7..9f2c71bf37f1 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -669,11 +669,12 @@ function requestMoney(report, amount, currency, created, merchant, payeeEmail, p * @param {Number} amount - always in the smallest unit of the currency * @param {String} comment * @param {String} currency + * @param {String} category * @param {String} existingSplitChatReportID - the report ID where the split bill happens, could be a group chat or a workspace chat * * @return {Object} */ -function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, existingSplitChatReportID = '') { +function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, existingSplitChatReportID = '') { const currentUserEmailForIOUSplit = OptionsListUtils.addSMSDomainIfPhoneNumber(currentUserLogin); const participantAccountIDs = _.map(participants, (participant) => Number(participant.accountID)); const existingSplitChatReport = @@ -697,6 +698,10 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco '', '', `${Localize.translateLocal('iou.splitBill')} ${Localize.translateLocal('common.with')} ${formattedParticipants} [${DateUtils.getDBTime().slice(0, 10)}]`, + undefined, + undefined, + undefined, + category, ); // Note: The created action must be optimistically generated before the IOU action so there's no chance that the created action appears after the IOU action in the chat @@ -824,8 +829,8 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco const hasMultipleParticipants = participants.length > 1; _.each(participants, (participant) => { // In case the participant is a worskapce, email & accountID should remain undefined and won't be used in the rest of this code - const email = isOwnPolicyExpenseChat ? '' : OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login).toLowerCase(); - const accountID = isOwnPolicyExpenseChat ? 0 : Number(participant.accountID); + const email = isOwnPolicyExpenseChat ? '' : OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login || '').toLowerCase(); + const accountID = isOwnPolicyExpenseChat ? 0 : Number(participant.accountID) || 0; if (email === currentUserEmailForIOUSplit) { return; } @@ -877,6 +882,11 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco '', CONST.IOU.MONEY_REQUEST_TYPE.SPLIT, splitTransaction.transactionID, + undefined, + undefined, + undefined, + undefined, + category, ); // STEP 4: Build optimistic reportActions. We need: @@ -976,10 +986,11 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco * @param {Number} amount - always in smallest currency unit * @param {String} comment * @param {String} currency + * @param {String} category * @param {String} existingSplitChatReportID - Either a group DM or a workspace chat */ -function splitBill(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, existingSplitChatReportID = '') { - const {splitData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, existingSplitChatReportID); +function splitBill(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, existingSplitChatReportID = '') { + const {splitData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, existingSplitChatReportID); API.write( 'SplitBill', @@ -989,6 +1000,7 @@ function splitBill(participants, currentUserLogin, currentUserAccountID, amount, splits: JSON.stringify(splits), currency, comment, + category, transactionID: splitData.transactionID, reportActionID: splitData.reportActionID, createdReportActionID: splitData.createdReportActionID, @@ -1009,9 +1021,10 @@ function splitBill(participants, currentUserLogin, currentUserAccountID, amount, * @param {Number} amount - always in smallest currency unit * @param {String} comment * @param {String} currency + * @param {String} category */ -function splitBillAndOpenReport(participants, currentUserLogin, currentUserAccountID, amount, comment, currency) { - const {splitData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency); +function splitBillAndOpenReport(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category) { + const {splitData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category); API.write( 'SplitBillAndOpenReport', @@ -1021,6 +1034,7 @@ function splitBillAndOpenReport(participants, currentUserLogin, currentUserAccou splits: JSON.stringify(splits), currency, comment, + category, transactionID: splitData.transactionID, reportActionID: splitData.reportActionID, createdReportActionID: splitData.createdReportActionID, diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index 224f661915d8..e22254ff19d0 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -192,6 +192,7 @@ function MoneyRequestConfirmPage(props) { props.iou.amount, trimmedComment, props.iou.currency, + props.iou.category, reportID.current, ); return; @@ -206,6 +207,7 @@ function MoneyRequestConfirmPage(props) { props.iou.amount, trimmedComment, props.iou.currency, + props.iou.category, ); return; } @@ -230,6 +232,7 @@ function MoneyRequestConfirmPage(props) { props.currentUserPersonalDetails.login, props.currentUserPersonalDetails.accountID, props.iou.currency, + props.iou.category, props.iou.receiptPath, props.iou.receiptSource, isDistanceRequest, From 576ad9391f396b24523a7a664ff1ac2d8e3239aa Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 21 Sep 2023 14:54:47 +0200 Subject: [PATCH 04/28] fix crash when a participant a workspace --- src/libs/actions/IOU.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index dbbebce3a5cb..75049004063f 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -886,9 +886,11 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco const hasMultipleParticipants = participants.length > 1; _.each(participants, (participant) => { + const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(participant); + // In case the participant is a worskapce, email & accountID should remain undefined and won't be used in the rest of this code - const email = isOwnPolicyExpenseChat ? '' : OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login || '').toLowerCase(); - const accountID = isOwnPolicyExpenseChat ? 0 : Number(participant.accountID) || 0; + const email = isOwnPolicyExpenseChat || isPolicyExpenseChat ? '' : OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login).toLowerCase(); + const accountID = isOwnPolicyExpenseChat || isPolicyExpenseChat ? 0 : Number(participant.accountID); if (email === currentUserEmailForIOUSplit) { return; } @@ -1050,7 +1052,6 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco */ function splitBill(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, existingSplitChatReportID = '') { const {splitData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, existingSplitChatReportID); - API.write( 'SplitBill', { From f4f86d8538a055ce6f9c4e30fe9f89be0357c854 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 21 Sep 2023 15:18:01 +0200 Subject: [PATCH 05/28] fix policy and report props of MoneyRequestConfirmationList --- src/pages/iou/steps/MoneyRequestConfirmPage.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index f9d80e784e22..6b90a4bc5f37 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -322,14 +322,14 @@ function MoneyRequestConfirmPage(props) { receiptPath={props.iou.receiptPath} receiptSource={props.iou.receiptSource} iouType={iouType.current} - reportID={reportID.current} + reportID={reportID.current || lodashGet(props.iou, 'participants.0.reportID', '')} // The participants can only be modified when the action is initiated from directly within a group chat and not the floating-action-button. // This is because when there is a group of people, say they are on a trip, and you have some shared expenses with some of the people, // but not all of them (maybe someone skipped out on dinner). Then it's nice to be able to select/deselect people from the group chat bill // split rather than forcing the user to create a new group, just for that expense. The reportID is empty, when the action was initiated from // the floating-action-button (since it is something that exists outside the context of a report). canModifyParticipants={!_.isEmpty(reportID.current)} - policyID={props.report.policyID} + policyID={props.policy.id} bankAccountRoute={ReportUtils.getBankAccountRoute(props.report)} iouMerchant={props.iou.merchant} iouCreated={props.iou.created} From e7ba3fc0716f3c11f5ea166b0b969f6ad70eafd2 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 21 Sep 2023 15:20:42 +0200 Subject: [PATCH 06/28] fix props --- src/pages/iou/steps/MoneyRequestConfirmPage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index 6b90a4bc5f37..c43635d484d3 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -329,7 +329,7 @@ function MoneyRequestConfirmPage(props) { // split rather than forcing the user to create a new group, just for that expense. The reportID is empty, when the action was initiated from // the floating-action-button (since it is something that exists outside the context of a report). canModifyParticipants={!_.isEmpty(reportID.current)} - policyID={props.policy.id} + policyID={props.report.policyID} bankAccountRoute={ReportUtils.getBankAccountRoute(props.report)} iouMerchant={props.iou.merchant} iouCreated={props.iou.created} From aa8f4f6f409a37fda24c48c2a5f093f656d50cab Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 21 Sep 2023 15:46:12 +0200 Subject: [PATCH 07/28] fix adding recently categories --- src/libs/actions/IOU.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 75049004063f..6b3c82eb21fb 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -465,13 +465,16 @@ function getMoneyRequestInformation( billable, ); - const uniquePolicyRecentlyUsedCategories = allRecentlyUsedCategories - ? _.filter( - allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${iouReport.policyID}`], - (recentlyUsedPolicyCategory) => recentlyUsedPolicyCategory !== category, - ) - : []; - const optimisticPolicyRecentlyUsedCategories = [category, ...uniquePolicyRecentlyUsedCategories]; + let optimisticPolicyRecentlyUsedCategories; + if (category) { + const uniquePolicyRecentlyUsedCategories = allRecentlyUsedCategories + ? _.filter( + allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${iouReport.policyID}`], + (recentlyUsedPolicyCategory) => recentlyUsedPolicyCategory !== category, + ) + : []; + optimisticPolicyRecentlyUsedCategories = [category, ...uniquePolicyRecentlyUsedCategories]; + } const optimisticPolicyRecentlyUsedTags = {}; const recentlyUsedPolicyTags = allRecentlyUsedTags[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${iouReport.policyID}`]; From 495894eae7a3e5aa89b3c782ed8e686e91e97291 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 21 Sep 2023 15:54:12 +0200 Subject: [PATCH 08/28] add recently categories for split bills optimistic --- src/libs/actions/IOU.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 6b3c82eb21fb..f793d7e776c4 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -990,6 +990,17 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco oneOnOneReportPreviewAction = ReportUtils.buildOptimisticReportPreview(oneOnOneChatReport, oneOnOneIOUReport); } + let optimisticPolicyRecentlyUsedCategories; + if (category && isPolicyExpenseChat) { + const uniquePolicyRecentlyUsedCategories = allRecentlyUsedCategories + ? _.filter( + allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${participant.policyID}`], + (recentlyUsedPolicyCategory) => recentlyUsedPolicyCategory !== category, + ) + : []; + optimisticPolicyRecentlyUsedCategories = [category, ...uniquePolicyRecentlyUsedCategories]; + } + // STEP 5: Build Onyx Data const [oneOnOneOptimisticData, oneOnOneSuccessData, oneOnOneFailureData] = buildOnyxDataForMoneyRequest( oneOnOneChatReport, @@ -1000,7 +1011,7 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco oneOnOneIOUAction, oneOnOnePersonalDetailListAction, oneOnOneReportPreviewAction, - [], + optimisticPolicyRecentlyUsedCategories, {}, isNewOneOnOneChatReport, shouldCreateNewOneOnOneIOUReport, From cd5a6c2687e321ec1aa6436ad56baa509facd7ab Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 21 Sep 2023 19:13:35 +0200 Subject: [PATCH 09/28] use one withOnyx --- src/pages/iou/MoneyRequestCategoryPage.js | 35 ++++++++++------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/pages/iou/MoneyRequestCategoryPage.js b/src/pages/iou/MoneyRequestCategoryPage.js index 246e94a6e261..5e94ec498b01 100644 --- a/src/pages/iou/MoneyRequestCategoryPage.js +++ b/src/pages/iou/MoneyRequestCategoryPage.js @@ -4,7 +4,6 @@ import lodashGet from 'lodash/get'; import {withOnyx} from 'react-native-onyx'; import ROUTES from '../../ROUTES'; import Navigation from '../../libs/Navigation/Navigation'; -import compose from '../../libs/compose'; import useLocalize from '../../hooks/useLocalize'; import ScreenWrapper from '../../components/ScreenWrapper'; import HeaderWithBackButton from '../../components/HeaderWithBackButton'; @@ -83,23 +82,19 @@ MoneyRequestCategoryPage.displayName = 'MoneyRequestCategoryPage'; MoneyRequestCategoryPage.propTypes = propTypes; MoneyRequestCategoryPage.defaultProps = defaultProps; -export default compose( - withOnyx({ - iou: { - key: ONYXKEYS.IOU, +export default withOnyx({ + iou: { + key: ONYXKEYS.IOU, + }, + report: { + key: ({route, iou}) => { + let reportID = lodashGet(route, 'params.reportID', ''); + if (!reportID) { + // When the money request creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. + // Get the report id from the participants list on the IOU object stored in Onyx. + reportID = lodashGet(iou, 'participants.0.reportID', ''); + } + return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; }, - }), - withOnyx({ - report: { - key: ({route, iou}) => { - let reportID = lodashGet(route, 'params.reportID', ''); - if (!reportID) { - // When the money request creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. - // Get the report id from the participants list on the IOU object stored in Onyx. - reportID = lodashGet(iou, 'participants.0.reportID', ''); - } - return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; - }, - }, - }), -)(MoneyRequestCategoryPage); + }, +})(MoneyRequestCategoryPage); From 57b61e5645a59f605419dffaab8172c0bd13bd4a Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 22 Sep 2023 14:05:26 +0200 Subject: [PATCH 10/28] add comments --- src/libs/actions/IOU.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 7183baebdd55..818f4ab9633b 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -889,9 +889,10 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco const hasMultipleParticipants = participants.length > 1; _.each(participants, (participant) => { + // In a case when a participant is a workspace, even when a current user is not an owner of the workspace const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(participant); - // In case the participant is a worskapce, email & accountID should remain undefined and won't be used in the rest of this code + // In case the participant is a workspace, email & accountID should remain undefined and won't be used in the rest of this code const email = isOwnPolicyExpenseChat || isPolicyExpenseChat ? '' : OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login).toLowerCase(); const accountID = isOwnPolicyExpenseChat || isPolicyExpenseChat ? 0 : Number(participant.accountID); if (email === currentUserEmailForIOUSplit) { @@ -990,6 +991,7 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco oneOnOneReportPreviewAction = ReportUtils.buildOptimisticReportPreview(oneOnOneChatReport, oneOnOneIOUReport); } + // Add category to optimistic policy recently used categories when a participant is a workspace let optimisticPolicyRecentlyUsedCategories; if (category && isPolicyExpenseChat) { const uniquePolicyRecentlyUsedCategories = allRecentlyUsedCategories From a036c4da73ba6cbf9da4d9ee40a2ebdee479edf0 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 22 Sep 2023 15:11:11 +0200 Subject: [PATCH 11/28] move isPolicyExpenseChat to parent --- src/components/MoneyRequestConfirmationList.js | 10 +++++----- src/pages/iou/steps/MoneyRequestConfirmPage.js | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.js b/src/components/MoneyRequestConfirmationList.js index 13471407914f..cdcb875f163f 100755 --- a/src/components/MoneyRequestConfirmationList.js +++ b/src/components/MoneyRequestConfirmationList.js @@ -137,6 +137,9 @@ const propTypes = { /** Whether the money request is a distance request */ isDistanceRequest: PropTypes.bool, + /** A flag for verifying that the current report is a workspace */ + isPolicyExpenseChat: PropTypes.bool.isRequired, + /* Onyx Props */ /** Collection of categories attached to a policy */ policyCategories: PropTypes.objectOf(categoryPropTypes), @@ -195,11 +198,8 @@ function MoneyRequestConfirmationList(props) { const distance = lodashGet(transaction, 'routes.route0.distance', 0); const shouldCalculateDistanceAmount = props.isDistanceRequest && props.iouAmount === 0; - // A flag for verifying that the current report is a sub-report of a workspace chat - const isPolicyExpenseChat = useMemo(() => ReportUtils.isPolicyExpenseChat(ReportUtils.getRootParentReport(ReportUtils.getReport(props.reportID))), [props.reportID]); - // A flag for showing the categories field - const shouldShowCategories = isPolicyExpenseChat && Permissions.canUseCategories(props.betas) && OptionsListUtils.hasEnabledOptions(_.values(props.policyCategories)); + const shouldShowCategories = props.isPolicyExpenseChat && Permissions.canUseCategories(props.betas) && OptionsListUtils.hasEnabledOptions(_.values(props.policyCategories)); // Fetches the first tag list of the policy const tagListKey = _.first(_.keys(props.policyTags)); @@ -207,7 +207,7 @@ function MoneyRequestConfirmationList(props) { const tagListName = lodashGet(props.policyTags, [tagListKey, 'name'], ''); const canUseTags = Permissions.canUseTags(props.betas); // A flag for showing the tags field - const shouldShowTags = isPolicyExpenseChat && canUseTags && _.any(tagList, (tag) => tag.enabled); + const shouldShowTags = props.isPolicyExpenseChat && canUseTags && _.any(tagList, (tag) => tag.enabled); // A flag for showing the billable field const shouldShowBillable = canUseTags && !lodashGet(props.policy, 'disabledFields.defaultBillable', true); diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index 3b9f7dcebcfa..5916e0662c99 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -74,6 +74,7 @@ function MoneyRequestConfirmPage(props) { }), [props.iou.participants, props.personalDetails], ); + const isPolicyExpenseChat = useMemo(() => ReportUtils.isPolicyExpenseChat(ReportUtils.getRootParentReport(props.report)), [props.report]); useEffect(() => { const policyExpenseChat = _.find(participants, (participant) => participant.isPolicyExpenseChat); @@ -325,7 +326,8 @@ function MoneyRequestConfirmPage(props) { receiptPath={props.iou.receiptPath} receiptSource={props.iou.receiptSource} iouType={iouType.current} - reportID={reportID.current || lodashGet(props.iou, 'participants.0.reportID', '')} + reportID={reportID.current} + isPolicyExpenseChat={isPolicyExpenseChat} // The participants can only be modified when the action is initiated from directly within a group chat and not the floating-action-button. // This is because when there is a group of people, say they are on a trip, and you have some shared expenses with some of the people, // but not all of them (maybe someone skipped out on dinner). Then it's nice to be able to select/deselect people from the group chat bill From ddcf5d6fa946017467d8c454bd4fed602ce093ef Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 22 Sep 2023 15:11:22 +0200 Subject: [PATCH 12/28] hack onyx forwarding --- src/pages/iou/MoneyRequestCategoryPage.js | 38 ++++++++++++++--------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/pages/iou/MoneyRequestCategoryPage.js b/src/pages/iou/MoneyRequestCategoryPage.js index 36fa5624cc58..65e514f8582e 100644 --- a/src/pages/iou/MoneyRequestCategoryPage.js +++ b/src/pages/iou/MoneyRequestCategoryPage.js @@ -2,6 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import lodashGet from 'lodash/get'; import {withOnyx} from 'react-native-onyx'; +import compose from '../../libs/compose'; import ROUTES from '../../ROUTES'; import Navigation from '../../libs/Navigation/Navigation'; import useLocalize from '../../hooks/useLocalize'; @@ -83,19 +84,26 @@ MoneyRequestCategoryPage.displayName = 'MoneyRequestCategoryPage'; MoneyRequestCategoryPage.propTypes = propTypes; MoneyRequestCategoryPage.defaultProps = defaultProps; -export default withOnyx({ - iou: { - key: ONYXKEYS.IOU, - }, - report: { - key: ({route, iou}) => { - let reportID = lodashGet(route, 'params.reportID', ''); - if (!reportID) { - // When the money request creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. - // Get the report id from the participants list on the IOU object stored in Onyx. - reportID = lodashGet(iou, 'participants.0.reportID', ''); - } - return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; +export default compose( + withOnyx({ + iou: { + key: ONYXKEYS.IOU, }, - }, -})(MoneyRequestCategoryPage); + }), + // This is a temporary hack to forward forward iou value to next getter. + // There is a problem that with one withOnyx it does not forward value from first to second. + // eslint-disable-next-line rulesdir/no-multiple-onyx-in-file + withOnyx({ + report: { + key: ({route, iou}) => { + let reportID = lodashGet(route, 'params.reportID', ''); + if (!reportID) { + // When the money request creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. + // Get the report id from the participants list on the IOU object stored in Onyx. + reportID = lodashGet(iou, 'participants.0.reportID', ''); + } + return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; + }, + }, + }), +)(MoneyRequestCategoryPage); From 36b69512bfede11cda937f098d35e9bc87640665 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 22 Sep 2023 16:02:08 +0200 Subject: [PATCH 13/28] fix props types --- src/components/MoneyRequestConfirmationList.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/MoneyRequestConfirmationList.js b/src/components/MoneyRequestConfirmationList.js index cdcb875f163f..c04c6be55218 100755 --- a/src/components/MoneyRequestConfirmationList.js +++ b/src/components/MoneyRequestConfirmationList.js @@ -138,7 +138,7 @@ const propTypes = { isDistanceRequest: PropTypes.bool, /** A flag for verifying that the current report is a workspace */ - isPolicyExpenseChat: PropTypes.bool.isRequired, + isPolicyExpenseChat: PropTypes.bool, /* Onyx Props */ /** Collection of categories attached to a policy */ @@ -181,6 +181,7 @@ const defaultProps = { transaction: {}, mileageRate: {unit: CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES, rate: 0, currency: 'USD'}, isDistanceRequest: false, + isPolicyExpenseChat: false, }; function MoneyRequestConfirmationList(props) { From 0bcda516755ab261aa5de9aa1e72216ed0d15c78 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Tue, 26 Sep 2023 15:13:39 +0200 Subject: [PATCH 14/28] unify a fix to get report id on confirm page --- src/pages/iou/MoneyRequestCategoryPage.js | 4 +--- src/pages/iou/MoneyRequestTagPage.js | 17 +++++++++++--- .../iou/steps/MoneyRequestConfirmPage.js | 2 +- .../MoneyRequestParticipantsPage.js | 23 ++++--------------- .../MoneyRequestParticipantsSelector.js | 2 +- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/pages/iou/MoneyRequestCategoryPage.js b/src/pages/iou/MoneyRequestCategoryPage.js index c18a8f1f50df..6a630c2ec705 100644 --- a/src/pages/iou/MoneyRequestCategoryPage.js +++ b/src/pages/iou/MoneyRequestCategoryPage.js @@ -90,15 +90,13 @@ export default compose( key: ONYXKEYS.IOU, }, }), - // This is a temporary hack to forward forward iou value to next getter. - // There is a problem that with one withOnyx it does not forward value from first to second. // eslint-disable-next-line rulesdir/no-multiple-onyx-in-file withOnyx({ report: { key: ({route, iou}) => { let reportID = lodashGet(route, 'params.reportID', ''); if (!reportID) { - // When the money request creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. + // When the money request or split bill creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. // Get the report id from the participants list on the IOU object stored in Onyx. reportID = lodashGet(iou, 'participants.0.reportID', ''); } diff --git a/src/pages/iou/MoneyRequestTagPage.js b/src/pages/iou/MoneyRequestTagPage.js index 43c0cd9d1480..44adf34385c5 100644 --- a/src/pages/iou/MoneyRequestTagPage.js +++ b/src/pages/iou/MoneyRequestTagPage.js @@ -98,14 +98,25 @@ MoneyRequestTagPage.defaultProps = defaultProps; export default compose( withOnyx({ - report: { - key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${lodashGet(route, 'params.reportID')}`, - }, iou: { key: ONYXKEYS.IOU, }, }), // eslint-disable-next-line rulesdir/no-multiple-onyx-in-file + withOnyx({ + report: { + key: ({route, iou}) => { + let reportID = lodashGet(route, 'params.reportID', ''); + if (!reportID) { + // When the money request or split bill creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. + // Get the report id from the participants list on the IOU object stored in Onyx. + reportID = lodashGet(iou, 'participants.0.reportID', ''); + } + return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; + }, + }, + }), + // eslint-disable-next-line rulesdir/no-multiple-onyx-in-file withOnyx({ policyTags: { key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${report ? report.policyID : '0'}`, diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index ee1e346a425b..41fd36c8d8f9 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -381,7 +381,7 @@ export default compose( key: ({route, iou}) => { let reportID = lodashGet(route, 'params.reportID', ''); if (!reportID) { - // When the money request creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. + // When the money request or split bill creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. // Get the report id from the participants list on the IOU object stored in Onyx. reportID = lodashGet(iou, 'participants.0.reportID', ''); } diff --git a/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsPage.js b/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsPage.js index 8d745903eb40..89c18efc4e76 100644 --- a/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsPage.js +++ b/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsPage.js @@ -45,7 +45,6 @@ const defaultProps = { function MoneyRequestParticipantsPage({iou, selectedTab, route}) { const {translate} = useLocalize(); const prevMoneyRequestId = useRef(iou.id); - const isNewReportIDSelectedLocally = useRef(false); const optionsSelectorRef = useRef(); const iouType = useRef(lodashGet(route, 'params.iouType', '')); const reportID = useRef(lodashGet(route, 'params.reportID', '')); @@ -63,19 +62,7 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) { setHeaderTitle(_.isEmpty(iou.participants) ? translate('tabSelector.manual') : translate('iou.split')); }, [iou.participants, isDistanceRequest, translate]); - const navigateToRequestStep = (moneyRequestType, option) => { - if (option.reportID) { - isNewReportIDSelectedLocally.current = true; - IOU.setMoneyRequestId(`${moneyRequestType}${option.reportID}`); - Navigation.navigate(ROUTES.MONEY_REQUEST_CONFIRMATION.getRoute(moneyRequestType, option.reportID)); - return; - } - - IOU.setMoneyRequestId(moneyRequestType); - Navigation.navigate(ROUTES.MONEY_REQUEST_CONFIRMATION.getRoute(moneyRequestType, reportID.current)); - }; - - const navigateToSplitStep = (moneyRequestType) => { + const navigateToConfirmationStep = (moneyRequestType) => { IOU.setMoneyRequestId(moneyRequestType); Navigation.navigate(ROUTES.MONEY_REQUEST_CONFIRMATION.getRoute(moneyRequestType, reportID.current)); }; @@ -88,7 +75,7 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) { // ID in Onyx could change by initiating a new request in a separate browser tab or completing a request if (prevMoneyRequestId.current !== iou.id) { // The ID is cleared on completing a request. In that case, we will do nothing - if (iou.id && !isDistanceRequest && !isSplitRequest && !isNewReportIDSelectedLocally.current) { + if (iou.id && !isDistanceRequest && !isSplitRequest) { navigateBack(true); } return; @@ -96,7 +83,7 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) { // Reset the money request Onyx if the ID in Onyx does not match the ID from params const moneyRequestId = `${iouType.current}${reportID.current}`; - const shouldReset = iou.id !== moneyRequestId && !isNewReportIDSelectedLocally.current; + const shouldReset = iou.id !== moneyRequestId; if (shouldReset) { IOU.resetMoneyRequestInfo(moneyRequestId); } @@ -126,8 +113,8 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) { ref={(el) => (optionsSelectorRef.current = el)} participants={iou.participants} onAddParticipants={IOU.setMoneyRequestParticipants} - navigateToRequest={(option) => navigateToRequestStep(CONST.IOU.MONEY_REQUEST_TYPE.REQUEST, option)} - navigateToSplit={() => navigateToSplitStep(CONST.IOU.MONEY_REQUEST_TYPE.SPLIT)} + navigateToRequest={() => navigateToConfirmationStep(CONST.IOU.MONEY_REQUEST_TYPE.REQUEST)} + navigateToSplit={() => navigateToConfirmationStep(CONST.IOU.MONEY_REQUEST_TYPE.SPLIT)} safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle} iouType={iouType.current} isDistanceRequest={isDistanceRequest} diff --git a/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsSelector.js b/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsSelector.js index 170ee042bffa..a8da1b4ec9ed 100755 --- a/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsSelector.js +++ b/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsSelector.js @@ -161,7 +161,7 @@ function MoneyRequestParticipantsSelector({ onAddParticipants([ {accountID: option.accountID, login: option.login, isPolicyExpenseChat: option.isPolicyExpenseChat, reportID: option.reportID, selected: true, searchText: option.searchText}, ]); - navigateToRequest(option); + navigateToRequest(); }; /** From 6b581a9c5be6b525e40cf50ea1b2887309f8edc0 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 28 Sep 2023 13:23:45 +0200 Subject: [PATCH 15/28] clarify comment --- src/components/MoneyRequestConfirmationList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/MoneyRequestConfirmationList.js b/src/components/MoneyRequestConfirmationList.js index 521eb676491b..621a8e3d1b87 100755 --- a/src/components/MoneyRequestConfirmationList.js +++ b/src/components/MoneyRequestConfirmationList.js @@ -138,7 +138,7 @@ const propTypes = { /** Whether the money request is a distance request */ isDistanceRequest: PropTypes.bool, - /** A flag for verifying that the current report is a workspace */ + /** A flag for verifying that the current report is a sub-report of a workspace chat */ isPolicyExpenseChat: PropTypes.bool, /* Onyx Props */ From d21274f8221b7c18df2703f18cb49520f5ea35e8 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 28 Sep 2023 14:58:57 +0200 Subject: [PATCH 16/28] unify iou report id getter --- src/libs/actions/IOU.js | 12 ++++++++++++ src/pages/iou/MoneyRequestCategoryPage.js | 4 +--- src/pages/iou/MoneyRequestTagPage.js | 4 +--- src/pages/iou/steps/MoneyRequestConfirmPage.js | 4 +--- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index e34774ef5328..d1e566fb2852 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2333,6 +2333,17 @@ function navigateToNextPage(iou, iouType, report, path = '') { Navigation.navigate(ROUTES.MONEY_REQUEST_PARTICIPANTS.getRoute(iouType)); } +/** + * When the money request or split bill creation flow is initialized via FAB, the reportID is not passed as a navigation + * parameter. + * Gets a report id from the first participant of the IOU object stored in Onyx. + * @param {Object} iou + * @returns {String} + */ +function getIOUReportID(iou) { + return lodashGet(iou, 'participants.0.reportID', ''); +} + export { createDistanceRequest, editMoneyRequest, @@ -2363,4 +2374,5 @@ export { navigateToNextPage, updateDistanceRequest, replaceReceipt, + getIOUReportID, }; diff --git a/src/pages/iou/MoneyRequestCategoryPage.js b/src/pages/iou/MoneyRequestCategoryPage.js index 6a630c2ec705..3ef0ce6aaee1 100644 --- a/src/pages/iou/MoneyRequestCategoryPage.js +++ b/src/pages/iou/MoneyRequestCategoryPage.js @@ -96,9 +96,7 @@ export default compose( key: ({route, iou}) => { let reportID = lodashGet(route, 'params.reportID', ''); if (!reportID) { - // When the money request or split bill creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. - // Get the report id from the participants list on the IOU object stored in Onyx. - reportID = lodashGet(iou, 'participants.0.reportID', ''); + reportID = IOU.getIOUReportID(iou); } return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; }, diff --git a/src/pages/iou/MoneyRequestTagPage.js b/src/pages/iou/MoneyRequestTagPage.js index 44adf34385c5..a8ca82eed302 100644 --- a/src/pages/iou/MoneyRequestTagPage.js +++ b/src/pages/iou/MoneyRequestTagPage.js @@ -108,9 +108,7 @@ export default compose( key: ({route, iou}) => { let reportID = lodashGet(route, 'params.reportID', ''); if (!reportID) { - // When the money request or split bill creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. - // Get the report id from the participants list on the IOU object stored in Onyx. - reportID = lodashGet(iou, 'participants.0.reportID', ''); + reportID = IOU.getIOUReportID(iou); } return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; }, diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index 24227c1f39ae..e199b818cfcb 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -382,9 +382,7 @@ export default compose( key: ({route, iou}) => { let reportID = lodashGet(route, 'params.reportID', ''); if (!reportID) { - // When the money request or split bill creation flow is initialized on Global Create, the reportID is not passed as a navigation parameter. - // Get the report id from the participants list on the IOU object stored in Onyx. - reportID = lodashGet(iou, 'participants.0.reportID', ''); + reportID = IOU.getIOUReportID(iou); } return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; }, From 5a5dc17214349f701e88515fece8c4ad3e90050b Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Thu, 28 Sep 2023 15:15:58 +0200 Subject: [PATCH 17/28] unify adding a category to recently used --- src/libs/PolicyUtils.js | 15 +++++++++++++++ src/libs/actions/IOU.js | 23 +++++++++-------------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/libs/PolicyUtils.js b/src/libs/PolicyUtils.js index 347a825f59cc..ef8506791a21 100644 --- a/src/libs/PolicyUtils.js +++ b/src/libs/PolicyUtils.js @@ -261,6 +261,20 @@ function isPendingDeletePolicy(policy) { return policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE; } +/** + * Creates a new array based on a previous instance of "policyRecentlyUsedCategories", + * where a new category is a first one. + * + * @param {Array} recentlyUsedPolicyCategories + * @param {String} category + * @returns {Array} + */ +function addCategoryToRecentlyUsed(recentlyUsedPolicyCategories, category) { + const uniquePolicyRecentlyUsedCategories = _.filter(recentlyUsedPolicyCategories, (recentlyUsedPolicyCategory) => recentlyUsedPolicyCategory !== category); + + return [category, ...uniquePolicyRecentlyUsedCategories]; +} + export { getActivePolicies, hasPolicyMemberError, @@ -280,4 +294,5 @@ export { getTagListName, getTagList, isPendingDeletePolicy, + addCategoryToRecentlyUsed, }; diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index d1e566fb2852..27cfc21daaf6 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -22,6 +22,7 @@ import * as Report from './Report'; import * as NumberUtils from '../NumberUtils'; import ReceiptGeneric from '../../../assets/images/receipt-generic.png'; import * as LocalePhoneNumber from '../LocalePhoneNumber'; +import * as PolicyUtils from '../PolicyUtils'; let allReports; Onyx.connect({ @@ -483,13 +484,10 @@ function getMoneyRequestInformation( let optimisticPolicyRecentlyUsedCategories; if (category) { - const uniquePolicyRecentlyUsedCategories = allRecentlyUsedCategories - ? _.filter( - allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${iouReport.policyID}`], - (recentlyUsedPolicyCategory) => recentlyUsedPolicyCategory !== category, - ) - : []; - optimisticPolicyRecentlyUsedCategories = [category, ...uniquePolicyRecentlyUsedCategories]; + optimisticPolicyRecentlyUsedCategories = PolicyUtils.addCategoryToRecentlyUsed( + allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${iouReport.policyID}`] || [], + category, + ); } const optimisticPolicyRecentlyUsedTags = {}; @@ -1150,13 +1148,10 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco // Add category to optimistic policy recently used categories when a participant is a workspace let optimisticPolicyRecentlyUsedCategories; if (category && isPolicyExpenseChat) { - const uniquePolicyRecentlyUsedCategories = allRecentlyUsedCategories - ? _.filter( - allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${participant.policyID}`], - (recentlyUsedPolicyCategory) => recentlyUsedPolicyCategory !== category, - ) - : []; - optimisticPolicyRecentlyUsedCategories = [category, ...uniquePolicyRecentlyUsedCategories]; + optimisticPolicyRecentlyUsedCategories = PolicyUtils.addCategoryToRecentlyUsed( + allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${participant.policyID}`] || [], + category, + ); } // STEP 5: Build Onyx Data From 43144a92d07b5d9ad50917f36ffaa3ce349b4f0b Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 29 Sep 2023 14:47:28 +0200 Subject: [PATCH 18/28] revert a fix --- src/libs/actions/IOU.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 27cfc21daaf6..1254cb0e6cb2 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -482,13 +482,10 @@ function getMoneyRequestInformation( billable, ); - let optimisticPolicyRecentlyUsedCategories; - if (category) { - optimisticPolicyRecentlyUsedCategories = PolicyUtils.addCategoryToRecentlyUsed( - allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${iouReport.policyID}`] || [], - category, - ); - } + const optimisticPolicyRecentlyUsedCategories = PolicyUtils.addCategoryToRecentlyUsed( + allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${iouReport.policyID}`] || [], + category, + ); const optimisticPolicyRecentlyUsedTags = {}; const recentlyUsedPolicyTags = allRecentlyUsedTags[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${iouReport.policyID}`]; From 31e4209f250d0ca7069f92e893268f0b45e9bb2a Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 29 Sep 2023 14:53:04 +0200 Subject: [PATCH 19/28] extend getIOUReportID --- src/libs/actions/IOU.js | 25 +++++++++++++++++-- src/pages/iou/MoneyRequestCategoryPage.js | 6 ++--- src/pages/iou/MoneyRequestTagPage.js | 6 ++--- .../iou/steps/MoneyRequestConfirmPage.js | 6 ++--- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 1254cb0e6cb2..23f124e61736 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2325,15 +2325,36 @@ function navigateToNextPage(iou, iouType, report, path = '') { Navigation.navigate(ROUTES.MONEY_REQUEST_PARTICIPANTS.getRoute(iouType)); } +// route: PropTypes.shape({ +// /** Route specific parameters used on this screen via route :iouType/new/category/:reportID? */ +// params: PropTypes.shape({ +// /** The type of IOU report, i.e. bill, request, send */ +// iouType: PropTypes.string, + +// /** The report ID of the IOU */ +// reportID: PropTypes.string, +// }), +// }).isRequired, + /** * When the money request or split bill creation flow is initialized via FAB, the reportID is not passed as a navigation * parameter. * Gets a report id from the first participant of the IOU object stored in Onyx. * @param {Object} iou + * @param {Array} iou.participants + * @param {Object} route + * @param {Object} route.params + * @param {String} [route.params.reportID] * @returns {String} */ -function getIOUReportID(iou) { - return lodashGet(iou, 'participants.0.reportID', ''); +function getIOUReportID(iou, route) { + let reportID = lodashGet(route, 'params.reportID', ''); + + if (!reportID) { + reportID = lodashGet(iou, 'participants.0.reportID', ''); + } + + return reportID; } export { diff --git a/src/pages/iou/MoneyRequestCategoryPage.js b/src/pages/iou/MoneyRequestCategoryPage.js index 3ef0ce6aaee1..5055c9f90f8a 100644 --- a/src/pages/iou/MoneyRequestCategoryPage.js +++ b/src/pages/iou/MoneyRequestCategoryPage.js @@ -94,10 +94,8 @@ export default compose( withOnyx({ report: { key: ({route, iou}) => { - let reportID = lodashGet(route, 'params.reportID', ''); - if (!reportID) { - reportID = IOU.getIOUReportID(iou); - } + const reportID = IOU.getIOUReportID(iou, route); + return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; }, }, diff --git a/src/pages/iou/MoneyRequestTagPage.js b/src/pages/iou/MoneyRequestTagPage.js index a8ca82eed302..2fc37d2c4271 100644 --- a/src/pages/iou/MoneyRequestTagPage.js +++ b/src/pages/iou/MoneyRequestTagPage.js @@ -106,10 +106,8 @@ export default compose( withOnyx({ report: { key: ({route, iou}) => { - let reportID = lodashGet(route, 'params.reportID', ''); - if (!reportID) { - reportID = IOU.getIOUReportID(iou); - } + const reportID = IOU.getIOUReportID(iou, route); + return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; }, }, diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index e199b818cfcb..429f62ab280d 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -380,10 +380,8 @@ export default compose( withOnyx({ report: { key: ({route, iou}) => { - let reportID = lodashGet(route, 'params.reportID', ''); - if (!reportID) { - reportID = IOU.getIOUReportID(iou); - } + const reportID = IOU.getIOUReportID(iou, route); + return `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; }, }, From 3563a7c2526e0e18b63517c6ca78dbc4ea33a46c Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 29 Sep 2023 16:36:26 +0200 Subject: [PATCH 20/28] fix unit-tests --- tests/actions/IOUTest.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 3df3b137bab3..25ae453a865e 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -57,7 +57,10 @@ describe('actions/IOU', () => { let iouAction; let transactionID; fetch.pause(); - IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment); + Onyx.set(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {}).then(() => + IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment), + ); + return waitForBatchedUpdates() .then( () => @@ -221,6 +224,7 @@ describe('actions/IOU', () => { [createdAction.reportActionID]: createdAction, }), ) + .then(() => Onyx.set(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {})) .then(() => { IOU.requestMoney(chatReport, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment); return waitForBatchedUpdates(); @@ -413,6 +417,7 @@ describe('actions/IOU', () => { }), ) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${existingTransaction.transactionID}`, existingTransaction)) + .then(() => Onyx.set(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {})) .then(() => { IOU.requestMoney(chatReport, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment); return waitForBatchedUpdates(); @@ -547,7 +552,10 @@ describe('actions/IOU', () => { let iouAction; let transactionID; fetch.pause(); - IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment); + Onyx.set(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {}).then(() => + IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment), + ); + return ( waitForBatchedUpdates() .then( @@ -901,6 +909,7 @@ describe('actions/IOU', () => { }), ) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${julesExistingTransaction.transactionID}`, julesExistingTransaction)) + .then(() => Onyx.set(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {})) .then(() => { // When we split a bill offline fetch.pause(); From c6d978669ab6949f09cb591ac03df8df804f0020 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 29 Sep 2023 18:19:41 +0200 Subject: [PATCH 21/28] simplify condition --- src/libs/actions/IOU.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 23f124e61736..d92cf866b27a 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2348,13 +2348,7 @@ function navigateToNextPage(iou, iouType, report, path = '') { * @returns {String} */ function getIOUReportID(iou, route) { - let reportID = lodashGet(route, 'params.reportID', ''); - - if (!reportID) { - reportID = lodashGet(iou, 'participants.0.reportID', ''); - } - - return reportID; + return lodashGet(route, 'params.reportID') || lodashGet(iou, 'participants.0.reportID', ''); } export { From 2a061f0f0ae391b56bc7806908dffe6d7a7b5776 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Fri, 29 Sep 2023 18:21:14 +0200 Subject: [PATCH 22/28] Revert "fix unit-tests" This reverts commit 3563a7c2526e0e18b63517c6ca78dbc4ea33a46c. --- tests/actions/IOUTest.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 25ae453a865e..3df3b137bab3 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -57,10 +57,7 @@ describe('actions/IOU', () => { let iouAction; let transactionID; fetch.pause(); - Onyx.set(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {}).then(() => - IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment), - ); - + IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment); return waitForBatchedUpdates() .then( () => @@ -224,7 +221,6 @@ describe('actions/IOU', () => { [createdAction.reportActionID]: createdAction, }), ) - .then(() => Onyx.set(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {})) .then(() => { IOU.requestMoney(chatReport, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment); return waitForBatchedUpdates(); @@ -417,7 +413,6 @@ describe('actions/IOU', () => { }), ) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${existingTransaction.transactionID}`, existingTransaction)) - .then(() => Onyx.set(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {})) .then(() => { IOU.requestMoney(chatReport, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment); return waitForBatchedUpdates(); @@ -552,10 +547,7 @@ describe('actions/IOU', () => { let iouAction; let transactionID; fetch.pause(); - Onyx.set(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {}).then(() => - IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment), - ); - + IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment); return ( waitForBatchedUpdates() .then( @@ -909,7 +901,6 @@ describe('actions/IOU', () => { }), ) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${julesExistingTransaction.transactionID}`, julesExistingTransaction)) - .then(() => Onyx.set(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {})) .then(() => { // When we split a bill offline fetch.pause(); From 56cfc89f20bdea91c7de258421bf07ef4ece17f9 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Mon, 2 Oct 2023 14:37:25 +0200 Subject: [PATCH 23/28] implement buildOptimisticPolicyRecentlyUsedCategories --- src/libs/actions/IOU.js | 29 ++++++----------------------- src/libs/actions/Policy.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 24c192e807ff..5b12b2f018d7 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -22,7 +22,7 @@ import * as Report from './Report'; import * as NumberUtils from '../NumberUtils'; import ReceiptGeneric from '../../../assets/images/receipt-generic.png'; import * as LocalePhoneNumber from '../LocalePhoneNumber'; -import * as PolicyUtils from '../PolicyUtils'; +import * as Policy from './Policy'; let allReports; Onyx.connect({ @@ -45,13 +45,6 @@ Onyx.connect({ }, }); -let allRecentlyUsedCategories = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, - waitForCollectionCallback: true, - callback: (val) => (allRecentlyUsedCategories = val), -}); - let allRecentlyUsedTags = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS, @@ -176,11 +169,7 @@ function buildOnyxDataForMoneyRequest( ]; if (!_.isEmpty(optimisticRecentlyUsedCategories)) { - optimisticData.push({ - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${iouReport.policyID}`, - value: optimisticRecentlyUsedCategories, - }); + optimisticData.push(optimisticRecentlyUsedCategories); } if (!_.isEmpty(optimisticPolicyRecentlyUsedTags)) { @@ -464,10 +453,7 @@ function getMoneyRequestInformation( billable, ); - const optimisticPolicyRecentlyUsedCategories = PolicyUtils.addCategoryToRecentlyUsed( - allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${iouReport.policyID}`] || [], - category, - ); + const optimisticPolicyRecentlyUsedCategories = Policy.buildOptimisticPolicyRecentlyUsedCategories(iouReport.policyID, category); const optimisticPolicyRecentlyUsedTags = {}; const recentlyUsedPolicyTags = allRecentlyUsedTags[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${iouReport.policyID}`]; @@ -1125,12 +1111,9 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco } // Add category to optimistic policy recently used categories when a participant is a workspace - let optimisticPolicyRecentlyUsedCategories; - if (category && isPolicyExpenseChat) { - optimisticPolicyRecentlyUsedCategories = PolicyUtils.addCategoryToRecentlyUsed( - allRecentlyUsedCategories[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${participant.policyID}`] || [], - category, - ); + let optimisticPolicyRecentlyUsedCategories = {}; + if (isPolicyExpenseChat) { + optimisticPolicyRecentlyUsedCategories = Policy.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category); } // STEP 5: Build Onyx Data diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index fcce909c5582..b8f94ba37149 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -12,6 +12,7 @@ import * as OptionsListUtils from '../OptionsListUtils'; import * as ErrorUtils from '../ErrorUtils'; import * as ReportUtils from '../ReportUtils'; import * as PersonalDetailsUtils from '../PersonalDetailsUtils'; +import * as PolicyUtils from '../PolicyUtils'; import Log from '../Log'; const allPolicies = {}; @@ -65,6 +66,13 @@ Onyx.connect({ callback: (val) => (allPersonalDetails = val), }); +let allRecentlyUsedCategories = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, + waitForCollectionCallback: true, + callback: (val) => (allRecentlyUsedCategories = val), +}); + /** * Stores in Onyx the policy ID of the last workspace that was accessed by the user * @param {String|null} policyID @@ -1168,6 +1176,25 @@ function clearErrors(policyID) { hideWorkspaceAlertMessage(policyID); } +/** + * @param {String} policyID + * @param {String} category + * @returns {Object} + */ +function buildOptimisticPolicyRecentlyUsedCategories(policyID, category) { + if (!policyID || !category) { + return {}; + } + + const policyRecentlyUsedCategories = lodashGet(allRecentlyUsedCategories, `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`, []); + + return { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`, + value: PolicyUtils.addCategoryToRecentlyUsed(policyRecentlyUsedCategories, category), + }; +} + export { removeMembers, addMembersToWorkspace, @@ -1197,4 +1224,5 @@ export { setWorkspaceInviteMembersDraft, clearErrors, openDraftWorkspaceRequest, + buildOptimisticPolicyRecentlyUsedCategories, }; From 87154dc61ab2c4599ed96ca11198fd0d807ef144 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Mon, 2 Oct 2023 17:11:44 +0200 Subject: [PATCH 24/28] remove extra helper --- src/libs/PolicyUtils.js | 15 --------------- src/libs/actions/Policy.js | 4 ++-- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/libs/PolicyUtils.js b/src/libs/PolicyUtils.js index ef8506791a21..347a825f59cc 100644 --- a/src/libs/PolicyUtils.js +++ b/src/libs/PolicyUtils.js @@ -261,20 +261,6 @@ function isPendingDeletePolicy(policy) { return policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE; } -/** - * Creates a new array based on a previous instance of "policyRecentlyUsedCategories", - * where a new category is a first one. - * - * @param {Array} recentlyUsedPolicyCategories - * @param {String} category - * @returns {Array} - */ -function addCategoryToRecentlyUsed(recentlyUsedPolicyCategories, category) { - const uniquePolicyRecentlyUsedCategories = _.filter(recentlyUsedPolicyCategories, (recentlyUsedPolicyCategory) => recentlyUsedPolicyCategory !== category); - - return [category, ...uniquePolicyRecentlyUsedCategories]; -} - export { getActivePolicies, hasPolicyMemberError, @@ -294,5 +280,4 @@ export { getTagListName, getTagList, isPendingDeletePolicy, - addCategoryToRecentlyUsed, }; diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index b8f94ba37149..819b3163c492 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -12,7 +12,6 @@ import * as OptionsListUtils from '../OptionsListUtils'; import * as ErrorUtils from '../ErrorUtils'; import * as ReportUtils from '../ReportUtils'; import * as PersonalDetailsUtils from '../PersonalDetailsUtils'; -import * as PolicyUtils from '../PolicyUtils'; import Log from '../Log'; const allPolicies = {}; @@ -1187,11 +1186,12 @@ function buildOptimisticPolicyRecentlyUsedCategories(policyID, category) { } const policyRecentlyUsedCategories = lodashGet(allRecentlyUsedCategories, `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`, []); + const uniquePolicyRecentlyUsedCategories = _.filter(policyRecentlyUsedCategories, (recentlyUsedPolicyCategory) => recentlyUsedPolicyCategory !== category); return { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`, - value: PolicyUtils.addCategoryToRecentlyUsed(policyRecentlyUsedCategories, category), + value: [category, ...uniquePolicyRecentlyUsedCategories], }; } From 864154caa1deb7c384f44f09eea25356a5e8f4a2 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Mon, 2 Oct 2023 18:07:42 +0200 Subject: [PATCH 25/28] unify --- src/libs/actions/IOU.js | 12 ++++++++---- src/libs/actions/Policy.js | 8 ++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 5b12b2f018d7..26ff5f7b7773 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -117,7 +117,7 @@ function buildOnyxDataForMoneyRequest( iouAction, optimisticPersonalDetailListAction, reportPreviewAction, - optimisticRecentlyUsedCategories, + optimisticPolicyRecentlyUsedCategories, optimisticPolicyRecentlyUsedTags, isNewChatReport, isNewIOUReport, @@ -168,8 +168,12 @@ function buildOnyxDataForMoneyRequest( }, ]; - if (!_.isEmpty(optimisticRecentlyUsedCategories)) { - optimisticData.push(optimisticRecentlyUsedCategories); + if (!_.isEmpty(optimisticPolicyRecentlyUsedCategories)) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${iouReport.policyID}`, + value: optimisticPolicyRecentlyUsedCategories, + }); } if (!_.isEmpty(optimisticPolicyRecentlyUsedTags)) { @@ -1111,7 +1115,7 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco } // Add category to optimistic policy recently used categories when a participant is a workspace - let optimisticPolicyRecentlyUsedCategories = {}; + let optimisticPolicyRecentlyUsedCategories = []; if (isPolicyExpenseChat) { optimisticPolicyRecentlyUsedCategories = Policy.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category); } diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 819b3163c492..f4ad81e9d62c 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -1182,17 +1182,13 @@ function clearErrors(policyID) { */ function buildOptimisticPolicyRecentlyUsedCategories(policyID, category) { if (!policyID || !category) { - return {}; + return []; } const policyRecentlyUsedCategories = lodashGet(allRecentlyUsedCategories, `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`, []); const uniquePolicyRecentlyUsedCategories = _.filter(policyRecentlyUsedCategories, (recentlyUsedPolicyCategory) => recentlyUsedPolicyCategory !== category); - return { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`, - value: [category, ...uniquePolicyRecentlyUsedCategories], - }; + return [category, ...uniquePolicyRecentlyUsedCategories]; } export { From 733d2292ed299492ce90a349c92141ada9dc77cf Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Tue, 3 Oct 2023 17:07:18 +0200 Subject: [PATCH 26/28] clear category on go back --- src/pages/iou/steps/MoneyRequestConfirmPage.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index 429f62ab280d..a508b8b22402 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -117,11 +117,15 @@ function MoneyRequestConfirmPage(props) { const navigateBack = () => { let fallback; + if (reportID.current) { fallback = ROUTES.MONEY_REQUEST.getRoute(iouType.current, reportID.current); } else { fallback = ROUTES.MONEY_REQUEST_PARTICIPANTS.getRoute(iouType.current); } + + IOU.resetMoneyRequestCategory(); + Navigation.goBack(fallback); }; From df19f0397c1dad0a5599357098f2b0b4f6b3ae67 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Wed, 4 Oct 2023 12:56:45 +0200 Subject: [PATCH 27/28] clear values on start confirm --- src/pages/iou/steps/MoneyRequestConfirmPage.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index a508b8b22402..907869c0e3a4 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -78,6 +78,11 @@ function MoneyRequestConfirmPage(props) { const isPolicyExpenseChat = useMemo(() => ReportUtils.isPolicyExpenseChat(ReportUtils.getRootParentReport(props.report)), [props.report]); const isManualRequestDM = props.selectedTab === CONST.TAB.MANUAL && iouType.current === CONST.IOU.MONEY_REQUEST_TYPE.REQUEST; + useEffect(() => { + IOU.resetMoneyRequestCategory(); + IOU.resetMoneyRequestTag(); + }, []); + useEffect(() => { const policyExpenseChat = _.find(participants, (participant) => participant.isPolicyExpenseChat); if (policyExpenseChat) { @@ -117,15 +122,11 @@ function MoneyRequestConfirmPage(props) { const navigateBack = () => { let fallback; - if (reportID.current) { fallback = ROUTES.MONEY_REQUEST.getRoute(iouType.current, reportID.current); } else { fallback = ROUTES.MONEY_REQUEST_PARTICIPANTS.getRoute(iouType.current); } - - IOU.resetMoneyRequestCategory(); - Navigation.goBack(fallback); }; From 585d6a226ea191b15af22f310247945a8b3b0f37 Mon Sep 17 00:00:00 2001 From: Mykhailo Kravchenko Date: Mon, 9 Oct 2023 15:06:04 +0200 Subject: [PATCH 28/28] simplify combining array --- src/libs/actions/Policy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index f4ad81e9d62c..3e5e91a74488 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -1,6 +1,7 @@ import _ from 'underscore'; import Onyx from 'react-native-onyx'; import lodashGet from 'lodash/get'; +import lodashUnion from 'lodash/union'; import {PUBLIC_DOMAINS} from 'expensify-common/lib/CONST'; import Str from 'expensify-common/lib/str'; import {escapeRegExp} from 'lodash'; @@ -1186,9 +1187,8 @@ function buildOptimisticPolicyRecentlyUsedCategories(policyID, category) { } const policyRecentlyUsedCategories = lodashGet(allRecentlyUsedCategories, `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`, []); - const uniquePolicyRecentlyUsedCategories = _.filter(policyRecentlyUsedCategories, (recentlyUsedPolicyCategory) => recentlyUsedPolicyCategory !== category); - return [category, ...uniquePolicyRecentlyUsedCategories]; + return lodashUnion([category], policyRecentlyUsedCategories); } export {