Skip to content

Commit

Permalink
add setpolicy tax code onyx method
Browse files Browse the repository at this point in the history
  • Loading branch information
rushatgabhane committed Jul 9, 2024
1 parent f782d3f commit bee9d44
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/libs/API/parameters/UpdatePolicyTaxCodeParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
type UpdatePolicyTaxCodeParams = {
policyID: string;
taxID: string;
oldTaxCode: string;
newTaxCode: string;
};
Expand Down
74 changes: 73 additions & 1 deletion src/libs/actions/TaxRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import type {OnyxCollection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {FormOnyxValues} from '@components/Form/types';
import * as API from '@libs/API';
import type {CreatePolicyTaxParams, DeletePolicyTaxesParams, RenamePolicyTaxParams, SetPolicyTaxesEnabledParams, UpdatePolicyTaxValueParams} from '@libs/API/parameters';
import type {
CreatePolicyTaxParams,
DeletePolicyTaxesParams,
RenamePolicyTaxParams,
SetPolicyTaxesEnabledParams,
UpdatePolicyTaxCodeParams,
UpdatePolicyTaxValueParams,
} from '@libs/API/parameters';
import {WRITE_COMMANDS} from '@libs/API/types';
import {translateLocal} from '@libs/Localize';
import * as ValidationUtils from '@libs/ValidationUtils';
Expand Down Expand Up @@ -463,6 +470,70 @@ function renamePolicyTax(policyID: string, taxID: string, newName: string) {
API.write(WRITE_COMMANDS.RENAME_POLICY_TAX, parameters, onyxData);
}

function setPolicyTaxCode(policyID: string, oldTaxCode: string, newTaxCode: string) {
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
const originalTaxRate = {...policy?.taxRates?.taxes[oldTaxCode]};
const onyxData: OnyxData = {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
taxRates: {
taxes: {
[newTaxCode]: {
...originalTaxRate,
pendingFields: {code: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE},

This comment has been minimized.

Copy link
@c3024

c3024 Sep 8, 2024

Contributor

While offline, if some field was updated before tax code the pending field for that previously updated field was lost here. This was fixed in #47947.

pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
errorFields: {code: null},
},
},
},
},
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
taxRates: {
taxes: {
[newTaxCode]: {pendingFields: {code: null}, pendingAction: null, errorFields: {code: null}},
},
},
},
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
taxRates: {
taxes: {
[newTaxCode]: {
code: originalTaxRate.code,
pendingFields: {code: null},
pendingAction: null,
errorFields: {code: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('workspace.taxes.error.updateFailureMessage')},
},
},
},
},
},
],
};

const parameters: UpdatePolicyTaxCodeParams = {
policyID,
oldTaxCode,
newTaxCode,
};

API.write(WRITE_COMMANDS.UPDATE_POLICY_TAX_CODE, parameters, onyxData);
}

export {
createPolicyTax,
getNextTaxCode,
Expand All @@ -475,4 +546,5 @@ export {
deletePolicyTaxes,
updatePolicyTaxValue,
renamePolicyTax,
setPolicyTaxCode,
};
22 changes: 14 additions & 8 deletions src/pages/workspace/taxes/WorkspaceTaxCodePage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useCallback, useState} from 'react';
import React, {useCallback} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import FormProvider from '@components/Form/FormProvider';
Expand All @@ -14,7 +14,6 @@ import useThemeStyles from '@hooks/useThemeStyles';
import {renamePolicyTax, validateTaxName} from '@libs/actions/TaxRate';
import Navigation from '@libs/Navigation/Navigation';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import Parser from '@libs/Parser';
import * as PolicyUtils from '@libs/PolicyUtils';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
Expand All @@ -33,22 +32,29 @@ function WorkspaceTaxCodePage({route}: WorkspaceTaxCodePageProps) {
const currentTaxCode = route.params.taxID;

const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
const currentTaxRate = PolicyUtils.getTaxByID(policy, currentTaxCode);
const {inputCallbackRef} = useAutoFocusInput();

const [name, setName] = useState(() => Parser.htmlToMarkdown(currentTaxRate?.name ?? ''));

const goBack = useCallback(() => Navigation.goBack(ROUTES.WORKSPACE_TAX_EDIT.getRoute(policyID ?? '-1', taxID)), [policyID, taxID]);

const submit = () => {
const taxName = name.trim();
// Do not call the API if the edited tax name is the same as the current tag name
if (currentTaxRate?.name !== taxName) {
renamePolicyTax(policyID, taxID, taxName);
}
goBack();
Navigation.goBack();
};

const editTaxCode = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAX_CODE_FORM>) => {
const newTaxCode = values.taxCode.trim();
// Do not call the API if the edited category name is the same as the current category name
if (currentTaxCode !== newTaxCode) {
Category.renamePolicyCategory(route.params.policyID, {oldName: currentCategoryName, newName: values.categoryName});
}
Navigation.goBack();
},
[backTo, currentCategoryName, route.params.categoryName, route.params.policyID],
);

const validate = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAX_CODE_FORM>) => {
if (!policy) {
Expand Down

0 comments on commit bee9d44

Please sign in to comment.