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

fix gray-out and name issue in offline category update #51927

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
19 changes: 17 additions & 2 deletions src/libs/CategoryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {LocaleContextProps} from '@components/LocaleContextProvider';
import CONST from '@src/CONST';
import type {Policy, TaxRate, TaxRatesWithDefault} from '@src/types/onyx';
import type {ApprovalRule, ExpenseRule} from '@src/types/onyx/Policy';
import type {ApprovalRule, ExpenseRule, MccGroup} from '@src/types/onyx/Policy';
import * as CurrencyUtils from './CurrencyUtils';

function formatDefaultTaxRateText(translate: LocaleContextProps['translate'], taxID: string, taxRate: TaxRate, policyTaxRates?: TaxRatesWithDefault) {
Expand Down Expand Up @@ -68,4 +68,19 @@ function getCategoryDefaultTaxRate(expenseRules: ExpenseRule[], categoryName: st
return categoryDefaultTaxRate;
}

export {formatDefaultTaxRateText, formatRequireReceiptsOverText, getCategoryApproverRule, getCategoryExpenseRule, getCategoryDefaultTaxRate};
function updateCategoryInMccGroup(mccGroups: Record<string, MccGroup>, oldCategoryName: string, newCategoryName: string, shouldClearPendingAction?: boolean) {
if (oldCategoryName === newCategoryName) {
return mccGroups;
}

const updatedGroups: Record<string, MccGroup> = {};

for (const [key, group] of Object.entries(mccGroups || {})) {
updatedGroups[key] =
group.category === oldCategoryName ? {...group, category: newCategoryName, pendingAction: shouldClearPendingAction ? null : CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE} : group;
}

return updatedGroups;
}

export {formatDefaultTaxRateText, formatRequireReceiptsOverText, getCategoryApproverRule, getCategoryExpenseRule, getCategoryDefaultTaxRate, updateCategoryInMccGroup};
15 changes: 14 additions & 1 deletion src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyCategories, PolicyCategory, RecentlyUsedCategories, Report} from '@src/types/onyx';
import type {ApprovalRule, ExpenseRule} from '@src/types/onyx/Policy';
import type {ApprovalRule, ExpenseRule, MccGroup} from '@src/types/onyx/Policy';
import type {PolicyCategoryExpenseLimitType} from '@src/types/onyx/PolicyCategory';
import type {OnyxData} from '@src/types/onyx/Request';

Expand Down Expand Up @@ -549,8 +549,12 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string
const policyCategoryExpenseRule = CategoryUtils.getCategoryExpenseRule(policy?.rules?.expenseRules ?? [], policyCategory.oldName);
const approvalRules = policy?.rules?.approvalRules ?? [];
const expenseRules = policy?.rules?.expenseRules ?? [];
const mccGroup = policy?.mccGroup ?? {};
const updatedApprovalRules: ApprovalRule[] = lodashCloneDeep(approvalRules);
const updatedExpenseRules: ExpenseRule[] = lodashCloneDeep(expenseRules);
const clonedMccGroup: Record<string, MccGroup> = lodashCloneDeep(mccGroup);
const updatedMccGroup = CategoryUtils.updateCategoryInMccGroup(clonedMccGroup, policyCategory.oldName, policyCategory.newName);
const updatedMccGroupWithClearedPendingAction = CategoryUtils.updateCategoryInMccGroup(clonedMccGroup, policyCategory.oldName, policyCategory.newName, true);

if (policyCategoryExpenseRule) {
const ruleIndex = updatedExpenseRules.findIndex((rule) => rule.id === policyCategoryExpenseRule.id);
Expand Down Expand Up @@ -603,10 +607,18 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string
approvalRules: updatedApprovalRules,
expenseRules: updatedExpenseRules,
},
mccGroup: updatedMccGroup,
},
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
mccGroup: updatedMccGroupWithClearedPendingAction,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`,
Expand Down Expand Up @@ -646,6 +658,7 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string
rules: {
approvalRules,
},
mccGroup,
},
},
],
Expand Down
1 change: 1 addition & 0 deletions src/types/onyx/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1923,4 +1923,5 @@ export type {
ApprovalRule,
ExpenseRule,
NetSuiteConnectionConfig,
MccGroup,
};
Loading