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
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
42 changes: 42 additions & 0 deletions src/libs/actions/Policy/DistanceRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,48 @@ 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

// first check the default rate
let rateEntryToBeEnabled = rateEntries.find((rate) => rate[1]?.name === 'Default Rate');
// if the default rate is not enabled/doesn't exist, we'll switch to the first enabled rate
Copy link
Contributor

Choose a reason for hiding this comment

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

@dominictb noticed one case,

  1. Create 3 distance rates
  2. Disable the first one and leave the others enabled
  3. Go offline
  4. Disable and enable the feature
  5. You'll notice the 2nd rate will remain enabled while the first one is disabled
  6. Go online
  7. The change will be reverted and only the first one will be re-enabled

I think we need to make sure that we re-enable the first distance rate, even if it was previously disabled.

Screen.Recording.2024-06-18.at.1.35.24.in.the.afternoon.mov

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@getusha lemme check and update. Give me some time, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

web-8MB.mp4

Fixed

if (!rateEntryToBeEnabled?.[1] || !rateEntryToBeEnabled[1].enabled) {
rateEntryToBeEnabled = rateEntries.find((rate) => !!rate[1]?.enabled);
}
// if no rate is enabled, we'll switch to the first rate
if (!rateEntryToBeEnabled?.[1]) {
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 @@ -494,7 +494,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 @@ -514,6 +515,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 @@ -686,17 +714,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