Skip to content

Commit

Permalink
Merge pull request #49044 from daledah/fix/47613
Browse files Browse the repository at this point in the history
fix: Error when selecting distance rate that no longer has tax rate
  • Loading branch information
MonilBhavsar authored Oct 16, 2024
2 parents a53705d + 60d54e5 commit f60b99c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
49 changes: 48 additions & 1 deletion src/libs/actions/TaxRate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {OnyxCollection} from 'react-native-onyx';
import type {NullishDeep, OnyxCollection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {FormOnyxValues} from '@components/Form/types';
import * as API from '@libs/API';
Expand Down Expand Up @@ -288,6 +288,11 @@ function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) {
const firstTaxID = Object.keys(policyTaxRates ?? {})
.sort((a, b) => a.localeCompare(b))
.at(0);
const customUnits = policy?.customUnits ?? {};
const customUnitID = Object.keys(customUnits).at(0) ?? '-1';
const ratesToUpdate = Object.values(customUnits?.[customUnitID]?.rates ?? {}).filter(
(rate) => !!rate.attributes?.taxRateExternalID && taxesToDelete.includes(rate.attributes?.taxRateExternalID),
);

if (!policyTaxRates) {
console.debug('Policy or tax rates not found');
Expand All @@ -296,6 +301,33 @@ function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) {

const isForeignTaxRemoved = foreignTaxDefault && taxesToDelete.includes(foreignTaxDefault);

const optimisticRates: Record<string, NullishDeep<Rate>> = {};
const successRates: Record<string, Rate> = {};
const failureRates: Record<string, Rate> = {};

ratesToUpdate.forEach((rate) => {
const rateID = rate.customUnitRateID ?? '';
optimisticRates[rateID] = {
attributes: {
taxRateExternalID: null,
taxClaimablePercentage: null,
},
pendingFields: {
taxRateExternalID: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
taxClaimablePercentage: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
};
successRates[rateID] = {pendingFields: {taxRateExternalID: null, taxClaimablePercentage: null}};
failureRates[rateID] = {
attributes: {...rate?.attributes},
pendingFields: {taxRateExternalID: null, taxClaimablePercentage: null},
errorFields: {
taxRateExternalID: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
taxClaimablePercentage: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
},
};
});

const onyxData: OnyxData = {
optimisticData: [
{
Expand All @@ -310,6 +342,11 @@ function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) {
return acc;
}, {}),
},
customUnits: customUnits && {
[customUnitID]: {
rates: optimisticRates,
},
},
},
},
],
Expand All @@ -325,6 +362,11 @@ function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) {
return acc;
}, {}),
},
customUnits: customUnits && {
[customUnitID]: {
rates: successRates,
},
},
},
},
],
Expand All @@ -344,6 +386,11 @@ function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) {
return acc;
}, {}),
},
customUnits: customUnits && {
[customUnitID]: {
rates: failureRates,
},
},
},
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
Expand All @@ -26,22 +25,17 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import type {Rate, TaxRateAttributes} from '@src/types/onyx/Policy';

type PolicyDistanceRateDetailsPageOnyxProps = {
/** Policy details */
policy: OnyxEntry<OnyxTypes.Policy>;
};
type PolicyDistanceRateDetailsPageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.DISTANCE_RATE_DETAILS>;

type PolicyDistanceRateDetailsPageProps = PolicyDistanceRateDetailsPageOnyxProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.DISTANCE_RATE_DETAILS>;

function PolicyDistanceRateDetailsPage({policy, route}: PolicyDistanceRateDetailsPageProps) {
function PolicyDistanceRateDetailsPage({route}: PolicyDistanceRateDetailsPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [isWarningModalVisible, setIsWarningModalVisible] = useState(false);
const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false);
const policyID = route.params.policyID;
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`);
const rateID = route.params.rateID;
const customUnits = policy?.customUnits ?? {};
const customUnit = customUnits[Object.keys(customUnits)[0]];
Expand Down Expand Up @@ -155,7 +149,7 @@ function PolicyDistanceRateDetailsPage({policy, route}: PolicyDistanceRateDetail
</View>
</OfflineWithFeedback>
)}
{isDistanceTrackTaxEnabled && isPolicyTrackTaxEnabled && (
{isDistanceTrackTaxEnabled && !!taxRate && isPolicyTrackTaxEnabled && (
<OfflineWithFeedback
errors={ErrorUtils.getLatestErrorField(rate, 'taxClaimablePercentage')}
pendingAction={rate?.pendingFields?.taxClaimablePercentage}
Expand Down Expand Up @@ -209,8 +203,4 @@ function PolicyDistanceRateDetailsPage({policy, route}: PolicyDistanceRateDetail

PolicyDistanceRateDetailsPage.displayName = 'PolicyDistanceRateDetailsPage';

export default withOnyx<PolicyDistanceRateDetailsPageProps, PolicyDistanceRateDetailsPageOnyxProps>({
policy: {
key: ({route}) => `${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`,
},
})(PolicyDistanceRateDetailsPage);
export default PolicyDistanceRateDetailsPage;

0 comments on commit f60b99c

Please sign in to comment.