Skip to content

Commit

Permalink
Merge pull request #42381 from bernhardoj/fix/41083-escape-colon-from…
Browse files Browse the repository at this point in the history
…-tag-name

Fix opening tag with colon shows not found page while opening for the first time
  • Loading branch information
techievivek authored May 29, 2024
2 parents e5ec7be + 6e97610 commit 1029cd3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ function getCleanedTagName(tag: string) {
return tag?.replace(/\\{1,2}:/g, CONST.COLON);
}

/**
* Escape colon from tag name
*/
function escapeTagName(tag: string) {
return tag?.replaceAll(CONST.COLON, '\\:');
}

/**
* Gets a count of enabled tags of a policy
*/
Expand Down Expand Up @@ -411,6 +418,7 @@ function getCurrentXeroOrganizationName(policy: Policy | undefined): string | un
export {
canEditTaxRate,
extractPolicyIDFromPath,
escapeTagName,
getActivePolicies,
getAdminEmployees,
getCleanedTagName,
Expand Down
33 changes: 18 additions & 15 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3170,6 +3170,7 @@ function createWorkspaceFromIOUPayment(iouReport: Report | EmptyObject): string

function createPolicyTag(policyID: string, tagName: string) {
const policyTag = PolicyUtils.getTagLists(allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {})?.[0] ?? {};
const newTagName = PolicyUtils.escapeTagName(tagName);

const onyxData: OnyxData = {
optimisticData: [
Expand All @@ -3179,8 +3180,8 @@ function createPolicyTag(policyID: string, tagName: string) {
value: {
[policyTag.name]: {
tags: {
[tagName]: {
name: tagName,
[newTagName]: {
name: newTagName,
enabled: true,
errors: null,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
Expand All @@ -3197,7 +3198,7 @@ function createPolicyTag(policyID: string, tagName: string) {
value: {
[policyTag.name]: {
tags: {
[tagName]: {
[newTagName]: {
errors: null,
pendingAction: null,
},
Expand All @@ -3213,7 +3214,7 @@ function createPolicyTag(policyID: string, tagName: string) {
value: {
[policyTag.name]: {
tags: {
[tagName]: {
[newTagName]: {
errors: ErrorUtils.getMicroSecondOnyxError('workspace.tags.genericFailureMessage'),
},
},
Expand All @@ -3225,7 +3226,7 @@ function createPolicyTag(policyID: string, tagName: string) {

const parameters = {
policyID,
tags: JSON.stringify([{name: tagName}]),
tags: JSON.stringify([{name: newTagName}]),
};

API.write(WRITE_COMMANDS.CREATE_POLICY_TAG, parameters, onyxData);
Expand Down Expand Up @@ -3414,7 +3415,9 @@ 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];
const oldTag = allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`]?.[tagListName]?.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: [
{
Expand All @@ -3423,15 +3426,15 @@ function renamePolicyTag(policyID: string, policyTag: {oldName: string; newName:
value: {
[tagListName]: {
tags: {
[policyTag.oldName]: null,
[policyTag.newName]: {
[oldTagName]: null,
[newTagName]: {
...oldTag,
name: policyTag.newName,
name: newTagName,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
pendingFields: {
name: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
previousTagName: policyTag.oldName,
previousTagName: oldTagName,
},
},
},
Expand All @@ -3445,7 +3448,7 @@ function renamePolicyTag(policyID: string, policyTag: {oldName: string; newName:
value: {
[tagListName]: {
tags: {
[policyTag.newName]: {
[newTagName]: {
errors: null,
pendingAction: null,
pendingFields: {
Expand All @@ -3464,8 +3467,8 @@ function renamePolicyTag(policyID: string, policyTag: {oldName: string; newName:
value: {
[tagListName]: {
tags: {
[policyTag.newName]: null,
[policyTag.oldName]: {
[newTagName]: null,
[oldTagName]: {
...oldTag,
errors: ErrorUtils.getMicroSecondOnyxError('workspace.tags.genericFailureMessage'),
},
Expand All @@ -3478,8 +3481,8 @@ function renamePolicyTag(policyID: string, policyTag: {oldName: string; newName:

const parameters = {
policyID,
oldName: policyTag.oldName,
newName: policyTag.newName,
oldName: oldTagName,
newName: newTagName,
};

API.write(WRITE_COMMANDS.RENAME_POLICY_TAG, parameters, onyxData);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/workspace/tags/EditTagPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Policy.renamePolicyTag(route.params.policyID, {oldName: currentTagName, newName: values.tagName.trim()});
Policy.renamePolicyTag(route.params.policyID, {oldName: route.params.tagName, newName: values.tagName.trim()});
}
Keyboard.dismiss();
Navigation.goBack();
},
[route.params.policyID, currentTagName],
[route.params.policyID, route.params.tagName, currentTagName],
);

return (
Expand Down

0 comments on commit 1029cd3

Please sign in to comment.