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

feat: load policy categories only when needed #49756

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
36 changes: 12 additions & 24 deletions src/components/CategoryPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
import React, {useMemo} from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import SelectionList from './SelectionList';
import RadioListItem from './SelectionList/RadioListItem';
import type {ListItem} from './SelectionList/types';

type CategoryPickerOnyxProps = {
policyCategories: OnyxEntry<OnyxTypes.PolicyCategories>;
policyCategoriesDraft: OnyxEntry<OnyxTypes.PolicyCategories>;
policyRecentlyUsedCategories: OnyxEntry<OnyxTypes.RecentlyUsedCategories>;
};

type CategoryPickerProps = CategoryPickerOnyxProps & {
/** It's used by withOnyx HOC */
// eslint-disable-next-line react/no-unused-prop-types
type CategoryPickerProps = {
policyID: string;
selectedCategory?: string;
onSubmit: (item: ListItem) => void;
};

function CategoryPicker({selectedCategory, policyCategories, policyRecentlyUsedCategories, policyCategoriesDraft, onSubmit}: CategoryPickerProps) {
function CategoryPicker({selectedCategory, policyID, onSubmit}: CategoryPickerProps) {
const [policyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`);
const [policyCategoriesDraft] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES_DRAFT}${policyID}`);
const [policyRecentlyUsedCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`);
const {isOffline} = useNetwork();

const {translate} = useLocalize();
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');
const offlineMessage = isOffline ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : '';

const selectedOptions = useMemo(() => {
if (!selectedCategory) {
Expand Down Expand Up @@ -79,6 +76,7 @@ function CategoryPicker({selectedCategory, policyCategories, policyRecentlyUsedC
headerMessage={headerMessage}
textInputValue={searchValue}
textInputLabel={shouldShowTextInput ? translate('common.search') : undefined}
textInputHint={offlineMessage}
onChangeText={setSearchValue}
onSelectRow={onSubmit}
ListItem={RadioListItem}
Expand All @@ -90,14 +88,4 @@ function CategoryPicker({selectedCategory, policyCategories, policyRecentlyUsedC

CategoryPicker.displayName = 'CategoryPicker';

export default withOnyx<CategoryPickerProps, CategoryPickerOnyxProps>({
policyCategories: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`,
},
policyCategoriesDraft: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES_DRAFT}${policyID}`,
},
policyRecentlyUsedCategories: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`,
},
})(CategoryPicker);
export default CategoryPicker;
5 changes: 5 additions & 0 deletions src/libs/API/parameters/GetPolicyCategories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type GetPolicyCategories = {
policyID: string;
};

export default GetPolicyCategories;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type {default as ExpandURLPreviewParams} from './ExpandURLPreviewParams';
export type {default as GetMissingOnyxMessagesParams} from './GetMissingOnyxMessagesParams';
export type {default as GetNewerActionsParams} from './GetNewerActionsParams';
export type {default as GetOlderActionsParams} from './GetOlderActionsParams';
export type {default as GetPolicyCategoriesParams} from './GetPolicyCategories';
export type {default as GetReportPrivateNoteParams} from './GetReportPrivateNoteParams';
export type {default as GetRouteParams} from './GetRouteParams';
export type {default as GetStatementPDFParams} from './GetStatementPDFParams';
Expand Down
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ const READ_COMMANDS = {
BEGIN_SIGNIN: 'BeginSignIn',
SIGN_IN_WITH_SHORT_LIVED_AUTH_TOKEN: 'SignInWithShortLivedAuthToken',
SIGN_IN_WITH_SUPPORT_AUTH_TOKEN: 'SignInWithSupportAuthToken',
GET_POLICY_CATEGORIES: 'GetPolicyCategories',
OPEN_WORKSPACE: 'OpenWorkspace',
OPEN_WORKSPACE_MEMBERS_PAGE: 'OpenWorkspaceMembersPage',
OPEN_POLICY_CATEGORIES_PAGE: 'OpenPolicyCategoriesPage',
Expand Down Expand Up @@ -916,6 +917,7 @@ type ReadCommandParameters = {
[READ_COMMANDS.BEGIN_SIGNIN]: Parameters.BeginSignInParams;
[READ_COMMANDS.SIGN_IN_WITH_SHORT_LIVED_AUTH_TOKEN]: Parameters.SignInWithShortLivedAuthTokenParams;
[READ_COMMANDS.SIGN_IN_WITH_SUPPORT_AUTH_TOKEN]: Parameters.SignInWithSupportAuthTokenParams;
[READ_COMMANDS.GET_POLICY_CATEGORIES]: Parameters.GetPolicyCategoriesParams;
[READ_COMMANDS.OPEN_WORKSPACE]: Parameters.OpenWorkspaceParams;
[READ_COMMANDS.OPEN_WORKSPACE_MEMBERS_PAGE]: Parameters.OpenWorkspaceMembersPageParams;
[READ_COMMANDS.OPEN_POLICY_CATEGORIES_PAGE]: Parameters.OpenPolicyCategoriesPageParams;
Expand Down
10 changes: 5 additions & 5 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ Onyx.connect({
},
});

let allPolicyCategories: OnyxCollection<PolicyCategories> = {};
let allPolicies: OnyxCollection<Policy> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY_CATEGORIES,
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (val) => (allPolicyCategories = val),
callback: (val) => (allPolicies = val),
});

const lastReportActions: ReportActions = {};
Expand Down Expand Up @@ -1975,8 +1975,8 @@ function getOptions(
reportOption.isBold = shouldBoldTitleByDefault || shouldUseBoldText(reportOption);

if (action === CONST.IOU.ACTION.CATEGORIZE) {
const policyCategories = allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${reportOption.policyID}`] ?? {};
if (getEnabledCategoriesCount(policyCategories) !== 0) {
const reportPolicy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${reportOption.policyID}`];
if (reportPolicy?.areCategoriesEnabled) {
recentReportOptions.push(reportOption);
}
} else {
Expand Down
15 changes: 15 additions & 0 deletions src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Onyx from 'react-native-onyx';
import * as API from '@libs/API';
import type {
EnablePolicyCategoriesParams,
GetPolicyCategoriesParams,
OpenPolicyCategoriesPageParams,
RemovePolicyCategoryReceiptsRequiredParams,
SetPolicyCategoryApproverParams,
Expand Down Expand Up @@ -187,6 +188,19 @@ function openPolicyCategoriesPage(policyID: string) {
API.read(READ_COMMANDS.OPEN_POLICY_CATEGORIES_PAGE, params);
}

function getPolicyCategories(policyID: string) {
if (!policyID || policyID === '-1' || policyID === CONST.POLICY.ID_FAKE) {
Log.warn('GetPolicyCategories invalid params', {policyID});
return;
}

const params: GetPolicyCategoriesParams = {
policyID,
};

API.read(READ_COMMANDS.GET_POLICY_CATEGORIES, params);
}

function buildOptimisticPolicyRecentlyUsedCategories(policyID?: string, category?: string) {
if (!policyID || !category) {
return [];
Expand Down Expand Up @@ -1312,6 +1326,7 @@ function setPolicyCategoryTax(policyID: string, categoryName: string, taxID: str
}

export {
getPolicyCategories,
openPolicyCategoriesPage,
buildOptimisticPolicyRecentlyUsedCategories,
setWorkspaceCategoryEnabled,
Expand Down
Loading
Loading