Skip to content

Commit

Permalink
Merge pull request #43813 from Krishna2323/krishna2323/issue/43666
Browse files Browse the repository at this point in the history
  • Loading branch information
cead22 authored Jul 15, 2024
2 parents f04cb46 + 52035bc commit e956bee
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/libs/actions/Policy/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as ErrorUtils from '@libs/ErrorUtils';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import Log from '@libs/Log';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import {navigateWhenEnableFeature} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
Expand Down Expand Up @@ -189,29 +190,33 @@ function createPolicyTag(policyID: string, tagName: string) {
function setWorkspaceTagEnabled(policyID: string, tagsToUpdate: Record<string, {name: string; enabled: boolean}>, tagListIndex: number) {
const policyTag = PolicyUtils.getTagLists(allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {})?.[tagListIndex] ?? {};

const optimisticPolicyTagsData = {
...Object.keys(tagsToUpdate).reduce<PolicyTags>((acc, key) => {
acc[key] = {
...policyTag.tags[key],
...tagsToUpdate[key],
errors: null,
pendingFields: {
...policyTag.tags[key].pendingFields,
enabled: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
};

return acc;
}, {}),
};
const shouldDisableRequiredTag = !OptionsListUtils.hasEnabledOptions({...policyTag.tags, ...optimisticPolicyTagsData});

const onyxData: OnyxData = {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`,
value: {
[policyTag.name]: {
tags: {
...Object.keys(tagsToUpdate).reduce<PolicyTags>((acc, key) => {
acc[key] = {
...policyTag.tags[key],
...tagsToUpdate[key],
errors: null,
pendingFields: {
...policyTag.tags[key].pendingFields,
enabled: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
};

return acc;
}, {}),
},
...(shouldDisableRequiredTag ? {required: false, pendingFields: {required: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}} : {}),
tags: optimisticPolicyTagsData,
},
},
},
Expand All @@ -222,6 +227,7 @@ function setWorkspaceTagEnabled(policyID: string, tagsToUpdate: Record<string, {
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`,
value: {
[policyTag.name]: {
...(shouldDisableRequiredTag ? {pendingFields: {required: null}} : {}),
tags: {
...Object.keys(tagsToUpdate).reduce<PolicyTags>((acc, key) => {
acc[key] = {
Expand All @@ -248,6 +254,7 @@ function setWorkspaceTagEnabled(policyID: string, tagsToUpdate: Record<string, {
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`,
value: {
[policyTag.name]: {
...(shouldDisableRequiredTag ? {pendingFields: {required: null}, required: policyTag.required} : {}),
tags: {
...Object.keys(tagsToUpdate).reduce<PolicyTags>((acc, key) => {
acc[key] = {
Expand Down

0 comments on commit e956bee

Please sign in to comment.