Skip to content

Commit

Permalink
Merge pull request #41517 from Expensify/hayata-prevent-deletion-of-c…
Browse files Browse the repository at this point in the history
…ategories-and-tags

Prevent deletion of categories and tags
  • Loading branch information
yuwenmemon authored May 3, 2024
2 parents db96248 + 3571b8f commit 21abc89
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
15 changes: 9 additions & 6 deletions src/pages/workspace/categories/CategorySettingsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import {useOnyx, withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
Expand Down Expand Up @@ -39,6 +39,7 @@ function CategorySettingsPage({route, policyCategories}: CategorySettingsPagePro
const {translate} = useLocalize();
const {windowWidth} = useWindowDimensions();
const [deleteCategoryConfirmModalVisible, setDeleteCategoryConfirmModalVisible] = useState(false);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`);

const policyCategory = policyCategories?.[route.params.categoryName];

Expand All @@ -60,13 +61,15 @@ function CategorySettingsPage({route, policyCategories}: CategorySettingsPagePro
Navigation.dismissModal();
};

const threeDotsMenuItems = [
{
const isThereAnyAccountingConnection = Object.keys(policy?.connections ?? {}).length !== 0;
const threeDotsMenuItems = [];
if (!isThereAnyAccountingConnection) {
threeDotsMenuItems.push({
icon: Expensicons.Trashcan,
text: translate('workspace.categories.deleteCategory'),
onSelected: () => setDeleteCategoryConfirmModalVisible(true),
},
];
});
}

return (
<AccessOrNotFoundWrapper
Expand All @@ -80,7 +83,7 @@ function CategorySettingsPage({route, policyCategories}: CategorySettingsPagePro
testID={CategorySettingsPage.displayName}
>
<HeaderWithBackButton
shouldShowThreeDotsButton
shouldShowThreeDotsButton={threeDotsMenuItems.length > 0}
title={route.params.categoryName}
threeDotsAnchorPosition={styles.threeDotsPopoverOffsetNoCloseButton(windowWidth)}
threeDotsMenuItems={threeDotsMenuItems}
Expand Down
15 changes: 9 additions & 6 deletions src/pages/workspace/categories/WorkspaceCategoriesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,17 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {

const getHeaderButtons = () => {
const options: Array<DropdownOption<DeepValueOf<typeof CONST.POLICY.CATEGORIES_BULK_ACTION_TYPES>>> = [];
const isThereAnyAccountingConnection = Object.keys(policy?.connections ?? {}).length !== 0;

if (selectedCategoriesArray.length > 0) {
options.push({
icon: Expensicons.Trashcan,
text: translate(selectedCategoriesArray.length === 1 ? 'workspace.categories.deleteCategory' : 'workspace.categories.deleteCategories'),
value: CONST.POLICY.CATEGORIES_BULK_ACTION_TYPES.DELETE,
onSelected: () => setDeleteCategoriesConfirmModalVisible(true),
});
if (!isThereAnyAccountingConnection) {
options.push({
icon: Expensicons.Trashcan,
text: translate(selectedCategoriesArray.length === 1 ? 'workspace.categories.deleteCategory' : 'workspace.categories.deleteCategories'),
value: CONST.POLICY.CATEGORIES_BULK_ACTION_TYPES.DELETE,
onSelected: () => setDeleteCategoriesConfirmModalVisible(true),
});
}

const enabledCategories = selectedCategoriesArray.filter((categoryName) => policyCategories?.[categoryName]?.enabled);
if (enabledCategories.length > 0) {
Expand Down
23 changes: 14 additions & 9 deletions src/pages/workspace/tags/TagSettingsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useMemo} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import {useOnyx, withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
Expand Down Expand Up @@ -39,6 +39,7 @@ function TagSettingsPage({route, policyTags}: TagSettingsPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const policyTag = useMemo(() => PolicyUtils.getTagList(policyTags, 0), [policyTags]);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`);

const {windowWidth} = useWindowDimensions();

Expand All @@ -64,6 +65,16 @@ function TagSettingsPage({route, policyTags}: TagSettingsPageProps) {
Navigation.navigate(ROUTES.WORKSPACE_TAG_EDIT.getRoute(route.params.policyID, currentPolicyTag.name));
};

const isThereAnyAccountingConnection = Object.keys(policy?.connections ?? {}).length !== 0;
const threeDotsMenuItems = [];
if (!isThereAnyAccountingConnection) {
threeDotsMenuItems.push({
icon: Trashcan,
text: translate('workspace.tags.deleteTag'),
onSelected: () => setIsDeleteTagModalOpen(true),
});
}

return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
Expand All @@ -77,16 +88,10 @@ function TagSettingsPage({route, policyTags}: TagSettingsPageProps) {
>
<HeaderWithBackButton
title={PolicyUtils.getCleanedTagName(route.params.tagName)}
shouldShowThreeDotsButton
shouldShowThreeDotsButton={threeDotsMenuItems.length > 0}
shouldSetModalVisibility={false}
threeDotsAnchorPosition={styles.threeDotsPopoverOffset(windowWidth)}
threeDotsMenuItems={[
{
icon: Trashcan,
text: translate('workspace.tags.deleteTag'),
onSelected: () => setIsDeleteTagModalOpen(true),
},
]}
threeDotsMenuItems={threeDotsMenuItems}
/>
<ConfirmModal
title={translate('workspace.tags.deleteTag')}
Expand Down
15 changes: 9 additions & 6 deletions src/pages/workspace/tags/WorkspaceTagsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,17 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {

const getHeaderButtons = () => {
const options: Array<DropdownOption<DeepValueOf<typeof CONST.POLICY.TAGS_BULK_ACTION_TYPES>>> = [];
const isThereAnyAccountingConnection = Object.keys(policy?.connections ?? {}).length !== 0;

if (selectedTagsArray.length > 0) {
options.push({
icon: Expensicons.Trashcan,
text: translate(selectedTagsArray.length === 1 ? 'workspace.tags.deleteTag' : 'workspace.tags.deleteTags'),
value: CONST.POLICY.TAGS_BULK_ACTION_TYPES.DELETE,
onSelected: () => setDeleteTagsConfirmModalVisible(true),
});
if (!isThereAnyAccountingConnection) {
options.push({
icon: Expensicons.Trashcan,
text: translate(selectedTagsArray.length === 1 ? 'workspace.tags.deleteTag' : 'workspace.tags.deleteTags'),
value: CONST.POLICY.TAGS_BULK_ACTION_TYPES.DELETE,
onSelected: () => setDeleteTagsConfirmModalVisible(true),
});
}

const enabledTags = selectedTagsArray.filter((tagName) => tagListKeyedByName?.[tagName]?.enabled);
if (enabledTags.length > 0) {
Expand Down

0 comments on commit 21abc89

Please sign in to comment.