Skip to content

Commit

Permalink
Merge pull request #54669 from paultsimura/fix/46897-distance-account…
Browse files Browse the repository at this point in the history
…ant-2

fix: distance request sharing
  • Loading branch information
neil-marcellini authored Jan 23, 2025
2 parents 89a1293 + 4130ef5 commit 62b85e8
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 81 deletions.
43 changes: 42 additions & 1 deletion src/components/MoneyRequestConfirmationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,18 @@ function MoneyRequestConfirmationList({
const [didConfirm, setDidConfirm] = useState(isConfirmed);
const [didConfirmSplit, setDidConfirmSplit] = useState(false);

// Clear the form error if it's set to one among the list passed as an argument
const clearFormErrors = useCallback(
(errors: string[]) => {
if (!errors.includes(formError)) {
return;
}

setFormError('');
},
[formError, setFormError],
);

const shouldDisplayFieldError: boolean = useMemo(() => {
if (!isEditingSplitBill) {
return false;
Expand Down Expand Up @@ -353,6 +365,32 @@ function MoneyRequestConfirmationList({
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps -- we don't want this effect to run if it's just setFormError that changes
}, [isFocused, transaction, shouldDisplayFieldError, hasSmartScanFailed, didConfirmSplit]);

useEffect(() => {
// We want this effect to run only when the transaction is moving from Self DM to a workspace chat
if (!transactionID || !isDistanceRequest || !isMovingTransactionFromTrackExpense || !isPolicyExpenseChat) {
return;
}

const errorKey = 'iou.error.invalidRate';
const policyRates = DistanceRequestUtils.getMileageRates(policy);

// If the selected rate belongs to the policy, clear the error
if (customUnitRateID && Object.keys(policyRates).includes(customUnitRateID)) {
clearFormErrors([errorKey]);
return;
}

// If there is a distance rate in the policy that matches the rate and unit of the currently selected mileage rate, select it automatically
const matchingRate = Object.values(policyRates).find((policyRate) => policyRate.rate === mileageRate.rate && policyRate.unit === mileageRate.unit);
if (matchingRate?.customUnitRateID) {
setCustomUnitRateID(transactionID, matchingRate.customUnitRateID);
return;
}

// If none of the above conditions are met, display the rate error
setFormError(errorKey);
}, [isDistanceRequest, isPolicyExpenseChat, transactionID, mileageRate, customUnitRateID, policy, isMovingTransactionFromTrackExpense, setFormError, clearFormErrors]);

const routeError = Object.values(transaction?.errorFields?.route ?? {}).at(0);
const isFirstUpdatedDistanceAmount = useRef(false);

Expand Down Expand Up @@ -693,7 +731,9 @@ function MoneyRequestConfirmationList({
}, [isTypeSplit, translate, payeePersonalDetails, getSplitSectionHeader, splitParticipants, selectedParticipants]);

useEffect(() => {
if (!isDistanceRequest || isMovingTransactionFromTrackExpense || !transactionID) {
if (!isDistanceRequest || (isMovingTransactionFromTrackExpense && !isPolicyExpenseChat) || !transactionID) {
// We don't want to recalculate the distance merchant when moving a transaction from Track Expense to a 1:1 chat, because the distance rate will be the same default P2P rate.
// When moving to a policy chat (e.g. sharing with an accountant), we should recalculate the distance merchant with the policy's rate.
return;
}

Expand All @@ -716,6 +756,7 @@ function MoneyRequestConfirmationList({
translate,
toLocaleDigit,
isDistanceRequest,
isPolicyExpenseChat,
transaction,
transactionID,
action,
Expand Down
2 changes: 2 additions & 0 deletions src/components/MoneyRequestConfirmationListFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ function MoneyRequestConfirmationListFooter({
const taxRateTitle = getTaxName(policy, transaction);
// Determine if the merchant error should be displayed
const shouldDisplayMerchantError = isMerchantRequired && (shouldDisplayFieldError || formError === 'iou.error.invalidMerchant') && isMerchantEmpty;
const shouldDisplayDistanceRateError = formError === 'iou.error.invalidRate';
// The empty receipt component should only show for IOU Requests of a paid policy ("Team" or "Corporate")
const shouldShowReceiptEmptyState = iouType === CONST.IOU.TYPE.SUBMIT && isPaidGroupPolicy(policy) && !isPerDiemRequest;
// The per diem custom unit
Expand Down Expand Up @@ -397,6 +398,7 @@ function MoneyRequestConfirmationListFooter({

Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_DISTANCE_RATE.getRoute(action, iouType, transactionID, reportID, Navigation.getActiveRouteWithoutParams()));
}}
brickRoadIndicator={shouldDisplayDistanceRateError ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
disabled={didConfirm}
interactive={!!rate && !isReadOnly && isPolicyExpenseChat}
/>
Expand Down
3 changes: 3 additions & 0 deletions src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import type {
MarkedReimbursedParams,
MarkReimbursedFromIntegrationParams,
MissingPropertyParams,
MovedFromSelfDMParams,
NoLongerHaveAccessParams,
NotAllowedExtensionParams,
NotYouParams,
Expand Down Expand Up @@ -990,6 +991,7 @@ const translations = {
threadExpenseReportName: ({formattedAmount, comment}: ThreadRequestReportNameParams) => `${formattedAmount} ${comment ? `for ${comment}` : 'expense'}`,
threadTrackReportName: ({formattedAmount, comment}: ThreadRequestReportNameParams) => `Tracking ${formattedAmount} ${comment ? `for ${comment}` : ''}`,
threadPaySomeoneReportName: ({formattedAmount, comment}: ThreadSentMoneyReportNameParams) => `${formattedAmount} sent${comment ? ` for ${comment}` : ''}`,
movedFromSelfDM: ({workspaceName, reportName}: MovedFromSelfDMParams) => `moved expense from self DM to ${workspaceName ?? `chat with ${reportName}`}`,
tagSelection: 'Select a tag to better organize your spend.',
categorySelection: 'Select a category to better organize your spend.',
error: {
Expand Down Expand Up @@ -1022,6 +1024,7 @@ const translations = {
invalidQuantity: 'Please enter a valid quantity.',
quantityGreaterThanZero: 'Quantity must be greater than zero.',
invalidSubrateLength: 'There must be at least one subrate.',
invalidRate: 'Rate not valid for this workspace. Please select an available rate from the workspace.',
},
waitingOnEnabledWallet: ({submitterDisplayName}: WaitingOnBankAccountParams) => `started settling up. Payment is on hold until ${submitterDisplayName} enables their wallet.`,
enableWallet: 'Enable wallet',
Expand Down
3 changes: 3 additions & 0 deletions src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import type {
MarkedReimbursedParams,
MarkReimbursedFromIntegrationParams,
MissingPropertyParams,
MovedFromSelfDMParams,
NoLongerHaveAccessParams,
NotAllowedExtensionParams,
NotYouParams,
Expand Down Expand Up @@ -988,6 +989,7 @@ const translations = {
threadExpenseReportName: ({formattedAmount, comment}: ThreadRequestReportNameParams) => `${comment ? `${formattedAmount} para ${comment}` : `Gasto de ${formattedAmount}`}`,
threadTrackReportName: ({formattedAmount, comment}: ThreadRequestReportNameParams) => `Seguimiento ${formattedAmount} ${comment ? `para ${comment}` : ''}`,
threadPaySomeoneReportName: ({formattedAmount, comment}: ThreadSentMoneyReportNameParams) => `${formattedAmount} enviado${comment ? ` para ${comment}` : ''}`,
movedFromSelfDM: ({workspaceName, reportName}: MovedFromSelfDMParams) => `movió el gasto desde su propio mensaje directo a ${workspaceName ?? `un chat con ${reportName}`}`,
tagSelection: 'Selecciona una etiqueta para organizar mejor tus gastos.',
categorySelection: 'Selecciona una categoría para organizar mejor tus gastos.',
error: {
Expand Down Expand Up @@ -1020,6 +1022,7 @@ const translations = {
invalidQuantity: 'Por favor, introduce una cantidad válida.',
quantityGreaterThanZero: 'La cantidad debe ser mayor que cero.',
invalidSubrateLength: 'Debe haber al menos una subtasa.',
invalidRate: 'Tasa no válida para este espacio de trabajo. Por favor, selecciona una tasa disponible en el espacio de trabajo.',
},
waitingOnEnabledWallet: ({submitterDisplayName}: WaitingOnBankAccountParams) => `inició el pago, pero no se procesará hasta que ${submitterDisplayName} active su billetera`,
enableWallet: 'Habilitar billetera',
Expand Down
3 changes: 3 additions & 0 deletions src/languages/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ type ThreadRequestReportNameParams = {formattedAmount: string; comment: string};

type ThreadSentMoneyReportNameParams = {formattedAmount: string; comment: string};

type MovedFromSelfDMParams = {workspaceName?: string; reportName?: string};

type SizeExceededParams = {maxUploadSizeInMB: number};

type ResolutionConstraintsParams = {minHeightInPx: number; minWidthInPx: number; maxHeightInPx: number; maxWidthInPx: number};
Expand Down Expand Up @@ -690,6 +692,7 @@ export type {
LoggedInAsParams,
ManagerApprovedAmountParams,
ManagerApprovedParams,
MovedFromSelfDMParams,
SignUpNewFaceCodeParams,
NoLongerHaveAccessParams,
NotAllowedExtensionParams,
Expand Down
6 changes: 6 additions & 0 deletions src/libs/API/parameters/CategorizeTrackedExpenseParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type CategorizeTrackedExpenseParams = {
taxCode: string;
taxAmount: number;
billable?: boolean;
waypoints?: string;
customUnitRateID?: string;
policyExpenseChatReportID?: string;
policyExpenseCreatedReportActionID?: string;
adminsChatReportID?: string;
adminsCreatedReportActionID?: string;
};

export default CategorizeTrackedExpenseParams;
2 changes: 2 additions & 0 deletions src/libs/API/parameters/ShareTrackedExpenseParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type ShareTrackedExpenseParams = {
taxCode: string;
taxAmount: number;
billable?: boolean;
waypoints?: string;
customUnitRateID?: string;
policyExpenseChatReportID?: string;
policyExpenseCreatedReportActionID?: string;
adminsChatReportID?: string;
Expand Down
Loading

0 comments on commit 62b85e8

Please sign in to comment.