diff --git a/src/libs/PolicyDistanceRatesUtils.ts b/src/libs/PolicyDistanceRatesUtils.ts index cdcfb13eeb72..8870609e2c47 100644 --- a/src/libs/PolicyDistanceRatesUtils.ts +++ b/src/libs/PolicyDistanceRatesUtils.ts @@ -1,5 +1,7 @@ import type {FormInputErrors, FormOnyxValues} from '@components/Form/types'; +import CONST from '@src/CONST'; import type ONYXKEYS from '@src/ONYXKEYS'; +import type {Rate} from '@src/types/onyx/Policy'; import * as CurrencyUtils from './CurrencyUtils'; import getPermittedDecimalSeparator from './getPermittedDecimalSeparator'; import * as MoneyRequestUtils from './MoneyRequestUtils'; @@ -22,4 +24,13 @@ function validateRateValue(values: FormOnyxValues, currency: stri return errors; } -export default validateRateValue; +/** + * Get the optimistic rate name in a way that matches BE logic + * @param rates + */ +function getOptimisticRateName(rates: Record): string { + const existingRatesWithSameName = Object.values(rates ?? {}).filter((rate) => (rate.name ?? '').startsWith(CONST.CUSTOM_UNITS.DEFAULT_RATE)); + return existingRatesWithSameName.length ? `${CONST.CUSTOM_UNITS.DEFAULT_RATE} ${existingRatesWithSameName.length + 1}` : CONST.CUSTOM_UNITS.DEFAULT_RATE; +} + +export {validateRateValue, getOptimisticRateName}; diff --git a/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx b/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx index 0a361f3f8e85..1d01856992f9 100644 --- a/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx +++ b/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx @@ -11,7 +11,7 @@ import ScreenWrapper from '@components/ScreenWrapper'; import useAutoFocusInput from '@hooks/useAutoFocusInput'; import useLocalize from '@hooks/useLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; -import validateRateValue from '@libs/PolicyDistanceRatesUtils'; +import {getOptimisticRateName, validateRateValue} from '@libs/PolicyDistanceRatesUtils'; import Navigation from '@navigation/Navigation'; import type {SettingsNavigatorParamList} from '@navigation/types'; import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAccessOrNotFoundWrapper'; @@ -49,7 +49,7 @@ function CreateDistanceRatePage({policy, route}: CreateDistanceRatePageProps) { const submit = (values: FormOnyxValues) => { const newRate: Rate = { currency, - name: CONST.CUSTOM_UNITS.DEFAULT_RATE, + name: getOptimisticRateName(customUnits[customUnitID]?.rates), rate: parseFloat(values.rate) * CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET, customUnitRateID, enabled: true, @@ -70,6 +70,7 @@ function CreateDistanceRatePage({policy, route}: CreateDistanceRatePageProps) { includeSafeAreaPaddingBottom={false} style={[styles.defaultModalContainer]} testID={CreateDistanceRatePage.displayName} + shouldEnableMaxHeight > { - if (selectedDistanceRates.length === Object.values(customUnitRates).length) { + const allSelectableRates = Object.values(customUnitRates).filter((rate) => rate.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); + + if (selectedDistanceRates.length === allSelectableRates.length) { setSelectedDistanceRates([]); } else { - setSelectedDistanceRates([...Object.values(customUnitRates).filter((rate) => rate.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)]); + setSelectedDistanceRates([...allSelectableRates]); } }; diff --git a/src/pages/workspace/reimburse/WorkspaceRateAndUnitPage/RatePage.tsx b/src/pages/workspace/reimburse/WorkspaceRateAndUnitPage/RatePage.tsx index 709e51cba383..8685cd3b1aee 100644 --- a/src/pages/workspace/reimburse/WorkspaceRateAndUnitPage/RatePage.tsx +++ b/src/pages/workspace/reimburse/WorkspaceRateAndUnitPage/RatePage.tsx @@ -9,7 +9,7 @@ import useLocalize from '@hooks/useLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import compose from '@libs/compose'; import Navigation from '@libs/Navigation/Navigation'; -import validateRateValue from '@libs/PolicyDistanceRatesUtils'; +import {validateRateValue} from '@libs/PolicyDistanceRatesUtils'; import withPolicy from '@pages/workspace/withPolicy'; import type {WithPolicyProps} from '@pages/workspace/withPolicy'; import WorkspacePageWithSections from '@pages/workspace/WorkspacePageWithSections';