diff --git a/src/components/TestDrive/Modal/EmployeeTestDriveModal.tsx b/src/components/TestDrive/Modal/EmployeeTestDriveModal.tsx index 77d796caefbbd..f4971106d6e04 100644 --- a/src/components/TestDrive/Modal/EmployeeTestDriveModal.tsx +++ b/src/components/TestDrive/Modal/EmployeeTestDriveModal.tsx @@ -48,9 +48,11 @@ function EmployeeTestDriveModal() { setIsLoading(true); - verifyTestDriveRecipient(bossEmail) - .then(() => { - setTestReceipt(TestReceipt, 'jpg', (source, _, filename) => { + verifyTestDriveRecipient(bossEmail).then(() => { + setTestReceipt( + TestReceipt, + 'jpg', + (source, _, filename) => { const transactionID = CONST.IOU.OPTIMISTIC_TRANSACTION_ID; const reportID = generateReportID(); initMoneyRequest(reportID, undefined, false, undefined, CONST.IOU.REQUEST_TYPE.SCAN); @@ -75,12 +77,13 @@ function EmployeeTestDriveModal() { Navigation.goBack(); Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, CONST.IOU.TYPE.SUBMIT, transactionID, reportID)); }); - }); - }) - .catch(() => { - setIsLoading(false); - setFormError(translate('testDrive.modal.employee.error')); - }); + }, + () => { + setIsLoading(false); + setFormError(translate('testDrive.modal.employee.error')); + }, + ); + }); }; const skipTestDrive = () => { diff --git a/src/libs/actions/setTestReceipt/getFile/index.ios.ts b/src/libs/actions/setTestReceipt/getFile/index.ios.ts new file mode 100644 index 0000000000000..998109baaeb51 --- /dev/null +++ b/src/libs/actions/setTestReceipt/getFile/index.ios.ts @@ -0,0 +1,12 @@ +import ReactNativeBlobUtil from 'react-native-blob-util'; +import type GetFile from './types'; + +const getFile: GetFile = (source, path, assetExtension) => { + return ReactNativeBlobUtil.config({ + fileCache: true, + appendExt: assetExtension, + path, + }).fetch('GET', source); +}; + +export default getFile; diff --git a/src/libs/actions/setTestReceipt/getFile/index.ts b/src/libs/actions/setTestReceipt/getFile/index.ts new file mode 100644 index 0000000000000..9514a69969e7c --- /dev/null +++ b/src/libs/actions/setTestReceipt/getFile/index.ts @@ -0,0 +1,19 @@ +import ReactNativeBlobUtil from 'react-native-blob-util'; +import RNFS from 'react-native-fs'; +import CONFIG from '@src/CONFIG'; +import CONST from '@src/CONST'; +import type GetFile from './types'; + +const getFile: GetFile = (source, path, assetExtension) => { + if (CONFIG.ENVIRONMENT === CONST.ENVIRONMENT.DEV) { + return ReactNativeBlobUtil.config({ + fileCache: true, + appendExt: assetExtension, + path, + }).fetch('GET', source); + } + + return RNFS.copyFileRes(`${source}.${assetExtension}`, path); +}; + +export default getFile; diff --git a/src/libs/actions/setTestReceipt/getFile/types.ts b/src/libs/actions/setTestReceipt/getFile/types.ts new file mode 100644 index 0000000000000..3d1300e248c5b --- /dev/null +++ b/src/libs/actions/setTestReceipt/getFile/types.ts @@ -0,0 +1,6 @@ +import type {FetchBlobResponse} from 'react-native-blob-util'; +import type {AssetExtension} from '@userActions/setTestReceipt/types'; + +type GetFile = (source: string, path: string, assetExtension: AssetExtension) => Promise; + +export default GetFile; diff --git a/src/libs/actions/setTestReceipt/index.native.ts b/src/libs/actions/setTestReceipt/index.native.ts index eefa33b5e6ac0..49d547708edba 100644 --- a/src/libs/actions/setTestReceipt/index.native.ts +++ b/src/libs/actions/setTestReceipt/index.native.ts @@ -3,41 +3,35 @@ import ReactNativeBlobUtil from 'react-native-blob-util'; import type {FileObject} from '@components/AttachmentModal'; import Log from '@libs/Log'; import CONST from '@src/CONST'; +import getFile from './getFile'; import type {SetTestReceipt} from './types'; -const setTestReceipt: SetTestReceipt = (asset, assetExtension, onFileRead) => { - try { - const filename = `${CONST.TEST_RECEIPT.FILENAME}_${Date.now()}.${assetExtension}`; - const path = `${ReactNativeBlobUtil.fs.dirs.CacheDir}/${filename}`; - const source = Image.resolveAssetSource(asset).uri; +const setTestReceipt: SetTestReceipt = (asset, assetExtension, onFileRead, onFileError) => { + const filename = `${CONST.TEST_RECEIPT.FILENAME}_${Date.now()}.${assetExtension}`; + const path = `${ReactNativeBlobUtil.fs.dirs.CacheDir}/${filename}`; + const source = Image.resolveAssetSource(asset).uri; - ReactNativeBlobUtil.config({ - fileCache: true, - appendExt: assetExtension, - path, - }) - .fetch('GET', source) - .then(() => { - const file: FileObject = { - uri: `file://${path}`, - name: filename, - type: CONST.TEST_RECEIPT.FILE_TYPE, - size: 0, - }; + getFile(source, path, assetExtension) + .then(() => { + const file: FileObject = { + uri: `file://${path}`, + name: filename, + type: CONST.TEST_RECEIPT.FILE_TYPE, + size: 0, + }; + + if (!file.uri) { + Log.warn('Error reading test receipt'); + return; + } - if (!file.uri) { - Log.warn('Error reading test receipt'); - return; - } + onFileRead(file.uri, file, filename); + }) + .catch((error) => { + Log.warn('Error reading test receipt:', {message: error}); - onFileRead(file.uri, file, filename); - }) - .catch((error) => { - Log.warn('Error reading test receipt:', {message: error}); - }); - } catch (error) { - Log.warn('Error in setTestReceipt:', {message: error}); - } + onFileError?.(error); + }); }; export default setTestReceipt; diff --git a/src/libs/actions/setTestReceipt/index.ts b/src/libs/actions/setTestReceipt/index.ts index f564632ef57bd..790e3c099f0fd 100644 --- a/src/libs/actions/setTestReceipt/index.ts +++ b/src/libs/actions/setTestReceipt/index.ts @@ -3,25 +3,23 @@ import Log from '@libs/Log'; import CONST from '@src/CONST'; import type {SetTestReceipt} from './types'; -const setTestReceipt: SetTestReceipt = (asset, assetExtension, onFileRead) => { - try { - const filename = `${CONST.TEST_RECEIPT.FILENAME}_${Date.now()}.${assetExtension}`; - readFileAsync( - asset as string, - filename, - (file) => { - const source = URL.createObjectURL(file); +const setTestReceipt: SetTestReceipt = (asset, assetExtension, onFileRead, onFileError) => { + const filename = `${CONST.TEST_RECEIPT.FILENAME}_${Date.now()}.${assetExtension}`; + readFileAsync( + asset as string, + filename, + (file) => { + const source = URL.createObjectURL(file); - onFileRead(source, file, filename); - }, - (error) => { - Log.warn('Error reading test receipt:', {message: error}); - }, - CONST.TEST_RECEIPT.FILE_TYPE, - ); - } catch (error) { - Log.warn('Error in setTestReceipt:', {message: error}); - } + onFileRead(source, file, filename); + }, + (error) => { + Log.warn('Error reading test receipt:', {message: error}); + + onFileError?.(error); + }, + CONST.TEST_RECEIPT.FILE_TYPE, + ); }; export default setTestReceipt; diff --git a/src/libs/actions/setTestReceipt/types.ts b/src/libs/actions/setTestReceipt/types.ts index 54eeff60bc076..785ea115bea3a 100644 --- a/src/libs/actions/setTestReceipt/types.ts +++ b/src/libs/actions/setTestReceipt/types.ts @@ -3,9 +3,10 @@ import type {FileObject} from '@components/AttachmentModal'; type OnFileRead = (source: string, file: FileObject, filename: string) => void; -type ReceiptExtension = 'jpg' | 'png'; +type OnFileError = (error: unknown) => void; -type SetTestReceipt = (asset: ImageSourcePropType, assetExtension: ReceiptExtension, onFileRead: OnFileRead) => void; +type AssetExtension = 'jpg' | 'png'; -// eslint-disable-next-line import/prefer-default-export -export type {SetTestReceipt}; +type SetTestReceipt = (asset: ImageSourcePropType, assetExtension: AssetExtension, onFileRead: OnFileRead, onFileError?: OnFileError) => void; + +export type {AssetExtension, SetTestReceipt};