From ad5852282fd87fdb222a3e61470db77cb8560dae Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 27 Sep 2023 11:20:05 +0700 Subject: [PATCH 1/3] hide replace option --- src/components/AttachmentModal.js | 73 ++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/src/components/AttachmentModal.js b/src/components/AttachmentModal.js index 23545de26cfd..fee9334c3833 100755 --- a/src/components/AttachmentModal.js +++ b/src/components/AttachmentModal.js @@ -1,4 +1,4 @@ -import React, {useState, useCallback, useRef} from 'react'; +import React, {useState, useCallback, useRef, useMemo} from 'react'; import PropTypes from 'prop-types'; import {View, Animated, Keyboard} from 'react-native'; import Str from 'expensify-common/lib/str'; @@ -30,6 +30,11 @@ import useWindowDimensions from '../hooks/useWindowDimensions'; import Navigation from '../libs/Navigation/Navigation'; import ROUTES from '../ROUTES'; import useNativeDriver from '../libs/useNativeDriver'; +import * as ReportUtils from '../libs/ReportUtils'; +import * as ReportActionsUtils from '../libs/ReportActionsUtils'; +import { withOnyx } from 'react-native-onyx'; +import ONYXKEYS from '../ONYXKEYS'; +import * as Policy from '../libs/actions/Policy'; import useNetwork from '../hooks/useNetwork'; /** @@ -326,6 +331,37 @@ function AttachmentModal(props) { const sourceForAttachmentView = props.source || source; + const threeDotsMenuItems = useMemo(() => { + if (!isAttachmentReceipt || !props.parentReport || !props.parentReportActions) { + return []; + } + let menuItems = []; + const parentReportAction = props.parentReportActions[props.report.parentReportActionID]; + const isDeleted = ReportActionsUtils.isDeletedAction(parentReportAction); + const isSettled = ReportUtils.isSettled(props.parentReport.reportID); + + const isAdmin = Policy.isAdminOfFreePolicy([props.policy]) && ReportUtils.isExpenseReport(props.parentReport); + const isRequestor = ReportUtils.isMoneyRequestReport(props.parentReport) && lodashGet(props.session, 'accountID', null) === parentReportAction.actorAccountID; + const canEdit = !isSettled && !isDeleted && (isAdmin || isRequestor); + if (canEdit) { + menuItems.push({ + icon: Expensicons.Camera, + text: props.translate('common.replace'), + onSelected: () => { + onModalHideCallbackRef.current = () => Navigation.navigate(ROUTES.getEditRequestRoute(props.report.reportID, CONST.EDIT_REQUEST_FIELD.RECEIPT)); + closeModal(); + }, + }) + } + console.log(isSettled, isDeleted, isAdmin, isRequestor); + menuItems.push({ + icon: Expensicons.Download, + text: props.translate('common.download'), + onSelected: () => downloadAttachment(source), + }) + return menuItems; + }, [isAttachmentReceipt, props.parentReport, props.parentReportActions, props.policy]) + return ( <> { - onModalHideCallbackRef.current = () => Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(props.report.reportID, CONST.EDIT_REQUEST_FIELD.RECEIPT)); - closeModal(); - }, - }, - { - icon: Expensicons.Download, - text: props.translate('common.download'), - onSelected: () => downloadAttachment(source), - }, - ]} + threeDotsMenuItems={threeDotsMenuItems} shouldOverlay /> @@ -444,4 +466,21 @@ function AttachmentModal(props) { AttachmentModal.propTypes = propTypes; AttachmentModal.defaultProps = defaultProps; AttachmentModal.displayName = 'AttachmentModal'; -export default compose(withWindowDimensions, withLocalize)(AttachmentModal); +export default compose( + withWindowDimensions, + withLocalize, + withOnyx({ + parentReport: { + key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT}${report ? report.parentReportID : '0'}`, + }, + policy: { + key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '0'}`, + }, + parentReportActions: { + key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report ? report.parentReportID : '0'}` + }, + session: { + key: ONYXKEYS.SESSION + } + }) +)(AttachmentModal); From 7a2d07247a2c50485cabffecd4391c0f4cebbfd4 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Thu, 28 Sep 2023 15:02:27 +0700 Subject: [PATCH 2/3] merge main and fix lint --- src/components/AttachmentModal.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/components/AttachmentModal.js b/src/components/AttachmentModal.js index fee9334c3833..4e5b6ded1dca 100755 --- a/src/components/AttachmentModal.js +++ b/src/components/AttachmentModal.js @@ -5,6 +5,7 @@ import Str from 'expensify-common/lib/str'; import lodashGet from 'lodash/get'; import lodashExtend from 'lodash/extend'; import _ from 'underscore'; +import {withOnyx} from 'react-native-onyx'; import CONST from '../CONST'; import Modal from './Modal'; import AttachmentView from './Attachments/AttachmentView'; @@ -32,7 +33,6 @@ import ROUTES from '../ROUTES'; import useNativeDriver from '../libs/useNativeDriver'; import * as ReportUtils from '../libs/ReportUtils'; import * as ReportActionsUtils from '../libs/ReportActionsUtils'; -import { withOnyx } from 'react-native-onyx'; import ONYXKEYS from '../ONYXKEYS'; import * as Policy from '../libs/actions/Policy'; import useNetwork from '../hooks/useNetwork'; @@ -335,11 +335,11 @@ function AttachmentModal(props) { if (!isAttachmentReceipt || !props.parentReport || !props.parentReportActions) { return []; } - let menuItems = []; + const menuItems = []; const parentReportAction = props.parentReportActions[props.report.parentReportActionID]; const isDeleted = ReportActionsUtils.isDeletedAction(parentReportAction); const isSettled = ReportUtils.isSettled(props.parentReport.reportID); - + const isAdmin = Policy.isAdminOfFreePolicy([props.policy]) && ReportUtils.isExpenseReport(props.parentReport); const isRequestor = ReportUtils.isMoneyRequestReport(props.parentReport) && lodashGet(props.session, 'accountID', null) === parentReportAction.actorAccountID; const canEdit = !isSettled && !isDeleted && (isAdmin || isRequestor); @@ -351,16 +351,16 @@ function AttachmentModal(props) { onModalHideCallbackRef.current = () => Navigation.navigate(ROUTES.getEditRequestRoute(props.report.reportID, CONST.EDIT_REQUEST_FIELD.RECEIPT)); closeModal(); }, - }) + }); } - console.log(isSettled, isDeleted, isAdmin, isRequestor); menuItems.push({ icon: Expensicons.Download, text: props.translate('common.download'), onSelected: () => downloadAttachment(source), - }) + }); return menuItems; - }, [isAttachmentReceipt, props.parentReport, props.parentReportActions, props.policy]) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [isAttachmentReceipt, props.parentReport, props.parentReportActions, props.policy]); return ( <> @@ -467,7 +467,7 @@ AttachmentModal.propTypes = propTypes; AttachmentModal.defaultProps = defaultProps; AttachmentModal.displayName = 'AttachmentModal'; export default compose( - withWindowDimensions, + withWindowDimensions, withLocalize, withOnyx({ parentReport: { @@ -477,10 +477,10 @@ export default compose( key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '0'}`, }, parentReportActions: { - key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report ? report.parentReportID : '0'}` + key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report ? report.parentReportID : '0'}`, }, session: { - key: ONYXKEYS.SESSION - } - }) + key: ONYXKEYS.SESSION, + }, + }), )(AttachmentModal); From 08b323f5274141d3b293a9ae3c3758061db861fd Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Thu, 28 Sep 2023 16:11:31 +0700 Subject: [PATCH 3/3] add canEvict for parent report actionn --- src/components/AttachmentModal.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/AttachmentModal.js b/src/components/AttachmentModal.js index 4e5b6ded1dca..fc0101f4adcc 100755 --- a/src/components/AttachmentModal.js +++ b/src/components/AttachmentModal.js @@ -478,6 +478,7 @@ export default compose( }, parentReportActions: { key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report ? report.parentReportID : '0'}`, + canEvict: false, }, session: { key: ONYXKEYS.SESSION,