Skip to content

Commit 3c26cac

Browse files
authored
Merge pull request #48237 from Krishna2323/krishna2323/issue/47800
fix: Use local Onyx key to throttle location permission prompts.
2 parents cb0ccbc + 55701ea commit 3c26cac

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

src/CONST.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,7 @@ const CONST = {
20462046
OPTIMISTIC_TRANSACTION_ID: '1',
20472047
// Note: These payment types are used when building IOU reportAction message values in the server and should
20482048
// not be changed.
2049+
LOCATION_PERMISSION_PROMPT_THRESHOLD_DAYS: 7,
20492050
PAYMENT_TYPE: {
20502051
ELSEWHERE: 'Elsewhere',
20512052
EXPENSIFY: 'Expensify',

src/ONYXKEYS.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ const ONYXKEYS = {
133133
/** The NVP with the last payment method used per policy */
134134
NVP_LAST_PAYMENT_METHOD: 'nvp_private_lastPaymentMethod',
135135

136+
/** Last date (yyyy-MM-dd HH:mm:ss) when the location permission prompt was shown. */
137+
NVP_LAST_LOCATION_PERMISSION_PROMPT: 'nvp_lastLocalPermissionPrompt',
138+
136139
/** This NVP holds to most recent waypoints that a person has used when creating a distance expense */
137140
NVP_RECENT_WAYPOINTS: 'nvp_expensify_recentWaypoints',
138141

@@ -903,6 +906,7 @@ type OnyxValuesMapping = {
903906
[ONYXKEYS.NVP_DISMISSED_HOLD_USE_EXPLANATION]: boolean;
904907
[ONYXKEYS.FOCUS_MODE_NOTIFICATION]: boolean;
905908
[ONYXKEYS.NVP_LAST_PAYMENT_METHOD]: OnyxTypes.LastPaymentMethod;
909+
[ONYXKEYS.NVP_LAST_LOCATION_PERMISSION_PROMPT]: string;
906910
[ONYXKEYS.LAST_EXPORT_METHOD]: OnyxTypes.LastExportMethod;
907911
[ONYXKEYS.NVP_RECENT_WAYPOINTS]: OnyxTypes.RecentWaypoint[];
908912
[ONYXKEYS.NVP_INTRO_SELECTED]: OnyxTypes.IntroSelected;

src/libs/DateUtils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
addHours,
44
addMilliseconds,
55
addMinutes,
6+
differenceInDays,
67
eachDayOfInterval,
78
eachMonthOfInterval,
89
endOfDay,
@@ -825,6 +826,25 @@ function isCardExpired(expiryMonth: number, expiryYear: number): boolean {
825826
return expiryYear < currentYear || (expiryYear === currentYear && expiryMonth < currentMonth);
826827
}
827828

829+
/**
830+
* Returns the difference in the number of days from the provided date to/from now.
831+
* @param - The date to compare.
832+
* @returns The difference in days as an integer.
833+
*/
834+
function getDifferenceInDaysFromNow(date: Date) {
835+
return differenceInDays(new Date(), date);
836+
}
837+
838+
/**
839+
* Returns a boolean value indicating whether the provided date string can be parsed as a valid date.
840+
* @param dateString string
841+
* @returns True if the date string is valid, otherwise false.
842+
*/
843+
function isValidDateString(dateString: string) {
844+
const date = new Date(dateString);
845+
return !Number.isNaN(date.getTime());
846+
}
847+
828848
const DateUtils = {
829849
isDate,
830850
formatToDayOfWeek,
@@ -870,6 +890,8 @@ const DateUtils = {
870890
getFormattedTransportDate,
871891
doesDateBelongToAPastYear,
872892
isCardExpired,
893+
getDifferenceInDaysFromNow,
894+
isValidDateString,
873895
};
874896

875897
export default DateUtils;

src/libs/actions/IOU.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8289,6 +8289,10 @@ function mergeDuplicates(params: TransactionMergeParams) {
82898289
API.write(WRITE_COMMANDS.TRANSACTION_MERGE, params, {optimisticData, failureData});
82908290
}
82918291

8292+
function updateLastLocationPermissionPrompt() {
8293+
Onyx.set(ONYXKEYS.NVP_LAST_LOCATION_PERMISSION_PROMPT, new Date().toISOString());
8294+
}
8295+
82928296
/** Instead of merging the duplicates, it updates the transaction we want to keep and puts the others on hold without deleting them */
82938297
function resolveDuplicates(params: TransactionMergeParams) {
82948298
const originalSelectedTransaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`];
@@ -8479,6 +8483,7 @@ export {
84798483
updateMoneyRequestTaxAmount,
84808484
updateMoneyRequestTaxRate,
84818485
mergeDuplicates,
8486+
updateLastLocationPermissionPrompt,
84828487
resolveDuplicates,
84838488
getIOUReportActionToApproveOrPay,
84848489
};

src/pages/iou/request/step/IOURequestStepConfirmation.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import useLocalize from '@hooks/useLocalize';
1515
import useNetwork from '@hooks/useNetwork';
1616
import useThemeStyles from '@hooks/useThemeStyles';
1717
import useWindowDimensions from '@hooks/useWindowDimensions';
18+
import DateUtils from '@libs/DateUtils';
1819
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
1920
import * as FileUtils from '@libs/fileDownload/FileUtils';
2021
import getCurrentPosition from '@libs/getCurrentPosition';
@@ -74,6 +75,7 @@ function IOURequestStepConfirmation({
7475
const [receiptFile, setReceiptFile] = useState<OnyxEntry<Receipt>>();
7576
const requestType = TransactionUtils.getRequestType(transaction);
7677
const isDistanceRequest = requestType === CONST.IOU.REQUEST_TYPE.DISTANCE;
78+
const [lastLocationPermissionPrompt] = useOnyx(ONYXKEYS.NVP_LAST_LOCATION_PERMISSION_PROMPT);
7779

7880
const receiptFilename = transaction?.filename;
7981
const receiptPath = transaction?.receipt?.source;
@@ -580,9 +582,17 @@ function IOURequestStepConfirmation({
580582

581583
const onConfirm = (listOfParticipants: Participant[]) => {
582584
setSelectedParticipantList(listOfParticipants);
585+
583586
if (gpsRequired) {
584-
setStartLocationPermissionFlow(true);
585-
return;
587+
const shouldStartLocationPermissionFlow =
588+
!lastLocationPermissionPrompt ||
589+
(DateUtils.isValidDateString(lastLocationPermissionPrompt ?? '') &&
590+
DateUtils.getDifferenceInDaysFromNow(new Date(lastLocationPermissionPrompt ?? '')) > CONST.IOU.LOCATION_PERMISSION_PROMPT_THRESHOLD_DAYS);
591+
592+
if (shouldStartLocationPermissionFlow) {
593+
setStartLocationPermissionFlow(true);
594+
return;
595+
}
586596
}
587597

588598
createTransaction(listOfParticipants);
@@ -617,7 +627,10 @@ function IOURequestStepConfirmation({
617627
startPermissionFlow={startLocationPermissionFlow}
618628
resetPermissionFlow={() => setStartLocationPermissionFlow(false)}
619629
onGrant={() => createTransaction(selectedParticipantList, true)}
620-
onDeny={() => createTransaction(selectedParticipantList, false)}
630+
onDeny={() => {
631+
IOU.updateLastLocationPermissionPrompt();
632+
createTransaction(selectedParticipantList, false);
633+
}}
621634
/>
622635
)}
623636
<MoneyRequestConfirmationList

0 commit comments

Comments
 (0)