diff --git a/src/libs/API/parameters/RenamePolicyTaglist.ts b/src/libs/API/parameters/RenamePolicyTaglist.ts deleted file mode 100644 index 8c92c99aa7fb..000000000000 --- a/src/libs/API/parameters/RenamePolicyTaglist.ts +++ /dev/null @@ -1,7 +0,0 @@ -type RenamePolicyTaglist = { - policyID: string; - oldName: string; - newName: string; -}; - -export default RenamePolicyTaglist; diff --git a/src/libs/API/parameters/RenamePolicyTaglistParams.ts b/src/libs/API/parameters/RenamePolicyTaglistParams.ts new file mode 100644 index 000000000000..dd7d08209d32 --- /dev/null +++ b/src/libs/API/parameters/RenamePolicyTaglistParams.ts @@ -0,0 +1,8 @@ +type RenamePolicyTaglistParams = { + policyID: string; + oldName: string; + newName: string; + tagListIndex: number; +}; + +export default RenamePolicyTaglistParams; diff --git a/src/libs/API/parameters/RenamePolicyTagsParams.ts b/src/libs/API/parameters/RenamePolicyTagsParams.ts index bcf38384cf2c..6a384623d91f 100644 --- a/src/libs/API/parameters/RenamePolicyTagsParams.ts +++ b/src/libs/API/parameters/RenamePolicyTagsParams.ts @@ -2,6 +2,7 @@ type RenamePolicyTagsParams = { policyID: string; oldName: string; newName: string; + tagListIndex: number; }; export default RenamePolicyTagsParams; diff --git a/src/libs/API/parameters/index.ts b/src/libs/API/parameters/index.ts index 68097a201120..d040104b8031 100644 --- a/src/libs/API/parameters/index.ts +++ b/src/libs/API/parameters/index.ts @@ -172,7 +172,7 @@ export type {default as SetWorkspaceApprovalModeParams} from './SetWorkspaceAppr export type {default as SetWorkspacePayerParams} from './SetWorkspacePayerParams'; export type {default as SetWorkspaceReimbursementParams} from './SetWorkspaceReimbursementParams'; export type {default as SetPolicyRequiresTag} from './SetPolicyRequiresTag'; -export type {default as RenamePolicyTaglist} from './RenamePolicyTaglist'; +export type {default as RenamePolicyTaglistParams} from './RenamePolicyTaglistParams'; export type {default as SwitchToOldDotParams} from './SwitchToOldDotParams'; export type {default as TrackExpenseParams} from './TrackExpenseParams'; export type {default as EnablePolicyCategoriesParams} from './EnablePolicyCategoriesParams'; diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index 897b300f0471..1c1009bbcede 100644 --- a/src/libs/API/types.ts +++ b/src/libs/API/types.ts @@ -339,7 +339,7 @@ type WriteCommandParameters = { [WRITE_COMMANDS.SET_WORKSPACE_REQUIRES_CATEGORY]: Parameters.SetWorkspaceRequiresCategoryParams; [WRITE_COMMANDS.DELETE_WORKSPACE_CATEGORIES]: Parameters.DeleteWorkspaceCategoriesParams; [WRITE_COMMANDS.SET_POLICY_REQUIRES_TAG]: Parameters.SetPolicyRequiresTag; - [WRITE_COMMANDS.RENAME_POLICY_TAG_LIST]: Parameters.RenamePolicyTaglist; + [WRITE_COMMANDS.RENAME_POLICY_TAG_LIST]: Parameters.RenamePolicyTaglistParams; [WRITE_COMMANDS.CREATE_POLICY_TAG]: Parameters.CreatePolicyTagsParams; [WRITE_COMMANDS.RENAME_POLICY_TAG]: Parameters.RenamePolicyTagsParams; [WRITE_COMMANDS.SET_POLICY_TAGS_ENABLED]: Parameters.SetPolicyTagsEnabled; diff --git a/src/libs/actions/Policy/Tag.ts b/src/libs/actions/Policy/Tag.ts index 7760f29d6703..b4e86f3ca3bb 100644 --- a/src/libs/actions/Policy/Tag.ts +++ b/src/libs/actions/Policy/Tag.ts @@ -1,7 +1,7 @@ import type {NullishDeep, OnyxCollection, OnyxEntry} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as API from '@libs/API'; -import type {EnablePolicyTagsParams, OpenPolicyTagsPageParams, SetPolicyTagsEnabled} from '@libs/API/parameters'; +import type {EnablePolicyTagsParams, OpenPolicyTagsPageParams, RenamePolicyTaglistParams, RenamePolicyTagsParams, SetPolicyTagsEnabled} from '@libs/API/parameters'; import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; import * as ErrorUtils from '@libs/ErrorUtils'; import getIsNarrowLayout from '@libs/getIsNarrowLayout'; @@ -359,28 +359,30 @@ function clearPolicyTagErrors(policyID: string, tagName: string) { }); } -function renamePolicyTag(policyID: string, policyTag: {oldName: string; newName: string}) { - const tagListName = Object.keys(allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {})[0]; +function renamePolicyTag(policyID: string, policyTag: {oldName: string; newName: string}, tagListIndex: number) { + const tagList = PolicyUtils.getTagLists(allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {})?.[tagListIndex] ?? {}; + const tag = tagList.tags?.[policyTag.oldName]; + const oldTagName = policyTag.oldName; const newTagName = PolicyUtils.escapeTagName(policyTag.newName); - const oldTag = allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`]?.[tagListName]?.tags?.[oldTagName] ?? {}; const onyxData: OnyxData = { optimisticData: [ { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, value: { - [tagListName]: { + [tagList.name]: { tags: { [oldTagName]: null, [newTagName]: { - ...oldTag, + ...tag, name: newTagName, pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, pendingFields: { name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, previousTagName: oldTagName, + errors: null, }, }, }, @@ -392,10 +394,9 @@ function renamePolicyTag(policyID: string, policyTag: {oldName: string; newName: onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, value: { - [tagListName]: { + [tagList.name]: { tags: { [newTagName]: { - errors: null, pendingAction: null, pendingFields: { name: null, @@ -411,11 +412,15 @@ function renamePolicyTag(policyID: string, policyTag: {oldName: string; newName: onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, value: { - [tagListName]: { + [tagList.name]: { tags: { [newTagName]: null, [oldTagName]: { - ...oldTag, + ...tag, + pendingAction: null, + pendingFields: { + name: null, + }, errors: ErrorUtils.getMicroSecondOnyxError('workspace.tags.genericFailureMessage'), }, }, @@ -425,10 +430,11 @@ function renamePolicyTag(policyID: string, policyTag: {oldName: string; newName: ], }; - const parameters = { + const parameters: RenamePolicyTagsParams = { policyID, oldName: oldTagName, newName: newTagName, + tagListIndex, }; API.write(WRITE_COMMANDS.RENAME_POLICY_TAG, parameters, onyxData); @@ -482,7 +488,7 @@ function enablePolicyTags(policyID: string, enabled: boolean) { } } -function renamePolicyTaglist(policyID: string, policyTagListName: {oldName: string; newName: string}, policyTags: OnyxEntry) { +function renamePolicyTaglist(policyID: string, policyTagListName: {oldName: string; newName: string}, policyTags: OnyxEntry, tagListIndex: number) { const newName = policyTagListName.newName; const oldName = policyTagListName.oldName; const oldPolicyTags = policyTags?.[oldName] ?? {}; @@ -522,10 +528,11 @@ function renamePolicyTaglist(policyID: string, policyTagListName: {oldName: stri }, ], }; - const parameters = { + const parameters: RenamePolicyTaglistParams = { policyID, oldName, newName, + tagListIndex, }; API.write(WRITE_COMMANDS.RENAME_POLICY_TAG_LIST, parameters, onyxData); diff --git a/src/pages/workspace/tags/EditTagPage.tsx b/src/pages/workspace/tags/EditTagPage.tsx index a6b3b5dd6657..06f8f5853aa5 100644 --- a/src/pages/workspace/tags/EditTagPage.tsx +++ b/src/pages/workspace/tags/EditTagPage.tsx @@ -58,12 +58,12 @@ function EditTagPage({route, policyTags}: EditTagPageProps) { const tagName = values.tagName.trim(); // Do not call the API if the edited tag name is the same as the current tag name if (currentTagName !== tagName) { - Tag.renamePolicyTag(route.params.policyID, {oldName: route.params.tagName, newName: values.tagName.trim()}); + Tag.renamePolicyTag(route.params.policyID, {oldName: route.params.tagName, newName: values.tagName.trim()}, route.params.orderWeight); } Keyboard.dismiss(); Navigation.goBack(); }, - [route.params.policyID, route.params.tagName, currentTagName], + [currentTagName, route.params.policyID, route.params.tagName, route.params.orderWeight], ); return ( diff --git a/src/pages/workspace/tags/WorkspaceEditTagsPage.tsx b/src/pages/workspace/tags/WorkspaceEditTagsPage.tsx index f0b3ada7f2e5..6adf42c2376b 100644 --- a/src/pages/workspace/tags/WorkspaceEditTagsPage.tsx +++ b/src/pages/workspace/tags/WorkspaceEditTagsPage.tsx @@ -47,11 +47,11 @@ function WorkspaceEditTagsPage({route, policyTags}: WorkspaceEditTagsPageProps) const updateTaglistName = useCallback( (values: FormOnyxValues) => { if (values[INPUT_IDS.POLICY_TAGS_NAME] !== taglistName) { - Tag.renamePolicyTaglist(route.params.policyID, {oldName: taglistName, newName: values[INPUT_IDS.POLICY_TAGS_NAME]}, policyTags); + Tag.renamePolicyTaglist(route.params.policyID, {oldName: taglistName, newName: values[INPUT_IDS.POLICY_TAGS_NAME]}, policyTags, route.params.orderWeight); } Navigation.goBack(); }, - [policyTags, route.params.policyID, taglistName], + [policyTags, route.params.orderWeight, route.params.policyID, taglistName], ); return ( diff --git a/tests/actions/PolicyTagTest.ts b/tests/actions/PolicyTagTest.ts index ad473633d876..24ea7fb4504f 100644 --- a/tests/actions/PolicyTagTest.ts +++ b/tests/actions/PolicyTagTest.ts @@ -177,6 +177,7 @@ describe('actions/Policy', () => { newName: newTagListName, }, fakePolicyTags, + Object.values(fakePolicyTags)[0].orderWeight, ); return waitForBatchedUpdates(); }) @@ -244,6 +245,7 @@ describe('actions/Policy', () => { newName: newTagListName, }, fakePolicyTags, + Object.values(fakePolicyTags)[0].orderWeight, ); return waitForBatchedUpdates(); }) @@ -514,10 +516,14 @@ describe('actions/Policy', () => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - Tag.renamePolicyTag(fakePolicy.id, { - oldName: oldTagName, - newName: newTagName, - }); + Tag.renamePolicyTag( + fakePolicy.id, + { + oldName: oldTagName, + newName: newTagName, + }, + 0, + ); return waitForBatchedUpdates(); }) .then( @@ -580,10 +586,14 @@ describe('actions/Policy', () => { .then(() => { mockFetch?.fail?.(); - Tag.renamePolicyTag(fakePolicy.id, { - oldName: oldTagName, - newName: newTagName, - }); + Tag.renamePolicyTag( + fakePolicy.id, + { + oldName: oldTagName, + newName: newTagName, + }, + 0, + ); return waitForBatchedUpdates(); }) .then(mockFetch?.resume)