Skip to content

Commit 55bf8af

Browse files
authored
Merge pull request Expensify#67135 from allgandalf/fixTaxRate
Refactor funciton in TaxRate.ts to remove usage of `Onyx.Connect`
2 parents 698585e + 9ee1795 commit 55bf8af

5 files changed

Lines changed: 107 additions & 34 deletions

File tree

src/libs/actions/TaxRate.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {NullishDeep, OnyxCollection, OnyxEntry} from 'react-native-onyx';
1+
import type {NullishDeep, OnyxEntry} from 'react-native-onyx';
22
import Onyx from 'react-native-onyx';
33
import type {FormOnyxValues} from '@components/Form/types';
44
import type {LocaleContextProps} from '@components/LocaleContextProvider';
@@ -23,16 +23,9 @@ import INPUT_IDS from '@src/types/form/WorkspaceNewTaxForm';
2323
import {default as INPUT_IDS_TAX_CODE} from '@src/types/form/WorkspaceTaxCodeForm';
2424
import type {Policy, TaxRate, TaxRates} from '@src/types/onyx';
2525
import type * as OnyxCommon from '@src/types/onyx/OnyxCommon';
26-
import type {Rate} from '@src/types/onyx/Policy';
26+
import type {CustomUnit, Rate} from '@src/types/onyx/Policy';
2727
import type {OnyxData} from '@src/types/onyx/Request';
2828

29-
let allPolicies: OnyxCollection<Policy>;
30-
Onyx.connect({
31-
key: ONYXKEYS.COLLECTION.POLICY,
32-
waitForCollectionCallback: true,
33-
callback: (value) => (allPolicies = value),
34-
});
35-
3629
/**
3730
* Get tax value with percentage
3831
*/
@@ -421,9 +414,7 @@ function deletePolicyTaxes(policy: OnyxEntry<Policy>, taxesToDelete: string[], l
421414
API.write(WRITE_COMMANDS.DELETE_POLICY_TAXES, parameters, onyxData);
422415
}
423416

424-
function updatePolicyTaxValue(policyID: string, taxID: string, taxValue: number) {
425-
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
426-
const originalTaxRate = {...policy?.taxRates?.taxes[taxID]};
417+
function updatePolicyTaxValue(policyID: string, taxID: string, taxValue: number, originalTaxRate: TaxRate) {
427418
const stringTaxValue = `${taxValue}%`;
428419

429420
const onyxData: OnyxData = {
@@ -487,9 +478,7 @@ function updatePolicyTaxValue(policyID: string, taxID: string, taxValue: number)
487478
API.write(WRITE_COMMANDS.UPDATE_POLICY_TAX_VALUE, parameters, onyxData);
488479
}
489480

490-
function renamePolicyTax(policyID: string, taxID: string, newName: string) {
491-
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
492-
const originalTaxRate = {...policy?.taxRates?.taxes[taxID]};
481+
function renamePolicyTax(policyID: string, taxID: string, newName: string, originalTaxRate: TaxRate) {
493482
const onyxData: OnyxData = {
494483
optimisticData: [
495484
{
@@ -551,10 +540,15 @@ function renamePolicyTax(policyID: string, taxID: string, newName: string) {
551540
API.write(WRITE_COMMANDS.RENAME_POLICY_TAX, parameters, onyxData);
552541
}
553542

554-
function setPolicyTaxCode(policyID: string, oldTaxCode: string, newTaxCode: string) {
555-
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
556-
const originalTaxRate = {...policy?.taxRates?.taxes[oldTaxCode]};
557-
const distanceRateCustomUnit = getDistanceRateCustomUnit(policy);
543+
function setPolicyTaxCode(
544+
policyID: string,
545+
oldTaxCode: string,
546+
newTaxCode: string,
547+
originalTaxRate: TaxRate,
548+
oldForeignTaxDefault: string | undefined,
549+
oldDefaultExternalID: string | undefined,
550+
distanceRateCustomUnit: CustomUnit | undefined,
551+
) {
558552
const optimisticDistanceRateCustomUnit = distanceRateCustomUnit && {
559553
...distanceRateCustomUnit,
560554
rates: {
@@ -574,8 +568,7 @@ function setPolicyTaxCode(policyID: string, oldTaxCode: string, newTaxCode: stri
574568
),
575569
},
576570
};
577-
const oldDefaultExternalID = policy?.taxRates?.defaultExternalID;
578-
const oldForeignTaxDefault = policy?.taxRates?.foreignTaxDefault;
571+
579572
const onyxData: OnyxData = {
580573
optimisticData: [
581574
{

src/pages/workspace/taxes/NamePage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ function NamePage({
4444
const submit = () => {
4545
const taxName = name.trim();
4646
// Do not call the API if the edited tax name is the same as the current tag name
47-
if (currentTaxRate?.name !== taxName) {
48-
renamePolicyTax(policyID, taxID, taxName);
47+
if (currentTaxRate?.name !== taxName && policy?.taxRates?.taxes[taxID]) {
48+
renamePolicyTax(policyID, taxID, taxName, policy?.taxRates?.taxes[taxID]);
4949
}
5050
goBack();
5151
};

src/pages/workspace/taxes/ValuePage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ function ValuePage({
4242

4343
const submit = useCallback(
4444
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAX_VALUE_FORM>) => {
45-
if (defaultValue === values.value) {
45+
if (defaultValue === values.value || !policy?.taxRates?.taxes[taxID]) {
4646
goBack();
4747
return;
4848
}
49-
updatePolicyTaxValue(policyID, taxID, Number(values.value));
49+
updatePolicyTaxValue(policyID, taxID, Number(values.value), policy?.taxRates?.taxes[taxID]);
5050
goBack();
5151
},
52-
[goBack, policyID, taxID, defaultValue],
52+
[defaultValue, policyID, taxID, policy?.taxRates, goBack],
5353
);
5454

5555
if (!currentTaxRate) {

src/pages/workspace/taxes/WorkspaceTaxCodePage.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {setPolicyTaxCode, validateTaxCode} from '@libs/actions/TaxRate';
1414
import Navigation from '@libs/Navigation/Navigation';
1515
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
1616
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
17+
import {getDistanceRateCustomUnit} from '@libs/PolicyUtils';
1718
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
1819
import CONST from '@src/CONST';
1920
import ONYXKEYS from '@src/ONYXKEYS';
@@ -32,6 +33,8 @@ function WorkspaceTaxCodePage({route}: WorkspaceTaxCodePageProps) {
3233
const policy = usePolicy(policyID);
3334
const {inputCallbackRef} = useAutoFocusInput();
3435

36+
const distanceRateCustomUnit = getDistanceRateCustomUnit(policy);
37+
3538
const setTaxCode = useCallback(
3639
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAX_CODE_FORM>) => {
3740
const newTaxCode = values.taxCode.trim();
@@ -40,10 +43,21 @@ function WorkspaceTaxCodePage({route}: WorkspaceTaxCodePageProps) {
4043
return;
4144
}
4245

43-
setPolicyTaxCode(policyID, currentTaxCode, newTaxCode);
46+
if (!policy?.taxRates?.taxes[currentTaxCode]) {
47+
return;
48+
}
49+
setPolicyTaxCode(
50+
policyID,
51+
currentTaxCode,
52+
newTaxCode,
53+
policy?.taxRates?.taxes[currentTaxCode],
54+
policy?.taxRates?.foreignTaxDefault,
55+
policy?.taxRates?.defaultExternalID,
56+
distanceRateCustomUnit,
57+
);
4458
Navigation.goBack(ROUTES.WORKSPACE_TAX_EDIT.getRoute(policyID, currentTaxCode));
4559
},
46-
[currentTaxCode, policyID],
60+
[currentTaxCode, policyID, policy?.taxRates, distanceRateCustomUnit],
4761
);
4862

4963
const validate = useCallback(

tests/actions/PolicyTaxTest.ts

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Onyx from 'react-native-onyx';
2-
import {createPolicyTax, deletePolicyTaxes, renamePolicyTax, setPolicyTaxesEnabled, updatePolicyTaxValue} from '@libs/actions/TaxRate';
2+
import {createPolicyTax, deletePolicyTaxes, renamePolicyTax, setPolicyTaxCode, setPolicyTaxesEnabled, updatePolicyTaxValue} from '@libs/actions/TaxRate';
33
import CONST from '@src/CONST';
44
import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager';
55
import * as Policy from '@src/libs/actions/Policy/Policy';
@@ -12,7 +12,27 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
1212

1313
OnyxUpdateManager();
1414
describe('actions/PolicyTax', () => {
15-
const fakePolicy: PolicyType = {...createRandomPolicy(0), taxRates: CONST.DEFAULT_TAX};
15+
const fakePolicy: PolicyType = {
16+
...createRandomPolicy(0),
17+
taxRates: CONST.DEFAULT_TAX,
18+
customUnits: {
19+
[CONST.CUSTOM_UNITS.NAME_DISTANCE]: {
20+
name: CONST.CUSTOM_UNITS.NAME_DISTANCE,
21+
customUnitID: 'id_CUSTOM_UNIT_1',
22+
enabled: true,
23+
rates: {
24+
// eslint-disable-next-line @typescript-eslint/naming-convention
25+
id_CUSTOM_UNIT_1: {
26+
name: 'Distance',
27+
customUnitRateID: 'id_CUSTOM_UNIT_1',
28+
enabled: true,
29+
currency: 'USD',
30+
rate: 67,
31+
},
32+
},
33+
},
34+
},
35+
};
1636
beforeAll(() => {
1737
Onyx.init({
1838
keys: ONYXKEYS,
@@ -489,7 +509,8 @@ describe('actions/PolicyTax', () => {
489509
const taxID = 'id_TAX_RATE_1';
490510
const newTaxName = 'Tax rate 1 updated';
491511
mockFetch?.pause?.();
492-
renamePolicyTax(fakePolicy.id, taxID, newTaxName);
512+
// @ts-expect-error - we can send undefined tax rate here for testing
513+
renamePolicyTax(fakePolicy.id, taxID, newTaxName, fakePolicy?.taxRates?.taxes[taxID]);
493514
return waitForBatchedUpdates()
494515
.then(
495516
() =>
@@ -534,7 +555,8 @@ describe('actions/PolicyTax', () => {
534555
const newTaxName = 'Tax rate 1 updated';
535556
const originalTaxRate = {...fakePolicy?.taxRates?.taxes[taxID]};
536557
mockFetch?.pause?.();
537-
renamePolicyTax(fakePolicy.id, taxID, newTaxName);
558+
// @ts-expect-error - we can send undefined tax rate here for testing
559+
renamePolicyTax(fakePolicy.id, taxID, newTaxName, fakePolicy?.taxRates?.taxes[taxID]);
538560
return waitForBatchedUpdates()
539561
.then(
540562
() =>
@@ -584,7 +606,8 @@ describe('actions/PolicyTax', () => {
584606
const newTaxValue = 10;
585607
const stringTaxValue = `${newTaxValue}%`;
586608
mockFetch?.pause?.();
587-
updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue);
609+
// @ts-expect-error - we can send undefined tax rate here for testing
610+
updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue, fakePolicy?.taxRates?.taxes[taxID]);
588611
return waitForBatchedUpdates()
589612
.then(
590613
() =>
@@ -630,7 +653,8 @@ describe('actions/PolicyTax', () => {
630653
const originalTaxRate = {...fakePolicy?.taxRates?.taxes[taxID]};
631654
const stringTaxValue = `${newTaxValue}%`;
632655
mockFetch?.pause?.();
633-
updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue);
656+
// @ts-expect-error - we can send undefined tax rate here for testing
657+
updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue, fakePolicy?.taxRates?.taxes[taxID]);
634658
return waitForBatchedUpdates()
635659
.then(
636660
() =>
@@ -827,4 +851,46 @@ describe('actions/PolicyTax', () => {
827851
);
828852
});
829853
});
854+
describe('SetPolicyTaxCode', () => {
855+
const oldTaxCode = 'id_TAX_RATE_1';
856+
const newTaxCode = 'id_TAX_RATE_2';
857+
const oldTaxRateName = fakePolicy?.taxRates?.taxes[oldTaxCode]?.name;
858+
859+
it('Set policy`s tax code', () => {
860+
mockFetch?.pause?.();
861+
const distanceRateCustomUnit = fakePolicy?.customUnits?.[CONST.CUSTOM_UNITS.NAME_DISTANCE];
862+
863+
setPolicyTaxCode(
864+
fakePolicy.id,
865+
oldTaxCode,
866+
newTaxCode,
867+
// @ts-expect-error - we can send undefined tax rate here for testing
868+
fakePolicy?.taxRates?.taxes[oldTaxCode],
869+
fakePolicy?.taxRates?.foreignTaxDefault,
870+
fakePolicy?.taxRates?.defaultExternalID,
871+
distanceRateCustomUnit,
872+
);
873+
874+
return waitForBatchedUpdates().then(
875+
() =>
876+
new Promise<void>((resolve) => {
877+
const connection = Onyx.connect({
878+
key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`,
879+
waitForCollectionCallback: false,
880+
callback: (policy) => {
881+
Onyx.disconnect(connection);
882+
const taxRates = policy?.taxRates;
883+
const updatedTaxRate = taxRates?.taxes?.[newTaxCode];
884+
885+
// We expected to have a new tax rate with the new tax code
886+
expect(updatedTaxRate).toBeDefined();
887+
expect(updatedTaxRate?.previousTaxCode).toBe(oldTaxCode);
888+
expect(updatedTaxRate?.name).toBe(oldTaxRateName);
889+
resolve();
890+
},
891+
});
892+
}),
893+
);
894+
});
895+
});
830896
});

0 commit comments

Comments
 (0)