Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Simplified Collect][Taxes] - Fix for app crashing when opening Foreign currency default row after deleting the selected tax #38961

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading