Skip to content

Commit

Permalink
Merge pull request #38961 from software-mansion-labs/wave8/fix-foreig…
Browse files Browse the repository at this point in the history
…n-tax-removing

[Simplified Collect][Taxes] - Fix for app crashing when opening Foreign currency default row after deleting the selected tax
  • Loading branch information
luacmartins authored Mar 25, 2024
2 parents 8bb301b + a883c05 commit b25eb15
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/TaxPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function TaxPicker({selectedTaxRate = '', taxRates, insets, onSubmit}: TaxPicker

const shouldShowTextInput = !isTaxRatesCountBelowThreshold;

const getTaxName = useCallback((key: string) => taxRates?.taxes[key].name, [taxRates?.taxes]);
const getTaxName = useCallback((key: string) => taxRates?.taxes[key]?.name, [taxRates?.taxes]);

const selectedOptions = useMemo(() => {
if (!selectedTaxRate) {
Expand Down
8 changes: 8 additions & 0 deletions src/libs/actions/TaxRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,25 @@ type TaxRateDeleteMap = Record<
function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) {
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
const policyTaxRates = policy?.taxRates?.taxes;
const foreignTaxDefault = policy?.taxRates?.foreignTaxDefault;
const firstTaxID = Object.keys(policyTaxRates ?? {}).sort((a, b) => a.localeCompare(b))[0];

if (!policyTaxRates) {
console.debug('Policy or tax rates not found');
return;
}

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

const onyxData: OnyxData = {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
taxRates: {
pendingFields: {foreignTaxDefault: isForeignTaxRemoved ? CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE : null},
foreignTaxDefault: isForeignTaxRemoved ? firstTaxID : foreignTaxDefault,
taxes: taxesToDelete.reduce<TaxRateDeleteMap>((acc, taxID) => {
acc[taxID] = {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, errors: null};
return acc;
Expand All @@ -287,6 +293,7 @@ function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) {
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
taxRates: {
pendingFields: {foreignTaxDefault: null},
taxes: taxesToDelete.reduce<TaxRateDeleteMap>((acc, taxID) => {
acc[taxID] = null;
return acc;
Expand All @@ -301,6 +308,7 @@ function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) {
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
taxRates: {
pendingFields: {foreignTaxDefault: null},
taxes: taxesToDelete.reduce<TaxRateDeleteMap>((acc, taxID) => {
acc[taxID] = {
pendingAction: null,
Expand Down

0 comments on commit b25eb15

Please sign in to comment.