Skip to content

Commit

Permalink
Merge pull request #41293 from Nodebrute/invalidworkspace
Browse files Browse the repository at this point in the history
Fix app crash with optional chaining
  • Loading branch information
AndrewGable authored May 13, 2024
2 parents 89489c8 + cd23f73 commit c04628a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
27 changes: 16 additions & 11 deletions src/pages/workspace/distanceRates/PolicyDistanceRateDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -45,19 +46,23 @@ 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;

if (!rate) {
return <NotFoundPage />;
}

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);
}
Expand All @@ -69,7 +74,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 = [
Expand Down Expand Up @@ -110,22 +115,22 @@ function PolicyDistanceRateDetailsPage({policy, route}: PolicyDistanceRateDetail
/>
<View style={styles.flexGrow1}>
<OfflineWithFeedback
errors={ErrorUtils.getLatestErrorField(rate, 'enabled')}
errors={ErrorUtils.getLatestErrorField(rate ?? {}, 'enabled')}
pendingAction={rate?.pendingFields?.enabled}
errorRowStyles={styles.mh5}
onClose={() => clearErrorFields('enabled')}
>
<View style={[styles.flexRow, styles.justifyContentBetween, styles.p5]}>
<Text>{translate('workspace.distanceRates.enableRate')}</Text>
<Switch
isOn={rate.enabled ?? false}
isOn={rate?.enabled ?? false}
onToggle={toggleRate}
accessibilityLabel={translate('workspace.distanceRates.enableRate')}
/>
</View>
</OfflineWithFeedback>
<OfflineWithFeedback
errors={ErrorUtils.getLatestErrorField(rate, 'rate')}
errors={ErrorUtils.getLatestErrorField(rate ?? {}, 'rate')}
pendingAction={rate?.pendingFields?.rate ?? rate?.pendingFields?.currency}
errorRowStyles={styles.mh5}
onClose={() => clearErrorFields('rate')}
Expand Down
11 changes: 8 additions & 3 deletions src/pages/workspace/distanceRates/PolicyDistanceRateEditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -39,9 +40,9 @@ 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 currentRateValue = (rate.rate ?? 0).toString();
const rate = customUnit?.rates[rateID];
const currency = rate?.currency ?? CONST.CURRENCY.USD;
const currentRateValue = (rate?.rate ?? 0).toString();

const submitRate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.POLICY_DISTANCE_RATE_EDIT_FORM>) => {
Policy.updatePolicyDistanceRateValue(policyID, customUnit, [{...rate, rate: Number(values.rate) * CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET}]);
Expand All @@ -54,6 +55,10 @@ function PolicyDistanceRateEditPage({policy, route}: PolicyDistanceRateEditPageP
[currency, toLocaleDigit],
);

if (!rate) {
return <NotFoundPage />;
}

return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ function PolicyDistanceRatesSettingsPage({policy, policyCategories, route}: Poli
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}});
Expand Down Expand Up @@ -79,8 +79,8 @@ function PolicyDistanceRatesSettingsPage({policy, policyCategories, route}: Poli
<HeaderWithBackButton title={translate('workspace.common.settings')} />
<View style={styles.flexGrow1}>
<OfflineWithFeedback
errors={ErrorUtils.getLatestErrorField(customUnits[customUnitID], 'attributes')}
pendingAction={customUnits[customUnitID].pendingFields?.attributes}
errors={ErrorUtils.getLatestErrorField(customUnits[customUnitID] ?? {}, 'attributes')}
pendingAction={customUnits[customUnitID]?.pendingFields?.attributes}
errorRowStyles={styles.mh5}
onClose={() => clearErrorFields('attributes')}
>
Expand All @@ -93,8 +93,8 @@ function PolicyDistanceRatesSettingsPage({policy, policyCategories, route}: Poli
</OfflineWithFeedback>
{policy?.areCategoriesEnabled && OptionsListUtils.hasEnabledOptions(policyCategories ?? {}) && (
<OfflineWithFeedback
errors={ErrorUtils.getLatestErrorField(customUnits[customUnitID], 'defaultCategory')}
pendingAction={customUnits[customUnitID].pendingFields?.defaultCategory}
errors={ErrorUtils.getLatestErrorField(customUnits[customUnitID] ?? {}, 'defaultCategory')}
pendingAction={customUnits[customUnitID]?.pendingFields?.defaultCategory}
errorRowStyles={styles.mh5}
onClose={() => clearErrorFields('defaultCategory')}
>
Expand Down

0 comments on commit c04628a

Please sign in to comment.