Skip to content
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
28 changes: 28 additions & 0 deletions src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,30 @@ function deleteWorkspaceCategories(policyID: string, categoryNamesToDelete: stri
}

function enablePolicyCategories(policyID: string, enabled: boolean) {
const onyxUpdatesToDisableCategories: OnyxUpdate[] = [];
if (!enabled) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edge case: From #71645 we need to handle optimistic to updates policy categories to enabled when re-enabling the category feature.

onyxUpdatesToDisableCategories.push(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`,
value: Object.fromEntries(
Object.entries(allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`] ?? {}).map(([categoryName]) => [
categoryName,
{
enabled: false,
},
]),
),
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
requiresCategory: false,
},
},
);
}
const onyxData: OnyxData = {
optimisticData: [
{
Expand Down Expand Up @@ -520,6 +544,10 @@ function enablePolicyCategories(policyID: string, enabled: boolean) {
],
};

if (onyxUpdatesToDisableCategories.length > 0) {
onyxData.optimisticData?.push(...onyxUpdatesToDisableCategories);
}

const parameters: EnablePolicyCategoriesParams = {policyID, enabled};

API.write(WRITE_COMMANDS.ENABLE_POLICY_CATEGORIES, parameters, onyxData);
Expand Down
33 changes: 33 additions & 0 deletions src/libs/actions/Policy/DistanceRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,39 @@ function enablePolicyDistanceRates(policyID: string, enabled: boolean) {
],
};

if (!enabled) {
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
const customUnitID = Object.keys(policy?.customUnits ?? {})[0];
const customUnit = customUnitID ? policy?.customUnits?.[customUnitID] : undefined;

const rateEntries = Object.entries(customUnit?.rates ?? {});
// find the rate to be enabled after disabling the distance rate feature
Comment on lines +133 to +135

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB

Suggested change
const rateEntries = Object.entries(customUnit?.rates ?? {});
// find the rate to be enabled after disabling the distance rate feature
const rateEntries = Object.entries(customUnit?.rates ?? {});
// find the rate to be enabled after disabling the distance rate feature

const rateEntryToBeEnabled = rateEntries[0];

onyxData.optimisticData?.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
customUnits: {
[customUnitID]: {
rates: Object.fromEntries(
rateEntries.map((rateEntry) => {
const [rateID, rate] = rateEntry;
return [
rateID,
{
...rate,
enabled: rateID === rateEntryToBeEnabled[0],
},
];
}),
),
},
},
},
});
}

const parameters: EnablePolicyDistanceRatesParams = {policyID, enabled};

API.write(WRITE_COMMANDS.ENABLE_POLICY_DISTANCE_RATES, parameters, onyxData);
Expand Down
38 changes: 33 additions & 5 deletions src/libs/actions/Policy/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,8 @@ function enablePolicyTags(policyID: string, enabled: boolean) {
},
],
};
const policyTagList = allPolicyTags?.[policyID];

const policyTagList = allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`];
if (!policyTagList) {
const defaultTagList: PolicyTagList = {
Tag: {
Expand All @@ -520,6 +521,33 @@ function enablePolicyTags(policyID: string, enabled: boolean) {
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`,
value: null,
});
} else if (!enabled) {
const policyTag = PolicyUtils.getTagLists(policyTagList)[0];
onyxData.optimisticData?.push(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`,
value: {
[policyTag.name]: {
tags: Object.fromEntries(
Object.keys(policyTag.tags).map((tagName) => [
tagName,
{
enabled: false,
},
]),
),
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
requiresTag: false,
},
},
);
}

const parameters: EnablePolicyTagsParams = {policyID, enabled};
Expand Down Expand Up @@ -692,17 +720,17 @@ function setPolicyTagsRequired(policyID: string, requiresTag: boolean, tagListIn
}

export {
openPolicyTagsPage,
buildOptimisticPolicyRecentlyUsedTags,
setPolicyRequiresTag,
setPolicyTagsRequired,
renamePolicyTaglist,
enablePolicyTags,
createPolicyTag,
renamePolicyTag,
clearPolicyTagErrors,
clearPolicyTagListError,
deletePolicyTags,
enablePolicyTags,
openPolicyTagsPage,
renamePolicyTag,
renamePolicyTaglist,
setWorkspaceTagEnabled,
};

Expand Down