From 584ce1e58c7908bba00c807c5b1a144659a63240 Mon Sep 17 00:00:00 2001 From: Anusha Date: Tue, 30 Apr 2024 08:49:34 +0500 Subject: [PATCH 1/5] Fix app crash with optional chaining --- .../PolicyDistanceRateDetailsPage.tsx | 22 +++++++++---------- .../PolicyDistanceRateEditPage.tsx | 4 ++-- .../PolicyDistanceRatesSettingsPage.tsx | 16 +++++++------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRateDetailsPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRateDetailsPage.tsx index a75fc74c33cc..adf4e68664ad 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRateDetailsPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRateDetailsPage.tsx @@ -45,19 +45,19 @@ function PolicyDistanceRateDetailsPage({policy, route}: PolicyDistanceRateDetail const rateID = route.params.rateID; const customUnits = policy?.customUnits ?? {}; const customUnit = customUnits[Object.keys(customUnits)[0]]; - const rate = customUnit.rates[rateID]; - const currency = rate.currency ?? CONST.CURRENCY.USD; - const canDeleteRate = Object.values(customUnit.rates).filter((distanceRate) => distanceRate.enabled).length > 1 || !rate.enabled; - const canDisableRate = Object.values(customUnit.rates).filter((distanceRate) => distanceRate.enabled).length > 1; - const errorFields = rate.errorFields; + const rate = customUnit?.rates[rateID]; + const currency = rate?.currency ?? CONST.CURRENCY.USD; + const canDeleteRate = Object.values(customUnit?.rates ?? {}).filter((distanceRate) => distanceRate?.enabled).length > 1 || !rate?.enabled; + const canDisableRate = Object.values(customUnit?.rates ?? {}).filter((distanceRate) => distanceRate?.enabled).length > 1; + const errorFields = rate?.errorFields; const editRateValue = () => { Navigation.navigate(ROUTES.WORKSPACE_DISTANCE_RATE_EDIT.getRoute(policyID, rateID)); }; const toggleRate = () => { - if (!rate.enabled || canDisableRate) { - Policy.setPolicyDistanceRatesEnabled(policyID, customUnit, [{...rate, enabled: !rate.enabled}]); + if (!rate?.enabled || canDisableRate) { + Policy.setPolicyDistanceRatesEnabled(policyID, customUnit, [{...rate, enabled: !rate?.enabled}]); } else { setIsWarningModalVisible(true); } @@ -69,7 +69,7 @@ function PolicyDistanceRateDetailsPage({policy, route}: PolicyDistanceRateDetail setIsDeleteModalVisible(false); }; - const rateValueToDisplay = CurrencyUtils.convertAmountToDisplayString(rate.rate, currency); + const rateValueToDisplay = CurrencyUtils.convertAmountToDisplayString(rate?.rate, currency); const unitToDisplay = translate(`common.${customUnit?.attributes?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES}`); const threeDotsMenuItems = [ @@ -110,7 +110,7 @@ function PolicyDistanceRateDetailsPage({policy, route}: PolicyDistanceRateDetail /> clearErrorFields('enabled')} @@ -118,14 +118,14 @@ function PolicyDistanceRateDetailsPage({policy, route}: PolicyDistanceRateDetail {translate('workspace.distanceRates.enableRate')} clearErrorFields('rate')} diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx index 6c853557e01a..62ab867d5d0f 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx @@ -39,8 +39,8 @@ function PolicyDistanceRateEditPage({policy, route}: PolicyDistanceRateEditPageP const rateID = route.params.rateID; const customUnits = policy?.customUnits ?? {}; const customUnit = customUnits[Object.keys(customUnits)[0]]; - const rate = customUnit.rates[rateID]; - const currency = rate.currency ?? CONST.CURRENCY.USD; + const rate = customUnit?.rates[rateID] ?? {}; + const currency = rate?.currency ?? CONST.CURRENCY.USD; const currentRateValue = (rate.rate ?? 0).toString(); const submitRate = (values: FormOnyxValues) => { diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRatesSettingsPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRatesSettingsPage.tsx index d756084e5e49..647d49480d2d 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRatesSettingsPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRatesSettingsPage.tsx @@ -41,10 +41,10 @@ function PolicyDistanceRatesSettingsPage({policy, policyCategories, route}: Poli const customUnits = policy?.customUnits ?? {}; const customUnit = customUnits[Object.keys(customUnits)[0]]; const customUnitID = customUnit?.customUnitID ?? ''; - - const defaultCategory = customUnits[customUnitID].defaultCategory; - const defaultUnit = customUnits[customUnitID].attributes.unit; - const errorFields = customUnits[customUnitID].errorFields; + + const defaultCategory = customUnits[customUnitID]?.defaultCategory; + const defaultUnit = customUnits[customUnitID]?.attributes.unit; + const errorFields = customUnits[customUnitID]?.errorFields; const setNewUnit = (unit: UnitItemType) => { Policy.setPolicyDistanceRatesUnit(policyID, customUnit, {...customUnit, attributes: {unit: unit.value}}); @@ -79,8 +79,8 @@ function PolicyDistanceRatesSettingsPage({policy, policyCategories, route}: Poli clearErrorFields('attributes')} > @@ -93,8 +93,8 @@ function PolicyDistanceRatesSettingsPage({policy, policyCategories, route}: Poli {policy?.areCategoriesEnabled && OptionsListUtils.hasEnabledOptions(policyCategories ?? {}) && ( clearErrorFields('defaultCategory')} > From e4a0074e187fb57c6e30101f6ee9366827238e96 Mon Sep 17 00:00:00 2001 From: Anusha Date: Tue, 30 Apr 2024 09:05:39 +0500 Subject: [PATCH 2/5] npm run prettier --- .../workspace/distanceRates/PolicyDistanceRatesSettingsPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRatesSettingsPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRatesSettingsPage.tsx index 647d49480d2d..c3fb676858b3 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRatesSettingsPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRatesSettingsPage.tsx @@ -41,7 +41,7 @@ function PolicyDistanceRatesSettingsPage({policy, policyCategories, route}: Poli const customUnits = policy?.customUnits ?? {}; const customUnit = customUnits[Object.keys(customUnits)[0]]; const customUnitID = customUnit?.customUnitID ?? ''; - + const defaultCategory = customUnits[customUnitID]?.defaultCategory; const defaultUnit = customUnits[customUnitID]?.attributes.unit; const errorFields = customUnits[customUnitID]?.errorFields; From a68edd261c879d5562388599b79891929d7b1e7e Mon Sep 17 00:00:00 2001 From: Anusha Date: Mon, 6 May 2024 05:46:28 +0500 Subject: [PATCH 3/5] show not found page on invalid id --- .../distanceRates/PolicyDistanceRateDetailsPage.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRateDetailsPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRateDetailsPage.tsx index adf4e68664ad..f9c56b8763c3 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRateDetailsPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRateDetailsPage.tsx @@ -18,6 +18,7 @@ import * as CurrencyUtils from '@libs/CurrencyUtils'; import * as ErrorUtils from '@libs/ErrorUtils'; import Navigation from '@libs/Navigation/Navigation'; import type {SettingsNavigatorParamList} from '@navigation/types'; +import NotFoundPage from '@pages/ErrorPage/NotFoundPage'; import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper'; import * as Policy from '@userActions/Policy'; import CONST from '@src/CONST'; @@ -51,6 +52,10 @@ function PolicyDistanceRateDetailsPage({policy, route}: PolicyDistanceRateDetail const canDisableRate = Object.values(customUnit?.rates ?? {}).filter((distanceRate) => distanceRate?.enabled).length > 1; const errorFields = rate?.errorFields; + if (!rate) { + return ; + } + const editRateValue = () => { Navigation.navigate(ROUTES.WORKSPACE_DISTANCE_RATE_EDIT.getRoute(policyID, rateID)); }; From 165e1813e9ed7a85fea576a22a5c55f53caefcc7 Mon Sep 17 00:00:00 2001 From: Anusha Date: Mon, 6 May 2024 05:57:23 +0500 Subject: [PATCH 4/5] on edit page show not found on invalid rate --- .../distanceRates/PolicyDistanceRateEditPage.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx index 62ab867d5d0f..2b3aeb85e79f 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx @@ -15,6 +15,7 @@ import useThemeStyles from '@hooks/useThemeStyles'; import Navigation from '@libs/Navigation/Navigation'; import {validateRateValue} from '@libs/PolicyDistanceRatesUtils'; import type {SettingsNavigatorParamList} from '@navigation/types'; +import NotFoundPage from '@pages/ErrorPage/NotFoundPage'; import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper'; import * as Policy from '@userActions/Policy'; import CONST from '@src/CONST'; @@ -39,9 +40,13 @@ function PolicyDistanceRateEditPage({policy, route}: PolicyDistanceRateEditPageP const rateID = route.params.rateID; const customUnits = policy?.customUnits ?? {}; const customUnit = customUnits[Object.keys(customUnits)[0]]; - const rate = customUnit?.rates[rateID] ?? {}; + const rate = customUnit?.rates[rateID]; const currency = rate?.currency ?? CONST.CURRENCY.USD; - const currentRateValue = (rate.rate ?? 0).toString(); + const currentRateValue = (rate?.rate ?? 0).toString(); + + if (!rate) { + return ; + } const submitRate = (values: FormOnyxValues) => { Policy.updatePolicyDistanceRateValue(policyID, customUnit, [{...rate, rate: Number(values.rate) * CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET}]); From cd23f737461c22820f1c5919c0656a35674f6c6c Mon Sep 17 00:00:00 2001 From: Anusha Date: Mon, 6 May 2024 06:07:46 +0500 Subject: [PATCH 5/5] lint fix --- .../distanceRates/PolicyDistanceRateEditPage.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx index 2b3aeb85e79f..9335cff61a5b 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx @@ -44,10 +44,6 @@ function PolicyDistanceRateEditPage({policy, route}: PolicyDistanceRateEditPageP const currency = rate?.currency ?? CONST.CURRENCY.USD; const currentRateValue = (rate?.rate ?? 0).toString(); - if (!rate) { - return ; - } - const submitRate = (values: FormOnyxValues) => { Policy.updatePolicyDistanceRateValue(policyID, customUnit, [{...rate, rate: Number(values.rate) * CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET}]); Keyboard.dismiss(); @@ -59,6 +55,10 @@ function PolicyDistanceRateEditPage({policy, route}: PolicyDistanceRateEditPageP [currency, toLocaleDigit], ); + if (!rate) { + return ; + } + return (