Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow detaching receipts from transactions #26219

Merged
merged 44 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
6a458b7
Allow zIndex to be overridden
Beamanator Aug 29, 2023
c2aa62f
Allow overriding some modal styles from HeaderWithBackButton
Beamanator Aug 29, 2023
bfe6348
New attachment view header component
Beamanator Aug 29, 2023
9373871
Forgot to commit new style var
Beamanator Aug 29, 2023
73ba5b6
Finish transaction-get logic
Beamanator Aug 29, 2023
fe01307
Add optimistic and failure data
Beamanator Aug 29, 2023
c0f1198
Fixing up some lint errors
Beamanator Aug 29, 2023
e46307c
Navigate user back to receipt report after detaching
Beamanator Aug 29, 2023
802af8d
Add translations
Beamanator Aug 29, 2023
6491074
lint & prettify
Beamanator Aug 29, 2023
6283fa6
Merge branch 'main' of github.com:Expensify/App into beaman-addContex…
Beamanator Aug 30, 2023
dc50e8a
Merge branch 'main' of github.com:Expensify/App into beaman-addContex…
Beamanator Sep 1, 2023
776513e
Prop types fixes
Beamanator Sep 1, 2023
b46f19a
Merge branch 'main' of github.com:Expensify/App into beaman-addContex…
Beamanator Sep 19, 2023
d089863
Fix types
Beamanator Sep 19, 2023
b11ffb7
Merge branch 'main' of github.com:Expensify/App into beaman-addContex…
Beamanator Sep 20, 2023
f3087ee
Remove old version of custom header
Beamanator Sep 20, 2023
7c83e8c
Revert a few style updates
Beamanator Sep 20, 2023
eaa6671
Revert unused var
Beamanator Sep 20, 2023
ca054cf
Connect detachReceipt with transactionID
Beamanator Sep 20, 2023
0ae84f4
Merge branch 'main' of github.com:Expensify/App into beaman-addContex…
Beamanator Sep 21, 2023
cf3979a
Fix up logic
Beamanator Sep 21, 2023
64a4f68
revert a few old changes
Beamanator Sep 21, 2023
1c73f1f
move function to IOU.js
Gonals Sep 27, 2023
7aed7bc
hide if smartscanning
Gonals Sep 27, 2023
28bc51e
remove extras
Gonals Sep 27, 2023
8d6af6f
Better handle optimistic data
Gonals Sep 28, 2023
cc7ff5a
remove debug lines
Gonals Sep 28, 2023
22e636c
lint
Gonals Sep 28, 2023
9495751
Merge branch 'main' into beaman-addContextMenuForReceipts
Gonals Sep 28, 2023
9c3bff6
add confirm modal
Gonals Sep 28, 2023
a6de27f
hooks stuff
Gonals Sep 28, 2023
99fe73d
Merge branch 'main' into beaman-addContextMenuForReceipts
Gonals Oct 3, 2023
f0d73af
bunch of comments
Gonals Oct 3, 2023
b592c3c
remove unneded params
Gonals Oct 4, 2023
086fbd7
conflicts
Gonals Oct 4, 2023
b8ecb24
more conflicts
Gonals Oct 4, 2023
2902371
conflicts
Gonals Oct 9, 2023
4de580d
conflicts
Gonals Oct 9, 2023
0a52427
conflicts
Gonals Oct 10, 2023
dbce3ec
remove extra export
Gonals Oct 10, 2023
c3e4a26
fix confirm modal on ios
luacmartins Oct 11, 2023
bd019dd
Merge branch 'main' into beaman-addContextMenuForReceipts
luacmartins Oct 11, 2023
b976628
resolve conflicts
luacmartins Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 87 additions & 27 deletions src/components/AttachmentModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -30,7 +31,12 @@ import useWindowDimensions from '../hooks/useWindowDimensions';
import Navigation from '../libs/Navigation/Navigation';
import ROUTES from '../ROUTES';
import useNativeDriver from '../libs/useNativeDriver';
import * as ReportActionsUtils from '../libs/ReportActionsUtils';
import useNetwork from '../hooks/useNetwork';
import * as IOU from '../libs/actions/IOU';
import ONYXKEYS from '../ONYXKEYS';
import transactionPropTypes from './transactionPropTypes';
import * as TransactionUtils from '../libs/TransactionUtils';

/**
* Modal render prop component that exposes modal launching triggers that can be used
Expand Down Expand Up @@ -74,6 +80,9 @@ const propTypes = {
/** The report that has this attachment */
report: reportPropTypes,

/** The transaction associated with the receipt attachment, if any */
transaction: transactionPropTypes,

...withLocalizePropTypes,

...windowDimensionsPropTypes,
Expand All @@ -92,6 +101,7 @@ const defaultProps = {
allowDownload: false,
headerTitle: null,
report: {},
transaction: {},
onModalShow: () => {},
onModalHide: () => {},
onCarouselAttachmentChange: () => {},
Expand All @@ -103,6 +113,7 @@ function AttachmentModal(props) {
const [isModalOpen, setIsModalOpen] = useState(props.defaultOpen);
const [shouldLoadAttachment, setShouldLoadAttachment] = useState(false);
const [isAttachmentInvalid, setIsAttachmentInvalid] = useState(false);
const [isDeleteReceiptConfirmModalVisible, setIsDeleteReceiptConfirmModalVisible] = useState(false);
const [isAuthTokenRequired, setIsAuthTokenRequired] = useState(props.isAuthTokenRequired);
const [isAttachmentReceipt, setIsAttachmentReceipt] = useState(false);
const [attachmentInvalidReasonTitle, setAttachmentInvalidReasonTitle] = useState('');
Expand Down Expand Up @@ -200,12 +211,22 @@ function AttachmentModal(props) {
}, [isModalOpen, isConfirmButtonDisabled, props.onConfirm, file, source]);

/**
* Close the confirm modal.
* Close the confirm modals.
*/
const closeConfirmModal = useCallback(() => {
setIsAttachmentInvalid(false);
setIsDeleteReceiptConfirmModalVisible(false);
}, []);

/**
* Detach the receipt and close the modal.
*/
const deleteAndCloseModal = useCallback(() => {
IOU.detachReceipt(props.transaction.transactionID, props.report.reportID);
setIsDeleteReceiptConfirmModalVisible(false);
Navigation.dismissModal(props.report.reportID);
}, [props.transaction, props.report]);

/**
* @param {Object} _file
* @returns {Boolean}
Expand Down Expand Up @@ -326,6 +347,32 @@ function AttachmentModal(props) {

const sourceForAttachmentView = props.source || source;

const threeDotsMenuItems = [
{
icon: Expensicons.Camera,
text: props.translate('common.replace'),
onSelected: () => {
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),
},
];

if (TransactionUtils.hasReceipt(props.transaction) && !TransactionUtils.isReceiptBeingScanned(props.transaction)) {
Gonals marked this conversation as resolved.
Show resolved Hide resolved
threeDotsMenuItems.push({
icon: Expensicons.Trashcan,
text: props.translate('receipt.deleteReceipt'),
onSelected: () => {
setIsDeleteReceiptConfirmModalVisible(true);
},
});
}

return (
<>
<Modal
Expand Down Expand Up @@ -360,21 +407,7 @@ function AttachmentModal(props) {
onCloseButtonPress={closeModal}
shouldShowThreeDotsButton={isAttachmentReceipt}
threeDotsAnchorPosition={styles.threeDotsPopoverOffsetAttachmentModal(windowWidth)}
threeDotsMenuItems={[
{
icon: Expensicons.Camera,
text: props.translate('common.replace'),
onSelected: () => {
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
/>
<View style={styles.imageModalImageCenterContainer}>
Expand Down Expand Up @@ -421,16 +454,28 @@ function AttachmentModal(props) {
</SafeAreaConsumer>
)}
</Modal>

<ConfirmModal
title={attachmentInvalidReasonTitle ? translate(attachmentInvalidReasonTitle) : ''}
onConfirm={closeConfirmModal}
onCancel={closeConfirmModal}
isVisible={isAttachmentInvalid}
prompt={attachmentInvalidReason ? translate(attachmentInvalidReason) : ''}
confirmText={translate('common.close')}
shouldShowCancelButton={false}
/>
{isAttachmentReceipt ? (
<ConfirmModal
title={translate('receipt.deleteReceipt')}
isVisible={isDeleteReceiptConfirmModalVisible}
onConfirm={deleteAndCloseModal}
onCancel={closeConfirmModal}
prompt={translate('receipt.deleteConfirmation')}
confirmText={translate('common.delete')}
cancelText={translate('common.cancel')}
danger
/>
) : (
<ConfirmModal
title={attachmentInvalidReasonTitle ? translate(attachmentInvalidReasonTitle) : ''}
onConfirm={closeConfirmModal}
onCancel={closeConfirmModal}
isVisible={isAttachmentInvalid}
prompt={attachmentInvalidReason ? translate(attachmentInvalidReason) : ''}
confirmText={translate('common.close')}
shouldShowCancelButton={false}
/>
)}

{props.children &&
props.children({
Expand All @@ -444,4 +489,19 @@ function AttachmentModal(props) {
AttachmentModal.propTypes = propTypes;
AttachmentModal.defaultProps = defaultProps;
AttachmentModal.displayName = 'AttachmentModal';
export default compose(withWindowDimensions, withLocalize)(AttachmentModal);
export default compose(
withWindowDimensions,
withLocalize,
withOnyx({
transaction: {
key: ({report}) => {
if (!report) {
return undefined;
}
const parentReportAction = ReportActionsUtils.getReportAction(report.parentReportID, report.parentReportActionID);
const transactionID = lodashGet(parentReportAction, ['originalMessage', 'IOUTransactionID'], 0);
return `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`;
},
},
}),
)(AttachmentModal);
3 changes: 2 additions & 1 deletion src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ export default {
flash: 'flash',
shutter: 'shutter',
gallery: 'gallery',
deleteReceipt: 'Delete receipt',
deleteConfirmation: 'Are you sure you want to delete this receipt?',
addReceipt: 'Add receipt',
},
iou: {
Expand All @@ -511,7 +513,6 @@ export default {
pay: 'Pay',
viewDetails: 'View details',
pending: 'Pending',
deleteReceipt: 'Delete receipt',
receiptScanning: 'Receipt scan in progress…',
receiptMissingDetails: 'Receipt missing details',
receiptStatusTitle: 'Scanning…',
Expand Down
3 changes: 2 additions & 1 deletion src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ export default {
flash: 'flash',
shutter: 'obturador',
gallery: 'galería',
deleteReceipt: 'Eliminar recibo',
deleteConfirmation: '¿Estás seguro de que quieres borrar este recibo?',
addReceipt: 'Añadir recibo',
},
iou: {
Expand All @@ -503,7 +505,6 @@ export default {
pay: 'Pagar',
viewDetails: 'Ver detalles',
pending: 'Pendiente',
deleteReceipt: 'Eliminar recibo',
receiptScanning: 'Escaneo de recibo en curso…',
receiptMissingDetails: 'Recibo con campos vacíos',
receiptStatusTitle: 'Escaneando…',
Expand Down
24 changes: 24 additions & 0 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,29 @@ function payMoneyRequest(paymentType, chatReport, iouReport) {
Navigation.dismissModal(chatReport.reportID);
}

function detachReceipt(transactionID) {
const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] || {};
const newTransaction = {...transaction, filename: '', receipt: {}};

const optimisticData = [
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: newTransaction,
},
];

const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: transaction,
},
];

API.write('DetachReceipt', {transactionID}, {optimisticData, failureData});
}

/**
* @param {String} transactionID
* @param {Object} receipt
Expand Down Expand Up @@ -2329,4 +2352,5 @@ export {
navigateToNextPage,
updateDistanceRequest,
replaceReceipt,
detachReceipt,
};
Loading