From 6965b95065c127dd8883028310dae7446fdad95b Mon Sep 17 00:00:00 2001 From: Hans Date: Mon, 18 Sep 2023 19:08:43 +0700 Subject: [PATCH 1/6] fix persist receipt error --- src/pages/iou/ReceiptSelector/index.js | 44 ++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/pages/iou/ReceiptSelector/index.js b/src/pages/iou/ReceiptSelector/index.js index 3f5672c3d33d..f9e809196b57 100644 --- a/src/pages/iou/ReceiptSelector/index.js +++ b/src/pages/iou/ReceiptSelector/index.js @@ -1,6 +1,7 @@ import {View, Text, PixelRatio} from 'react-native'; import React, {useContext, useState} from 'react'; import lodashGet from 'lodash/get'; +import _ from 'underscore'; import PropTypes from 'prop-types'; import {withOnyx} from 'react-native-onyx'; import * as IOU from '../../../libs/actions/IOU'; @@ -19,8 +20,8 @@ import Receipt from '../../../libs/actions/Receipt'; import useWindowDimensions from '../../../hooks/useWindowDimensions'; import useLocalize from '../../../hooks/useLocalize'; import {DragAndDropContext} from '../../../components/DragAndDrop/Provider'; -import * as ReceiptUtils from '../../../libs/ReceiptUtils'; import {iouPropTypes, iouDefaultProps} from '../propTypes'; +import * as FileUtils from '../../../libs/fileDownload/FileUtils'; const propTypes = { /** Information shown to the user when a receipt is not valid */ @@ -62,14 +63,43 @@ const defaultProps = { function ReceiptSelector(props) { const reportID = lodashGet(props.route, 'params.reportID', ''); const iouType = lodashGet(props.route, 'params.iouType', ''); - const isAttachmentInvalid = lodashGet(props.receiptModal, 'isAttachmentInvalid', false); - const attachmentInvalidReasonTitle = lodashGet(props.receiptModal, 'attachmentInvalidReasonTitle', ''); - const attachmentInvalidReason = lodashGet(props.receiptModal, 'attachmentInvalidReason', ''); + const [isAttachmentInvalid, setIsAttachmentInvalid] = useState(false); + const [attachmentInvalidReasonTitle, setAttachmentInvalidReasonTitle] = useState(''); + const [attachmentInvalidReason, setAttachmentValidReason] = useState(''); const [receiptImageTopPosition, setReceiptImageTopPosition] = useState(0); const {isSmallScreenWidth} = useWindowDimensions(); const {translate} = useLocalize(); const {isDraggingOver} = useContext(DragAndDropContext); + /** + * Sets the upload receipt error modal content when an invalid receipt is uploaded + */ + const setUploadReceiptError = (isInvalid, title, reason) => { + setIsAttachmentInvalid(isInvalid); + setAttachmentInvalidReasonTitle(title); + setAttachmentValidReason(reason); + } + + function validateReceipt(file) { + const {fileExtension} = FileUtils.splitExtensionFromFileName(lodashGet(file, 'name', '')); + if (_.contains(CONST.API_ATTACHMENT_VALIDATIONS.UNALLOWED_EXTENSIONS, fileExtension.toLowerCase())) { + setUploadReceiptError(true, 'attachmentPicker.wrongFileType', 'attachmentPicker.notAllowedExtension'); + return false; + } + + if (lodashGet(file, 'size', 0) > CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE) { + setUploadReceiptError(true, 'attachmentPicker.attachmentTooLarge', 'attachmentPicker.sizeExceeded'); + return false; + } + + if (lodashGet(file, 'size', 0) < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) { + setUploadReceiptError(true, 'attachmentPicker.attachmentTooSmall', 'attachmentPicker.sizeNotMet'); + return false; + } + + return true; + } + /** * Sets the Receipt objects and navigates the user to the next page * @param {Object} file @@ -77,7 +107,7 @@ function ReceiptSelector(props) { * @param {Object} report */ const setReceiptAndNavigate = (file, iou, report) => { - if (!ReceiptUtils.validateReceipt(file)) { + if (!validateReceipt(file)) { return; } @@ -142,8 +172,8 @@ function ReceiptSelector(props) { /> setIsAttachmentInvalid(false)} + onCancel={() => setIsAttachmentInvalid(false)} isVisible={isAttachmentInvalid} prompt={attachmentInvalidReason ? translate(attachmentInvalidReason) : ''} confirmText={translate('common.close')} From 857d18b4e57f0f7b2851dd170eced8d35d826e9e Mon Sep 17 00:00:00 2001 From: Hans Date: Mon, 18 Sep 2023 22:13:00 +0700 Subject: [PATCH 2/6] fix linting --- src/pages/iou/ReceiptSelector/index.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/pages/iou/ReceiptSelector/index.js b/src/pages/iou/ReceiptSelector/index.js index f9e809196b57..80d5cf81f098 100644 --- a/src/pages/iou/ReceiptSelector/index.js +++ b/src/pages/iou/ReceiptSelector/index.js @@ -16,7 +16,6 @@ import ReceiptDropUI from '../ReceiptDropUI'; import AttachmentPicker from '../../../components/AttachmentPicker'; import ConfirmModal from '../../../components/ConfirmModal'; import ONYXKEYS from '../../../ONYXKEYS'; -import Receipt from '../../../libs/actions/Receipt'; import useWindowDimensions from '../../../hooks/useWindowDimensions'; import useLocalize from '../../../hooks/useLocalize'; import {DragAndDropContext} from '../../../components/DragAndDrop/Provider'; @@ -73,12 +72,15 @@ function ReceiptSelector(props) { /** * Sets the upload receipt error modal content when an invalid receipt is uploaded + * @param {*} isInvalid + * @param {*} title + * @param {*} reason */ const setUploadReceiptError = (isInvalid, title, reason) => { setIsAttachmentInvalid(isInvalid); setAttachmentInvalidReasonTitle(title); setAttachmentValidReason(reason); - } + }; function validateReceipt(file) { const {fileExtension} = FileUtils.splitExtensionFromFileName(lodashGet(file, 'name', '')); @@ -86,17 +88,17 @@ function ReceiptSelector(props) { setUploadReceiptError(true, 'attachmentPicker.wrongFileType', 'attachmentPicker.notAllowedExtension'); return false; } - + if (lodashGet(file, 'size', 0) > CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE) { setUploadReceiptError(true, 'attachmentPicker.attachmentTooLarge', 'attachmentPicker.sizeExceeded'); return false; } - + if (lodashGet(file, 'size', 0) < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) { setUploadReceiptError(true, 'attachmentPicker.attachmentTooSmall', 'attachmentPicker.sizeNotMet'); return false; } - + return true; } From a84c2d755f86ea5d867270044c274cbe44dfa036 Mon Sep 17 00:00:00 2001 From: Hans Date: Mon, 18 Sep 2023 22:35:19 +0700 Subject: [PATCH 3/6] remove unused code --- src/libs/ReceiptUtils.js | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/src/libs/ReceiptUtils.js b/src/libs/ReceiptUtils.js index d90a6cbf0e37..8f352c182171 100644 --- a/src/libs/ReceiptUtils.js +++ b/src/libs/ReceiptUtils.js @@ -1,34 +1,11 @@ -import lodashGet from 'lodash/get'; -import _ from 'underscore'; import Str from 'expensify-common/lib/str'; import * as FileUtils from './fileDownload/FileUtils'; import CONST from '../CONST'; -import Receipt from './actions/Receipt'; import ReceiptHTML from '../../assets/images/receipt-html.png'; import ReceiptDoc from '../../assets/images/receipt-doc.png'; import ReceiptGeneric from '../../assets/images/receipt-generic.png'; import ReceiptSVG from '../../assets/images/receipt-svg.png'; -function validateReceipt(file) { - const {fileExtension} = FileUtils.splitExtensionFromFileName(lodashGet(file, 'name', '')); - if (_.contains(CONST.API_ATTACHMENT_VALIDATIONS.UNALLOWED_EXTENSIONS, fileExtension.toLowerCase())) { - Receipt.setUploadReceiptError(true, 'attachmentPicker.wrongFileType', 'attachmentPicker.notAllowedExtension'); - return false; - } - - if (lodashGet(file, 'size', 0) > CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE) { - Receipt.setUploadReceiptError(true, 'attachmentPicker.attachmentTooLarge', 'attachmentPicker.sizeExceeded'); - return false; - } - - if (lodashGet(file, 'size', 0) < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) { - Receipt.setUploadReceiptError(true, 'attachmentPicker.attachmentTooSmall', 'attachmentPicker.sizeNotMet'); - return false; - } - - return true; -} - /** * Grab the appropriate receipt image and thumbnail URIs based on file type * @@ -64,4 +41,5 @@ function getThumbnailAndImageURIs(path, filename) { return {thumbnail: null, image}; } -export {validateReceipt, getThumbnailAndImageURIs}; +// eslint-disable-next-line import/prefer-default-export +export {getThumbnailAndImageURIs}; From 6e9ba02d8143a7fe8664a8930a168da54828fd36 Mon Sep 17 00:00:00 2001 From: Hans Date: Wed, 20 Sep 2023 11:54:59 +0700 Subject: [PATCH 4/6] fix linting --- src/pages/iou/ReceiptSelector/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pages/iou/ReceiptSelector/index.js b/src/pages/iou/ReceiptSelector/index.js index 3f753e438bd7..b42482ddf871 100644 --- a/src/pages/iou/ReceiptSelector/index.js +++ b/src/pages/iou/ReceiptSelector/index.js @@ -71,9 +71,9 @@ function ReceiptSelector(props) { const {isDraggingOver} = useContext(DragAndDropContext); const hideReciptModal = () => { - setIsAttachmentInvalid(false); - } - + setIsAttachmentInvalid(false); + }; + /** * Sets the upload receipt error modal content when an invalid receipt is uploaded * @param {*} isInvalid From a340c6b8d507a560bbda1f97cf445d9fd8e27481 Mon Sep 17 00:00:00 2001 From: Hans Date: Wed, 20 Sep 2023 19:49:34 +0700 Subject: [PATCH 5/6] remove unused file --- src/libs/actions/Receipt.ts | 39 ------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 src/libs/actions/Receipt.ts diff --git a/src/libs/actions/Receipt.ts b/src/libs/actions/Receipt.ts deleted file mode 100644 index acc23f04cf67..000000000000 --- a/src/libs/actions/Receipt.ts +++ /dev/null @@ -1,39 +0,0 @@ -import Onyx from 'react-native-onyx'; -import ONYXKEYS from '../../ONYXKEYS'; - -/** - * Sets the upload receipt error modal content when an invalid receipt is uploaded - */ -function setUploadReceiptError(isAttachmentInvalid: boolean, attachmentInvalidReasonTitle: string, attachmentInvalidReason: string) { - Onyx.merge(ONYXKEYS.RECEIPT_MODAL, { - isAttachmentInvalid, - attachmentInvalidReasonTitle, - attachmentInvalidReason, - }); -} - -/** - * Clears the receipt error modal - */ -function clearUploadReceiptError() { - Onyx.merge(ONYXKEYS.RECEIPT_MODAL, { - isAttachmentInvalid: false, - attachmentInvalidReasonTitle: '', - attachmentInvalidReason: '', - }); -} - -/** - * Close the receipt modal - */ -function closeUploadReceiptModal() { - Onyx.merge(ONYXKEYS.RECEIPT_MODAL, { - isAttachmentInvalid: false, - }); -} - -export default { - setUploadReceiptError, - clearUploadReceiptError, - closeUploadReceiptModal, -}; From ccbc03d8b4c2eafbac59de8682cd7a02233747cd Mon Sep 17 00:00:00 2001 From: Hans Date: Wed, 20 Sep 2023 20:08:18 +0700 Subject: [PATCH 6/6] remove unused type --- src/ONYXKEYS.ts | 1 - src/pages/iou/ReceiptSelector/index.js | 13 ------------- src/types/onyx/ReceiptModal.ts | 7 ------- src/types/onyx/index.ts | 2 -- 4 files changed, 23 deletions(-) delete mode 100644 src/types/onyx/ReceiptModal.ts diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index ae8c2037a8e3..05256f2b806c 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -362,7 +362,6 @@ type OnyxValues = { [ONYXKEYS.LAST_OPENED_PUBLIC_ROOM_ID]: string; [ONYXKEYS.PREFERRED_THEME]: ValueOf; [ONYXKEYS.IS_USING_MEMORY_ONLY_KEYS]: boolean; - [ONYXKEYS.RECEIPT_MODAL]: OnyxTypes.ReceiptModal; [ONYXKEYS.MAPBOX_ACCESS_TOKEN]: OnyxTypes.MapboxAccessToken; [ONYXKEYS.ONYX_UPDATES_FROM_SERVER]: OnyxTypes.OnyxUpdatesFromServer; [ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT]: number; diff --git a/src/pages/iou/ReceiptSelector/index.js b/src/pages/iou/ReceiptSelector/index.js index d91859f7282c..eb6e2328afd2 100644 --- a/src/pages/iou/ReceiptSelector/index.js +++ b/src/pages/iou/ReceiptSelector/index.js @@ -24,13 +24,6 @@ import * as FileUtils from '../../../libs/fileDownload/FileUtils'; import Navigation from '../../../libs/Navigation/Navigation'; const propTypes = { - /** Information shown to the user when a receipt is not valid */ - receiptModal: PropTypes.shape({ - isAttachmentInvalid: PropTypes.bool, - attachmentInvalidReasonTitle: PropTypes.string, - attachmentInvalidReason: PropTypes.string, - }), - /** The report on which the request is initiated on */ report: reportPropTypes, @@ -54,11 +47,6 @@ const propTypes = { }; const defaultProps = { - receiptModal: { - isAttachmentInvalid: false, - attachmentInvalidReasonTitle: '', - attachmentInvalidReason: '', - }, report: {}, iou: iouDefaultProps, transactionID: '', @@ -207,7 +195,6 @@ ReceiptSelector.displayName = 'ReceiptSelector'; export default withOnyx({ iou: {key: ONYXKEYS.IOU}, - receiptModal: {key: ONYXKEYS.RECEIPT_MODAL}, report: { key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${lodashGet(route, 'params.reportID', '')}`, }, diff --git a/src/types/onyx/ReceiptModal.ts b/src/types/onyx/ReceiptModal.ts deleted file mode 100644 index 0d52f684b4d2..000000000000 --- a/src/types/onyx/ReceiptModal.ts +++ /dev/null @@ -1,7 +0,0 @@ -type ReceiptModal = { - isAttachmentInvalid: boolean; - attachmentInvalidReasonTitle: string; - attachmentInvalidReason: string; -}; - -export default ReceiptModal; diff --git a/src/types/onyx/index.ts b/src/types/onyx/index.ts index 8711a0d208ef..069909153096 100644 --- a/src/types/onyx/index.ts +++ b/src/types/onyx/index.ts @@ -29,7 +29,6 @@ import FrequentlyUsedEmoji from './FrequentlyUsedEmoji'; import ReimbursementAccount from './ReimbursementAccount'; import ReimbursementAccountDraft from './ReimbursementAccountDraft'; import WalletTransfer from './WalletTransfer'; -import ReceiptModal from './ReceiptModal'; import MapboxAccessToken from './MapboxAccessToken'; import {OnyxUpdatesFromServer, OnyxUpdateEvent} from './OnyxUpdatesFromServer'; import Download from './Download'; @@ -79,7 +78,6 @@ export type { ReimbursementAccountDraft, FrequentlyUsedEmoji, WalletTransfer, - ReceiptModal, MapboxAccessToken, Download, PolicyMember,