From 41ef4a9786a24594d2286ce03c45963b38c67329 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Thu, 23 Nov 2023 17:55:09 +0700 Subject: [PATCH 1/4] fix unable to change currency when offline --- src/components/MoneyRequestConfirmationList.js | 2 +- src/components/ReportActionItem/MoneyRequestView.js | 5 +++-- src/libs/ReportUtils.js | 11 ++++++++++- src/pages/EditRequestPage.js | 7 ++++++- src/pages/iou/steps/NewRequestAmountPage.js | 2 +- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.js b/src/components/MoneyRequestConfirmationList.js index efa9c5a49cec..99afcd71826c 100755 --- a/src/components/MoneyRequestConfirmationList.js +++ b/src/components/MoneyRequestConfirmationList.js @@ -598,7 +598,7 @@ function MoneyRequestConfirmationList(props) { shouldShowRightIcon={!props.isReadOnly && !props.isDistanceRequest} title={formattedAmount} description={translate('iou.amount')} - interactive={!props.isReadOnly} + interactive={!props.isReadOnly && !props.isDistanceRequest} onPress={() => { if (props.isDistanceRequest) { return; diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 33ad99f32326..5541ab0f8c76 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -159,6 +159,7 @@ function MoneyRequestView({report, parentReport, policyCategories, shouldShowHor const pendingAction = lodashGet(transaction, 'pendingAction'); const getPendingFieldAction = (fieldPath) => lodashGet(transaction, fieldPath) || pendingAction; + const isAdmin = ReportUtils.isAdminOfMoneyRequestReport(moneyRequestReport); return ( @@ -190,8 +191,8 @@ function MoneyRequestView({report, parentReport, policyCategories, shouldShowHor titleIcon={Expensicons.Checkmark} description={amountDescription} titleStyle={styles.newKansasLarge} - interactive={canEdit && !isSettled} - shouldShowRightIcon={canEdit && !isSettled} + interactive={canEdit && !isSettled && (isDistanceRequest ? isAdmin : true)} + shouldShowRightIcon={canEdit && !isSettled && (isDistanceRequest ? isAdmin : true)} onPress={() => Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.AMOUNT))} brickRoadIndicator={hasErrors && transactionAmount === 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} error={hasErrors && transactionAmount === 0 ? translate('common.error.enterAmount') : ''} diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 2e91a93af7e1..b6ba729955f4 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1660,6 +1660,14 @@ function getTransactionDetails(transaction, createdDateFormat = CONST.DATE.FNS_F }; } +/** + * @param {Object} moneyRequestReport + * @returns {Boolean} + */ +function isAdminOfMoneyRequestReport(moneyRequestReport) { + return isExpenseReport(moneyRequestReport) && lodashGet(getPolicy(moneyRequestReport.policyID), 'role', '') === CONST.POLICY.ROLE.ADMIN; +} + /** * Can only edit if: * @@ -1693,7 +1701,7 @@ function canEditMoneyRequest(reportAction, fieldToEdit = '') { const moneyRequestReport = getReport(moneyRequestReportID); const isReportSettled = isSettled(moneyRequestReport.reportID); - const isAdmin = isExpenseReport(moneyRequestReport) && lodashGet(getPolicy(moneyRequestReport.policyID), 'role', '') === CONST.POLICY.ROLE.ADMIN; + const isAdmin = isAdminOfMoneyRequestReport(moneyRequestReport); const isRequestor = currentUserAccountID === reportAction.actorAccountID; if (isAdmin && !isRequestor && fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT) { return false; @@ -4533,4 +4541,5 @@ export { getRoom, shouldDisableWelcomeMessage, canEditWriteCapability, + isAdminOfMoneyRequestReport, }; diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index 95313bea142d..aae66256dacc 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -104,18 +104,23 @@ function EditRequestPage({report, route, parentReport, policyCategories, policyT // A flag for showing the tags page const shouldShowTags = isPolicyExpenseChat && (transactionTag || OptionsListUtils.hasEnabledOptions(lodashValues(policyTagList))); + const isAdmin = ReportUtils.isAdminOfMoneyRequestReport(report); + // Decides whether to allow or disallow editing a money request useEffect(() => { // Do not dismiss the modal, when a current user can edit this property of the money request. if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, parentReport.reportID, fieldToEdit)) { return; } + if (TransactionUtils.isDistanceRequest(transaction) ? isAdmin : true) { + return; + } // Dismiss the modal when a current user cannot edit a money request. Navigation.isNavigationReady().then(() => { Navigation.dismissModal(); }); - }, [parentReportAction, parentReport.reportID, fieldToEdit]); + }, [parentReportAction, parentReport.reportID, fieldToEdit, isAdmin, transaction]); // Update the transaction object and close the modal function editMoneyRequest(transactionChanges) { diff --git a/src/pages/iou/steps/NewRequestAmountPage.js b/src/pages/iou/steps/NewRequestAmountPage.js index 1140bbbcf5b2..5325c8e2a7e9 100644 --- a/src/pages/iou/steps/NewRequestAmountPage.js +++ b/src/pages/iou/steps/NewRequestAmountPage.js @@ -166,7 +166,7 @@ function NewRequestAmountPage({route, iou, report, selectedTab}) { testID={NewRequestAmountPage.displayName} > {({safeAreaPaddingBottomStyle}) => ( - + Date: Tue, 2 Jan 2024 17:58:55 +0700 Subject: [PATCH 2/4] fix only allow admin to edit amount in distance request --- .../ReportActionItem/MoneyRequestView.js | 3 +-- src/libs/ReportUtils.ts | 24 +++++++++---------- src/pages/EditRequestPage.js | 9 ++----- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index efaa4aed9536..bc2c36534288 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -116,7 +116,7 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const isSettled = ReportUtils.isSettled(moneyRequestReport.reportID); const isCancelled = moneyRequestReport && moneyRequestReport.isCancelledIOU; const canEdit = ReportUtils.canEditMoneyRequest(parentReportAction); - const canEditAmount = canEdit && !isSettled && !isCardTransaction; + const canEditAmount = ReportUtils.canEditMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.AMOUNT, transaction) && !isSettled && !isCardTransaction; const canEditReceipt = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, moneyRequestReport.reportID, CONST.EDIT_REQUEST_FIELD.RECEIPT); // A flag for verifying that the current report is a sub-report of a workspace chat @@ -174,7 +174,6 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const pendingAction = lodashGet(transaction, 'pendingAction'); const getPendingFieldAction = (fieldPath) => lodashGet(transaction, fieldPath) || pendingAction; - const isAdmin = ReportUtils.isAdminOfMoneyRequestReport(moneyRequestReport); return ( diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 64a7dc7bae3d..f1830408593d 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1803,14 +1803,6 @@ function getTransactionDetails(transaction: OnyxEntry, createdDateF }; } -/** - * @param {Object} moneyRequestReport - * @returns {Boolean} - */ -function isAdminOfMoneyRequestReport(moneyRequestReport) { - return isExpenseReport(moneyRequestReport) && lodashGet(getPolicy(moneyRequestReport.policyID), 'role', '') === CONST.POLICY.ROLE.ADMIN; -} - /** * Can only edit if: * @@ -1820,7 +1812,7 @@ function isAdminOfMoneyRequestReport(moneyRequestReport) { * - the current user is the requestor and is not settled yet * - or the user is an admin on the policy the expense report is tied to */ -function canEditMoneyRequest(reportAction: OnyxEntry, fieldToEdit = ''): boolean { +function canEditMoneyRequest(reportAction: OnyxEntry, fieldToEdit = '', transaction?: OnyxEntry): boolean { const isDeleted = ReportActionsUtils.isDeletedAction(reportAction); if (isDeleted) { @@ -1847,11 +1839,14 @@ function canEditMoneyRequest(reportAction: OnyxEntry, fieldToEdit const isApproved = isReportApproved(moneyRequestReport); const isAdmin = isExpenseReport(moneyRequestReport) && (getPolicy(moneyRequestReport?.policyID ?? '')?.role ?? '') === CONST.POLICY.ROLE.ADMIN; const isRequestor = currentUserAccountID === reportAction?.actorAccountID; + const isDistanceRequest = !isEmpty(transaction) && TransactionUtils.isDistanceRequest(transaction); if (isAdmin && !isRequestor && fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT) { return false; } - + if (isDistanceRequest && fieldToEdit === CONST.EDIT_REQUEST_FIELD.AMOUNT) { + return isAdmin; + } if (isAdmin) { return true; } @@ -1863,7 +1858,12 @@ function canEditMoneyRequest(reportAction: OnyxEntry, fieldToEdit * Checks if the current user can edit the provided property of a money request * */ -function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, reportID: string, fieldToEdit: ValueOf): boolean { +function canEditFieldOfMoneyRequest( + reportAction: OnyxEntry, + reportID: string, + fieldToEdit: ValueOf, + transaction: OnyxEntry, +): boolean { // A list of fields that cannot be edited by anyone, once a money request has been settled const nonEditableFieldsWhenSettled: string[] = [ CONST.EDIT_REQUEST_FIELD.AMOUNT, @@ -1874,7 +1874,7 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, repor ]; // Checks if this user has permissions to edit this money request - if (!canEditMoneyRequest(reportAction, fieldToEdit)) { + if (!canEditMoneyRequest(reportAction, fieldToEdit, transaction)) { return false; // User doesn't have permission to edit } diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index c1027dac915a..4dfa945f29a5 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -101,15 +101,10 @@ function EditRequestPage({report, route, parentReport, policyCategories, policyT // A flag for showing the tags page const shouldShowTags = isPolicyExpenseChat && (transactionTag || OptionsListUtils.hasEnabledOptions(lodashValues(policyTagList))); - const isAdmin = ReportUtils.isAdminOfMoneyRequestReport(report); - // Decides whether to allow or disallow editing a money request useEffect(() => { // Do not dismiss the modal, when a current user can edit this property of the money request. - if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, parentReport.reportID, fieldToEdit)) { - return; - } - if (TransactionUtils.isDistanceRequest(transaction) ? isAdmin : true) { + if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, parentReport.reportID, fieldToEdit, transaction)) { return; } @@ -117,7 +112,7 @@ function EditRequestPage({report, route, parentReport, policyCategories, policyT Navigation.isNavigationReady().then(() => { Navigation.dismissModal(); }); - }, [parentReportAction, parentReport.reportID, fieldToEdit, isAdmin, transaction]); + }, [parentReportAction, parentReport.reportID, fieldToEdit, transaction]); // Update the transaction object and close the modal function editMoneyRequest(transactionChanges) { From 0988d01418e564890dedbe85bea6002e70f73a4b Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Tue, 2 Jan 2024 18:00:57 +0700 Subject: [PATCH 3/4] fix remove not related change --- src/components/MoneyRequestConfirmationList.js | 2 +- src/pages/iou/steps/NewRequestAmountPage.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.js b/src/components/MoneyRequestConfirmationList.js index 7aae6dc74354..b75f4e2df845 100755 --- a/src/components/MoneyRequestConfirmationList.js +++ b/src/components/MoneyRequestConfirmationList.js @@ -605,7 +605,7 @@ function MoneyRequestConfirmationList(props) { shouldShowRightIcon={!props.isReadOnly && !props.isDistanceRequest} title={formattedAmount} description={translate('iou.amount')} - interactive={!props.isReadOnly && !props.isDistanceRequest} + interactive={!props.isReadOnly} onPress={() => { if (props.isDistanceRequest) { return; diff --git a/src/pages/iou/steps/NewRequestAmountPage.js b/src/pages/iou/steps/NewRequestAmountPage.js index 834204f34974..1df74569e4c3 100644 --- a/src/pages/iou/steps/NewRequestAmountPage.js +++ b/src/pages/iou/steps/NewRequestAmountPage.js @@ -166,7 +166,7 @@ function NewRequestAmountPage({route, iou, report, selectedTab}) { testID={NewRequestAmountPage.displayName} > {({safeAreaPaddingBottomStyle}) => ( - + Date: Tue, 2 Jan 2024 18:07:51 +0700 Subject: [PATCH 4/4] fix lint --- src/pages/EditRequestPage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index 4dfa945f29a5..e41f30779f22 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -112,7 +112,7 @@ function EditRequestPage({report, route, parentReport, policyCategories, policyT Navigation.isNavigationReady().then(() => { Navigation.dismissModal(); }); - }, [parentReportAction, parentReport.reportID, fieldToEdit, transaction]); + }, [parentReportAction, parentReport.reportID, fieldToEdit, transaction]); // Update the transaction object and close the modal function editMoneyRequest(transactionChanges) {