From 29cd7ba23421ecc44c315b480394991b875838b5 Mon Sep 17 00:00:00 2001 From: Michal Muzyk Date: Fri, 22 Mar 2024 12:57:50 +0100 Subject: [PATCH 1/3] fix: instantly edit created distance rate --- src/libs/PolicyDistanceRatesUtils.ts | 13 ++++++++++++- .../distanceRates/CreateDistanceRatePage.tsx | 4 ++-- .../distanceRates/PolicyDistanceRateEditPage.tsx | 2 +- .../distanceRates/PolicyDistanceRatesPage.tsx | 6 ++++-- .../reimburse/WorkspaceRateAndUnitPage/RatePage.tsx | 2 +- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/libs/PolicyDistanceRatesUtils.ts b/src/libs/PolicyDistanceRatesUtils.ts index cdcfb13eeb72..dc3ef3647869 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..dcbdb4932076 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, diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx index be85ee680d36..16687e3fb299 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx @@ -13,7 +13,7 @@ import useAutoFocusInput from '@hooks/useAutoFocusInput'; import useLocalize from '@hooks/useLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import Navigation from '@libs/Navigation/Navigation'; -import validateRateValue from '@libs/PolicyDistanceRatesUtils'; +import {validateRateValue} from '@libs/PolicyDistanceRatesUtils'; import type {SettingsNavigatorParamList} from '@navigation/types'; import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAccessOrNotFoundWrapper'; import FeatureEnabledAccessOrNotFoundWrapper from '@pages/workspace/FeatureEnabledAccessOrNotFoundWrapper'; diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx index 93accdb10b28..6533d79e03b9 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx @@ -179,10 +179,12 @@ function PolicyDistanceRatesPage({policy, route}: PolicyDistanceRatesPageProps) }; const toggleAllRates = () => { - 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'; From 1fa4dc1ea3fe26bcb5efeb0a640f9fe00a8dd00a Mon Sep 17 00:00:00 2001 From: Michal Muzyk Date: Fri, 22 Mar 2024 13:59:53 +0100 Subject: [PATCH 2/3] fix: cr fix --- src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx b/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx index dcbdb4932076..0a6b28eb55aa 100644 --- a/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx +++ b/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx @@ -49,7 +49,7 @@ function CreateDistanceRatePage({policy, route}: CreateDistanceRatePageProps) { const submit = (values: FormOnyxValues) => { const newRate: Rate = { currency, - name: getOptimisticRateName(customUnits[customUnitID].rates), + name: getOptimisticRateName(customUnits[customUnitID]?.rates), rate: parseFloat(values.rate) * CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET, customUnitRateID, enabled: true, From 29a2fd13a555e176b6c7392bc0a04442fc512cfd Mon Sep 17 00:00:00 2001 From: Michal Muzyk Date: Fri, 22 Mar 2024 15:07:18 +0100 Subject: [PATCH 3/3] fix: more fixes --- src/libs/PolicyDistanceRatesUtils.ts | 2 +- src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libs/PolicyDistanceRatesUtils.ts b/src/libs/PolicyDistanceRatesUtils.ts index dc3ef3647869..8870609e2c47 100644 --- a/src/libs/PolicyDistanceRatesUtils.ts +++ b/src/libs/PolicyDistanceRatesUtils.ts @@ -29,7 +29,7 @@ function validateRateValue(values: FormOnyxValues, currency: stri * @param rates */ function getOptimisticRateName(rates: Record): string { - const existingRatesWithSameName = Object.values(rates).filter((rate) => (rate.name ?? '').startsWith(CONST.CUSTOM_UNITS.DEFAULT_RATE)); + 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; } diff --git a/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx b/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx index 0a6b28eb55aa..1d01856992f9 100644 --- a/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx +++ b/src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx @@ -70,6 +70,7 @@ function CreateDistanceRatePage({policy, route}: CreateDistanceRatePageProps) { includeSafeAreaPaddingBottom={false} style={[styles.defaultModalContainer]} testID={CreateDistanceRatePage.displayName} + shouldEnableMaxHeight >