diff --git a/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx b/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx index 0ec937b19ba2..57c69be49809 100644 --- a/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx +++ b/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx @@ -1,6 +1,8 @@ import type {StackScreenProps} from '@react-navigation/stack'; import React from 'react'; import {View} from 'react-native'; +import type {OnyxEntry} from 'react-native-onyx'; +import {withOnyx} from 'react-native-onyx'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; import OfflineWithFeedback from '@components/OfflineWithFeedback'; import ScreenWrapper from '@components/ScreenWrapper'; @@ -9,16 +11,24 @@ import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import {setWorkspaceRequiresCategory} from '@libs/actions/Policy'; +import * as OptionsListUtils from '@libs/OptionsListUtils'; import type {SettingsNavigatorParamList} from '@navigation/types'; import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAccessOrNotFoundWrapper'; import FeatureEnabledAccessOrNotFoundWrapper from '@pages/workspace/FeatureEnabledAccessOrNotFoundWrapper'; import PaidPolicyAccessOrNotFoundWrapper from '@pages/workspace/PaidPolicyAccessOrNotFoundWrapper'; import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; +import type * as OnyxTypes from '@src/types/onyx'; -type WorkspaceCategoriesSettingsPageProps = StackScreenProps; +type WorkspaceCategoriesSettingsPageOnyxProps = { + /** Collection of categories attached to a policy */ + policyCategories: OnyxEntry; +}; -function WorkspaceCategoriesSettingsPage({route}: WorkspaceCategoriesSettingsPageProps) { +type WorkspaceCategoriesSettingsPageProps = WorkspaceCategoriesSettingsPageOnyxProps & StackScreenProps; + +function WorkspaceCategoriesSettingsPage({route, policyCategories}: WorkspaceCategoriesSettingsPageProps) { const styles = useThemeStyles(); const {translate} = useLocalize(); @@ -26,6 +36,7 @@ function WorkspaceCategoriesSettingsPage({route}: WorkspaceCategoriesSettingsPag setWorkspaceRequiresCategory(route.params.policyID, value); }; + const policyCategoriesCount = OptionsListUtils.getEnabledCategoriesCount(policyCategories ?? {}); return ( @@ -53,7 +64,7 @@ function WorkspaceCategoriesSettingsPage({route}: WorkspaceCategoriesSettingsPag isOn={policy?.requiresCategory ?? false} accessibilityLabel={translate('workspace.categories.requiresCategory')} onToggle={updateWorkspaceRequiresCategory} - disabled={!policy?.areCategoriesEnabled} + disabled={!policy?.areCategoriesEnabled || policyCategoriesCount === 0} /> @@ -69,4 +80,8 @@ function WorkspaceCategoriesSettingsPage({route}: WorkspaceCategoriesSettingsPag WorkspaceCategoriesSettingsPage.displayName = 'WorkspaceCategoriesSettingsPage'; -export default WorkspaceCategoriesSettingsPage; +export default withOnyx({ + policyCategories: { + key: ({route}) => `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${route.params.policyID}`, + }, +})(WorkspaceCategoriesSettingsPage);