Skip to content

Commit

Permalink
Merge pull request #37457 from Expensify/cmartins-loadCategories
Browse files Browse the repository at this point in the history
Call OpenPolicyCategoriesPage
  • Loading branch information
luacmartins authored Mar 5, 2024
2 parents 7002422 + 2c0afdc commit 7a56d16
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/libs/API/parameters/OpenPolicyCategoriesPageParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type OpenPolicyCategoriesPageParams = {
policyID: string;
};

export default OpenPolicyCategoriesPageParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export type {default as OpenWorkspaceViewParams} from './OpenWorkspaceViewParams
export type {default as OpenWorkspaceReimburseViewParams} from './OpenWorkspaceReimburseViewParams';
export type {default as OpenWorkspaceInvitePageParams} from './OpenWorkspaceInvitePageParams';
export type {default as OpenWorkspaceMembersPageParams} from './OpenWorkspaceMembersPageParams';
export type {default as OpenPolicyCategoriesPageParams} from './OpenPolicyCategoriesPageParams';
export type {default as OpenDraftWorkspaceRequestParams} from './OpenDraftWorkspaceRequestParams';
export type {default as UpdateWorkspaceCustomUnitAndRateParams} from './UpdateWorkspaceCustomUnitAndRateParams';
export type {default as CreateWorkspaceFromIOUPaymentParams} from './CreateWorkspaceFromIOUPaymentParams';
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 @@ -340,6 +340,7 @@ const READ_COMMANDS = {
OPEN_WORKSPACE_REIMBURSE_VIEW: 'OpenWorkspaceReimburseView',
OPEN_WORKSPACE: 'OpenWorkspace',
OPEN_WORKSPACE_MEMBERS_PAGE: 'OpenWorkspaceMembersPage',
OPEN_POLICY_CATEGORIES_PAGE: 'OpenPolicyCategoriesPage',
OPEN_WORKSPACE_INVITE_PAGE: 'OpenWorkspaceInvitePage',
OPEN_DRAFT_WORKSPACE_REQUEST: 'OpenDraftWorkspaceRequest',
} as const;
Expand Down Expand Up @@ -374,6 +375,7 @@ type ReadCommandParameters = {
[READ_COMMANDS.OPEN_WORKSPACE_REIMBURSE_VIEW]: Parameters.OpenWorkspaceReimburseViewParams;
[READ_COMMANDS.OPEN_WORKSPACE]: Parameters.OpenWorkspaceParams;
[READ_COMMANDS.OPEN_WORKSPACE_MEMBERS_PAGE]: Parameters.OpenWorkspaceMembersPageParams;
[READ_COMMANDS.OPEN_POLICY_CATEGORIES_PAGE]: Parameters.OpenPolicyCategoriesPageParams;
[READ_COMMANDS.OPEN_WORKSPACE_INVITE_PAGE]: Parameters.OpenWorkspaceInvitePageParams;
[READ_COMMANDS.OPEN_DRAFT_WORKSPACE_REQUEST]: Parameters.OpenDraftWorkspaceRequestParams;
};
Expand Down
15 changes: 15 additions & 0 deletions src/libs/actions/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
DeleteWorkspaceAvatarParams,
DeleteWorkspaceParams,
OpenDraftWorkspaceRequestParams,
OpenPolicyCategoriesPageParams,
OpenWorkspaceInvitePageParams,
OpenWorkspaceMembersPageParams,
OpenWorkspaceParams,
Expand Down Expand Up @@ -1830,6 +1831,19 @@ function openWorkspaceMembersPage(policyID: string, clientMemberEmails: string[]
API.read(READ_COMMANDS.OPEN_WORKSPACE_MEMBERS_PAGE, params);
}

function openPolicyCategoriesPage(policyID: string) {
if (!policyID) {
Log.warn('openPolicyCategoriesPage invalid params', {policyID});
return;
}

const params: OpenPolicyCategoriesPageParams = {
policyID,
};

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

function openWorkspaceInvitePage(policyID: string, clientMemberEmails: string[]) {
if (!policyID || !clientMemberEmails) {
Log.warn('openWorkspaceInvitePage invalid params', {policyID, clientMemberEmails});
Expand Down Expand Up @@ -2516,6 +2530,7 @@ export {
generatePolicyID,
createWorkspace,
openWorkspaceMembersPage,
openPolicyCategoriesPage,
openWorkspaceInvitePage,
openWorkspace,
removeWorkspace,
Expand Down
42 changes: 33 additions & 9 deletions src/pages/workspace/categories/WorkspaceCategoriesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useMemo, useState} from 'react';
import {View} from 'react-native';
import React, {useEffect, useMemo, useState} from 'react';
import {ActivityIndicator, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import Button from '@components/Button';
Expand All @@ -14,13 +14,16 @@ import TableListItem from '@components/SelectionList/TableListItem';
import Text from '@components/Text';
import WorkspaceEmptyStateSection from '@components/WorkspaceEmptyStateSection';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import Navigation from '@libs/Navigation/Navigation';
import type {CentralPaneNavigatorParamList} from '@navigation/types';
import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAccessOrNotFoundWrapper';
import PaidPolicyAccessOrNotFoundWrapper from '@pages/workspace/PaidPolicyAccessOrNotFoundWrapper';
import * as Policy from '@userActions/Policy';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
Expand Down Expand Up @@ -48,6 +51,17 @@ function WorkspaceCategoriesPage({policyCategories, route}: WorkspaceCategoriesP
const {translate} = useLocalize();
const [selectedCategories, setSelectedCategories] = useState<Record<string, boolean>>({});

function fetchCategories() {
Policy.openPolicyCategoriesPage(route.params.policyID);
}

const {isOffline} = useNetwork({onReconnect: fetchCategories});

useEffect(() => {
fetchCategories();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const categoryList = useMemo<PolicyForList[]>(
() =>
Object.values(policyCategories ?? {}).map((value) => ({
Expand Down Expand Up @@ -97,6 +111,8 @@ function WorkspaceCategoriesPage({policyCategories, route}: WorkspaceCategoriesP
Navigation.navigate(ROUTES.WORKSPACE_CATEGORY_SETTINGS.getRoute(route.params.policyID, category.text));
};

const isLoading = !isOffline && policyCategories === undefined;

const settingsButton = (
<View style={[styles.w100, styles.flexRow, isSmallScreenWidth && styles.mb3]}>
<Button
Expand Down Expand Up @@ -129,7 +145,21 @@ function WorkspaceCategoriesPage({policyCategories, route}: WorkspaceCategoriesP
<View style={[styles.ph5, styles.pb5]}>
<Text style={[styles.textNormal, styles.colorMuted]}>{translate('workspace.categories.subtitle')}</Text>
</View>
{categoryList.length ? (
{isLoading && (
<ActivityIndicator
size={CONST.ACTIVITY_INDICATOR_SIZE.LARGE}
style={[styles.flex1]}
color={theme.spinner}
/>
)}
{categoryList.length === 0 && !isLoading && (
<WorkspaceEmptyStateSection
title={translate('workspace.categories.emptyCategories.title')}
icon={Illustrations.EmptyStateExpenses}
subtitle={translate('workspace.categories.emptyCategories.subtitle')}
/>
)}
{categoryList.length > 0 && (
<SelectionList
canSelectMultiple
sections={[{data: categoryList, indexOffset: 0, isDisabled: false}]}
Expand All @@ -141,12 +171,6 @@ function WorkspaceCategoriesPage({policyCategories, route}: WorkspaceCategoriesP
customListHeader={getCustomListHeader()}
listHeaderWrapperStyle={[styles.ph9, styles.pv3, styles.pb5]}
/>
) : (
<WorkspaceEmptyStateSection
title={translate('workspace.categories.emptyCategories.title')}
icon={Illustrations.EmptyStateExpenses}
subtitle={translate('workspace.categories.emptyCategories.subtitle')}
/>
)}
</ScreenWrapper>
</PaidPolicyAccessOrNotFoundWrapper>
Expand Down

0 comments on commit 7a56d16

Please sign in to comment.