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

fix: when selecting categories, the selected categories get reset #53519

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/hooks/useCleanupSelectedOptions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {NavigationContainerRefContext, useIsFocused} from '@react-navigation/native';
import {useContext, useEffect} from 'react';
import NAVIGATORS from '@src/NAVIGATORS';

const useCleanupSelectedOptions = (cleanupFunction?: () => void) => {
const navigationContainerRef = useContext(NavigationContainerRefContext);
const state = navigationContainerRef?.getState();
const lastRoute = state?.routes.at(-1);
const isRightModalOpening = lastRoute?.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR;

const isFocused = useIsFocused();

useEffect(() => {
if (isFocused || isRightModalOpening) {
return;
}
cleanupFunction?.();
}, [isFocused, cleanupFunction, isRightModalOpening]);
};

export default useCleanupSelectedOptions;
16 changes: 8 additions & 8 deletions src/pages/workspace/categories/WorkspaceCategoriesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useFocusEffect, useIsFocused} from '@react-navigation/native';
import {useFocusEffect} from '@react-navigation/native';
import lodashSortBy from 'lodash/sortBy';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {ActivityIndicator, View} from 'react-native';
Expand All @@ -23,6 +23,7 @@ import TableListItemSkeleton from '@components/Skeletons/TableRowSkeleton';
import Text from '@components/Text';
import TextLink from '@components/TextLink';
import useAutoTurnSelectionModeOffWhenHasNoActiveOption from '@hooks/useAutoTurnSelectionModeOffWhenHasNoActiveOption';
import useCleanupSelectedOptions from '@hooks/useCleanupSelectedOptions';
import useEnvironment from '@hooks/useEnvironment';
import useLocalize from '@hooks/useLocalize';
import useMobileSelectionMode from '@hooks/useMobileSelectionMode';
Expand Down Expand Up @@ -70,7 +71,6 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {
const [selectedCategories, setSelectedCategories] = useState<Record<string, boolean>>({});
const [isDownloadFailureModalVisible, setIsDownloadFailureModalVisible] = useState(false);
const [deleteCategoriesConfirmModalVisible, setDeleteCategoriesConfirmModalVisible] = useState(false);
const isFocused = useIsFocused();
const {environmentURL} = useEnvironment();
const policyId = route.params.policyID ?? '-1';
const backTo = route.params?.backTo;
Expand Down Expand Up @@ -98,12 +98,8 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {
}, [fetchCategories]),
);

useEffect(() => {
if (isFocused) {
return;
}
setSelectedCategories({});
}, [isFocused]);
const cleanupSelectedOption = useCallback(() => setSelectedCategories({}), []);
useCleanupSelectedOptions(cleanupSelectedOption);

const categoryList = useMemo<PolicyOption[]>(
() =>
Expand Down Expand Up @@ -151,6 +147,10 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {
};

const navigateToCategorySettings = (category: PolicyOption) => {
if (isSmallScreenWidth && selectionMode?.isEnabled) {
toggleCategory(category);
return;
}
Navigation.navigate(
isQuickSettingsFlow
? ROUTES.SETTINGS_CATEGORY_SETTINGS.getRoute(policyId, category.keyForList, backTo)
Expand Down
18 changes: 9 additions & 9 deletions src/pages/workspace/tags/WorkspaceTagsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useFocusEffect, useIsFocused} from '@react-navigation/native';
import {useFocusEffect} from '@react-navigation/native';
import lodashSortBy from 'lodash/sortBy';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import React, {useCallback, useMemo, useState} from 'react';
import {ActivityIndicator, View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Button from '@components/Button';
Expand All @@ -22,6 +22,7 @@ import CustomListHeader from '@components/SelectionListWithModal/CustomListHeade
import TableListItemSkeleton from '@components/Skeletons/TableRowSkeleton';
import Text from '@components/Text';
import TextLink from '@components/TextLink';
import useCleanupSelectedOptions from '@hooks/useCleanupSelectedOptions';
import useEnvironment from '@hooks/useEnvironment';
import useLocalize from '@hooks/useLocalize';
import useMobileSelectionMode from '@hooks/useMobileSelectionMode';
Expand Down Expand Up @@ -64,7 +65,6 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {
const [isDownloadFailureModalVisible, setIsDownloadFailureModalVisible] = useState(false);
const [isDeleteTagsConfirmModalVisible, setIsDeleteTagsConfirmModalVisible] = useState(false);
const [isOfflineModalVisible, setIsOfflineModalVisible] = useState(false);
const isFocused = useIsFocused();
const policyID = route.params.policyID ?? '-1';
const backTo = route.params.backTo;
const policy = usePolicy(policyID);
Expand All @@ -87,12 +87,8 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {

useFocusEffect(fetchTags);

useEffect(() => {
if (isFocused) {
return;
}
setSelectedTags({});
}, [isFocused]);
const cleanupSelectedOption = useCallback(() => setSelectedTags({}), []);
useCleanupSelectedOptions(cleanupSelectedOption);

const getPendingAction = (policyTagList: PolicyTagList): PendingAction | undefined => {
if (!policyTagList) {
Expand Down Expand Up @@ -176,6 +172,10 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {
};

const navigateToTagSettings = (tag: TagListItem) => {
if (isSmallScreenWidth && selectionMode?.isEnabled) {
toggleTag(tag);
return;
}
if (tag.orderWeight !== undefined) {
Navigation.navigate(
isQuickSettingsFlow ? ROUTES.SETTINGS_TAG_LIST_VIEW.getRoute(policyID, tag.orderWeight, backTo) : ROUTES.WORKSPACE_TAG_LIST_VIEW.getRoute(policyID, tag.orderWeight),
Expand Down
21 changes: 11 additions & 10 deletions src/pages/workspace/taxes/WorkspaceTaxesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useFocusEffect, useIsFocused} from '@react-navigation/native';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {useFocusEffect} from '@react-navigation/native';
import React, {useCallback, useMemo, useState} from 'react';
import {ActivityIndicator, View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Button from '@components/Button';
Expand All @@ -17,6 +17,7 @@ import SelectionListWithModal from '@components/SelectionListWithModal';
import CustomListHeader from '@components/SelectionListWithModal/CustomListHeader';
import Text from '@components/Text';
import TextLink from '@components/TextLink';
import useCleanupSelectedOptions from '@hooks/useCleanupSelectedOptions';
import useEnvironment from '@hooks/useEnvironment';
import useLocalize from '@hooks/useLocalize';
import useMobileSelectionMode from '@hooks/useMobileSelectionMode';
Expand Down Expand Up @@ -51,7 +52,8 @@ function WorkspaceTaxesPage({
params: {policyID},
},
}: WorkspaceTaxesPageProps) {
const {shouldUseNarrowLayout} = useResponsiveLayout();
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {shouldUseNarrowLayout, isSmallScreenWidth} = useResponsiveLayout();
const styles = useThemeStyles();
const theme = useTheme();
const {translate} = useLocalize();
Expand All @@ -61,7 +63,6 @@ function WorkspaceTaxesPage({
const {selectionMode} = useMobileSelectionMode();
const defaultExternalID = policy?.taxRates?.defaultExternalID;
const foreignTaxDefault = policy?.taxRates?.foreignTaxDefault;
const isFocused = useIsFocused();
const hasAccountingConnections = PolicyUtils.hasAccountingConnections(policy);
const [connectionSyncProgress] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CONNECTION_SYNC_PROGRESS}${policy?.id}`);
const isSyncInProgress = isConnectionInProgress(connectionSyncProgress, policy);
Expand All @@ -86,12 +87,8 @@ function WorkspaceTaxesPage({
}, [fetchTaxes]),
);

useEffect(() => {
if (isFocused) {
return;
}
setSelectedTaxesIDs([]);
}, [isFocused]);
const cleanupSelectedOption = useCallback(() => setSelectedTaxesIDs([]), []);
useCleanupSelectedOptions(cleanupSelectedOption);

const textForDefault = useCallback(
(taxID: string, taxRate: TaxRate): string => {
Expand Down Expand Up @@ -192,6 +189,10 @@ function WorkspaceTaxesPage({
if (!taxRate.keyForList) {
return;
}
if (isSmallScreenWidth && selectionMode?.isEnabled) {
toggleTax(taxRate);
return;
}
Navigation.navigate(ROUTES.WORKSPACE_TAX_EDIT.getRoute(policyID, taxRate.keyForList));
};

Expand Down
Loading