Skip to content

Commit

Permalink
Merge pull request #43637 from dominictb/feat/39463
Browse files Browse the repository at this point in the history
feat: disable categories/tags/distance rates when feature is disabled
  • Loading branch information
mountiny authored Jun 24, 2024
2 parents cbc21e0 + 0bbe021 commit 24f3bdc
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
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
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

0 comments on commit 24f3bdc

Please sign in to comment.