From 6b050576a6f8d93eb32a45943dfc8370810eb62d Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 3 Apr 2024 14:23:06 +0700 Subject: [PATCH] fix disable require category switch once remove any category --- src/libs/OptionsListUtils.ts | 4 +- src/libs/actions/Policy.ts | 45 +++++++++++++++++-- .../WorkspaceCategoriesSettingsPage.tsx | 2 +- src/types/onyx/PolicyTag.ts | 6 +-- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index ca44931e7e8e..56e2492a99b1 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -826,8 +826,8 @@ function getSearchValueForPhoneOrEmail(searchTerm: string) { /** * Verifies that there is at least one enabled option */ -function hasEnabledOptions(options: PolicyCategories | PolicyTag[]): boolean { - return Object.values(options).some((option) => option.enabled); +function hasEnabledOptions(options: PolicyCategories | PolicyTag[], shouldContainPendingDeleteOption = true): boolean { + return Object.values(options).some((option) => option.enabled && (shouldContainPendingDeleteOption || option.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)); } /** diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 778c9a8ff888..2464d7aee722 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3471,15 +3471,21 @@ function clearCategoryErrors(policyID: string, categoryName: string) { } function deleteWorkspaceCategories(policyID: string, categoryNamesToDelete: string[]) { + const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; + const policyCategories = allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`] ?? {}; + const optimisticPolicyCategoriesData = categoryNamesToDelete.reduce>>((acc, categoryName) => { + acc[categoryName] = {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE}; + return acc; + }, {}); + const shouldDisableRequiresCategory = !OptionsListUtils.hasEnabledOptions( + Object.values(policyCategories).filter((category) => !categoryNamesToDelete.includes(category.name) && category.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE), + ); const onyxData: OnyxData = { optimisticData: [ { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`, - value: categoryNamesToDelete.reduce>>((acc, categoryName) => { - acc[categoryName] = {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE}; - return acc; - }, {}), + value: optimisticPolicyCategoriesData, }, ], successData: [ @@ -3506,6 +3512,37 @@ function deleteWorkspaceCategories(policyID: string, categoryNamesToDelete: stri }, ], }; + if (shouldDisableRequiresCategory) { + onyxData.optimisticData?.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + value: { + requiresCategory: false, + pendingFields: { + requiresCategory: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, + }, + }, + }); + onyxData.successData?.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + value: { + pendingFields: { + requiresCategory: null, + }, + }, + }); + onyxData.failureData?.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + value: { + requiresCategory: policy?.requiresCategory, + pendingFields: { + requiresCategory: null, + }, + }, + }); + } const parameters = { policyID, diff --git a/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx b/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx index 260b6b94d4dc..bf17abd463b1 100644 --- a/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx +++ b/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx @@ -36,7 +36,7 @@ function WorkspaceCategoriesSettingsPage({route, policyCategories}: WorkspaceCat setWorkspaceRequiresCategory(route.params.policyID, value); }; - const hasEnabledOptions = OptionsListUtils.hasEnabledOptions(policyCategories ?? {}); + const hasEnabledOptions = OptionsListUtils.hasEnabledOptions(policyCategories ?? {}, false); return ( diff --git a/src/types/onyx/PolicyTag.ts b/src/types/onyx/PolicyTag.ts index f469ac7fff70..37e979fb58f6 100644 --- a/src/types/onyx/PolicyTag.ts +++ b/src/types/onyx/PolicyTag.ts @@ -1,6 +1,6 @@ import type * as OnyxCommon from './OnyxCommon'; -type PolicyTag = { +type PolicyTag = OnyxCommon.OnyxValueWithOfflineFeedback<{ /** Name of a Tag */ name: string; @@ -13,9 +13,9 @@ type PolicyTag = { /** A list of errors keyed by microtime */ errors?: OnyxCommon.Errors | null; -}; +}>; -type PolicyTags = Record>; +type PolicyTags = Record; type PolicyTagList = Record< T,