From 062d53c0ce98185162d153ddd0b5c86e7925213c Mon Sep 17 00:00:00 2001 From: Filip Solecki Date: Mon, 25 Mar 2024 21:47:44 +0100 Subject: [PATCH 1/2] Fix removing foreign tax default --- src/components/TaxPicker.tsx | 2 +- src/libs/actions/TaxRate.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/TaxPicker.tsx b/src/components/TaxPicker.tsx index 61a13d271e7d..936bd23b530d 100644 --- a/src/components/TaxPicker.tsx +++ b/src/components/TaxPicker.tsx @@ -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) { diff --git a/src/libs/actions/TaxRate.ts b/src/libs/actions/TaxRate.ts index 3f2420c76f87..ae62cadc3d0a 100644 --- a/src/libs/actions/TaxRate.ts +++ b/src/libs/actions/TaxRate.ts @@ -260,12 +260,16 @@ 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(policy?.taxRates?.taxes ?? {}).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: [ { @@ -273,6 +277,8 @@ function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) { 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((acc, taxID) => { acc[taxID] = {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, errors: null}; return acc; @@ -287,6 +293,7 @@ function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) { key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { taxRates: { + pendingFields: {foreignTaxDefault: null}, taxes: taxesToDelete.reduce((acc, taxID) => { acc[taxID] = null; return acc; @@ -301,6 +308,7 @@ function deletePolicyTaxes(policyID: string, taxesToDelete: string[]) { key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { taxRates: { + pendingFields: {foreignTaxDefault: null}, taxes: taxesToDelete.reduce((acc, taxID) => { acc[taxID] = { pendingAction: null, From a883c05e11d31752e0706ee23dbbd891cfdd0b30 Mon Sep 17 00:00:00 2001 From: filip-solecki <153545991+filip-solecki@users.noreply.github.com> Date: Mon, 25 Mar 2024 22:14:16 +0100 Subject: [PATCH 2/2] Update src/libs/actions/TaxRate.ts Co-authored-by: Carlos Martins --- src/libs/actions/TaxRate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/TaxRate.ts b/src/libs/actions/TaxRate.ts index ae62cadc3d0a..c8de4a2abdfc 100644 --- a/src/libs/actions/TaxRate.ts +++ b/src/libs/actions/TaxRate.ts @@ -261,7 +261,7 @@ 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(policy?.taxRates?.taxes ?? {}).sort((a, b) => a.localeCompare(b))[0]; + const firstTaxID = Object.keys(policyTaxRates ?? {}).sort((a, b) => a.localeCompare(b))[0]; if (!policyTaxRates) { console.debug('Policy or tax rates not found');