Skip to content

Commit

Permalink
Merge pull request #52483 from callstack-internal/fix/51761-stable-re…
Browse files Browse the repository at this point in the history
…ceipt-folder

Store receipts in stable directory on the native platforms
  • Loading branch information
neil-marcellini authored Dec 16, 2024
2 parents f57c28c + 4a68d98 commit 1c4a63e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5980,6 +5980,7 @@ const CONST = {
DOWNLOADS_PATH: '/Downloads',
DOWNLOADS_TIMEOUT: 5000,
NEW_EXPENSIFY_PATH: '/New Expensify',
RECEIPTS_UPLOAD_PATH: '/Receipts-Upload',

ENVIRONMENT_SUFFIX: {
DEV: ' Dev',
Expand Down
7 changes: 7 additions & 0 deletions src/libs/getReceiptsUploadFolderPath/index.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import RNFetchBlob from 'react-native-blob-util';
import CONST from '@src/CONST';
import type GetReceiptsUploadFolderPath from './types';

const getReceiptsUploadFolderPath: GetReceiptsUploadFolderPath = () => `${RNFetchBlob.fs.dirs.DownloadDir}${CONST.RECEIPTS_UPLOAD_PATH}`;

export default getReceiptsUploadFolderPath;
7 changes: 7 additions & 0 deletions src/libs/getReceiptsUploadFolderPath/index.ios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import RNFetchBlob from 'react-native-blob-util';
import CONST from '@src/CONST';
import type GetReceiptsUploadFolderPath from './types';

const getReceiptsUploadFolderPath: GetReceiptsUploadFolderPath = () => `${RNFetchBlob.fs.dirs.DocumentDir}${CONST.RECEIPTS_UPLOAD_PATH}`;

export default getReceiptsUploadFolderPath;
5 changes: 5 additions & 0 deletions src/libs/getReceiptsUploadFolderPath/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type GetReceiptsUploadFolderPath from './types';

const getReceiptsUploadFolderPath: GetReceiptsUploadFolderPath = () => '';

export default getReceiptsUploadFolderPath;
3 changes: 3 additions & 0 deletions src/libs/getReceiptsUploadFolderPath/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type GetReceiptsUploadFolderPath = () => string;

export default GetReceiptsUploadFolderPath;
97 changes: 59 additions & 38 deletions src/pages/iou/request/step/IOURequestStepScan/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {useFocusEffect} from '@react-navigation/core';
import {Str} from 'expensify-common';
import React, {useCallback, useMemo, useRef, useState} from 'react';
import {ActivityIndicator, Alert, AppState, InteractionManager, View} from 'react-native';
import ReactNativeBlobUtil from 'react-native-blob-util';
import {Gesture, GestureDetector} from 'react-native-gesture-handler';
import {useOnyx} from 'react-native-onyx';
import {RESULTS} from 'react-native-permissions';
Expand Down Expand Up @@ -31,6 +32,7 @@ import * as FileUtils from '@libs/fileDownload/FileUtils';
import getPhotoSource from '@libs/fileDownload/getPhotoSource';
import getCurrentPosition from '@libs/getCurrentPosition';
import getPlatform from '@libs/getPlatform';
import getReceiptsUploadFolderPath from '@libs/getReceiptsUploadFolderPath';
import * as IOUUtils from '@libs/IOUUtils';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -497,49 +499,68 @@ function IOURequestStepScan({

setDidCapturePhoto(true);

camera?.current
?.takePhoto({
flash: flash && hasFlash ? 'on' : 'off',
enableShutterSound: !isPlatformMuted,
const path = getReceiptsUploadFolderPath();

ReactNativeBlobUtil.fs
.isDir(path)
.then((isDir) => {
if (isDir) {
return;
}

ReactNativeBlobUtil.fs.mkdir(path).catch((error: string) => {
Log.warn('Error creating the directory', error);
});
})
.then((photo: PhotoFile) => {
// Store the receipt on the transaction object in Onyx
const source = getPhotoSource(photo.path);
IOU.setMoneyRequestReceipt(transactionID, source, photo.path, !isEditing);

FileUtils.readFileAsync(
source,
photo.path,
(file) => {
if (isEditing) {
updateScanAndNavigate(file, source);
return;
}
if (shouldSkipConfirmation) {
setFileResize(file);
setFileSource(source);
const gpsRequired = transaction?.amount === 0 && iouType !== CONST.IOU.TYPE.SPLIT && file;
if (gpsRequired) {
const shouldStartLocationPermissionFlow = IOUUtils.shouldStartLocationPermissionFlow();
if (shouldStartLocationPermissionFlow) {
setStartLocationPermissionFlow(true);
.catch((error: string) => {
Log.warn('Error checking if the directory exists', error);
})
.then(() => {
camera?.current
?.takePhoto({
flash: flash && hasFlash ? 'on' : 'off',
enableShutterSound: !isPlatformMuted,
path,
})
.then((photo: PhotoFile) => {
// Store the receipt on the transaction object in Onyx
const source = getPhotoSource(photo.path);
IOU.setMoneyRequestReceipt(transactionID, source, photo.path, !isEditing);

FileUtils.readFileAsync(
source,
photo.path,
(file) => {
if (isEditing) {
updateScanAndNavigate(file, source);
return;
}
}
}
navigateToConfirmationStep(file, source, false);
},
() => {
if (shouldSkipConfirmation) {
setFileResize(file);
setFileSource(source);
const gpsRequired = transaction?.amount === 0 && iouType !== CONST.IOU.TYPE.SPLIT && file;
if (gpsRequired) {
const shouldStartLocationPermissionFlow = IOUUtils.shouldStartLocationPermissionFlow();
if (shouldStartLocationPermissionFlow) {
setStartLocationPermissionFlow(true);
return;
}
}
}
navigateToConfirmationStep(file, source, false);
},
() => {
setDidCapturePhoto(false);
showCameraAlert();
Log.warn('Error reading photo');
},
);
})
.catch((error: string) => {
setDidCapturePhoto(false);
showCameraAlert();
Log.warn('Error reading photo');
},
);
})
.catch((error: string) => {
setDidCapturePhoto(false);
showCameraAlert();
Log.warn('Error taking photo', error);
Log.warn('Error taking photo', error);
});
});
}, [
cameraPermissionStatus,
Expand Down

0 comments on commit 1c4a63e

Please sign in to comment.