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

feat: disable categories/tags/distance rates when feature is disabled #43637

Merged
merged 7 commits into from
Jun 24, 2024
Merged
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) {
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
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
Loading