Skip to content
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
11 changes: 11 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11613,6 +11613,8 @@ function prepareOnboardingOnyxData({

let createWorkspaceTaskReportID;
let addExpenseApprovalsTaskReportID;
let setupTagsTaskReportID;
let setupCategoriesAndTagsTaskReportID;
const tasksData = onboardingMessage.tasks
.filter((task) => {
if (engagementChoice === CONST.ONBOARDING_CHOICES.MANAGE_TEAM) {
Expand Down Expand Up @@ -11694,6 +11696,12 @@ function prepareOnboardingOnyxData({
if (task.type === CONST.ONBOARDING_TASK_TYPE.ADD_EXPENSE_APPROVALS) {
addExpenseApprovalsTaskReportID = currentTask.reportID;
}
if (task.type === CONST.ONBOARDING_TASK_TYPE.SETUP_TAGS) {
setupTagsTaskReportID = currentTask.reportID;
}
if (task.type === CONST.ONBOARDING_TASK_TYPE.SETUP_CATEGORIES_AND_TAGS) {
setupCategoriesAndTagsTaskReportID = currentTask.reportID;
}

return {
task,
Expand Down Expand Up @@ -11901,6 +11909,8 @@ function prepareOnboardingOnyxData({
choice: engagementChoice,
createWorkspace: createWorkspaceTaskReportID,
addExpenseApprovals: addExpenseApprovalsTaskReportID,
setupTags: setupTagsTaskReportID,
setupCategoriesAndTags: setupCategoriesAndTagsTaskReportID,
},
},
);
Expand Down Expand Up @@ -11970,6 +11980,7 @@ function prepareOnboardingOnyxData({
choice: null,
createWorkspace: null,
addExpenseApprovals: null,
setupCategoriesAndTags: null,
},
},
);
Expand Down
110 changes: 90 additions & 20 deletions src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,40 @@ import type {ApprovalRule, ExpenseRule, MccGroup} from '@src/types/onyx/Policy';
import type {PolicyCategoryExpenseLimitType} from '@src/types/onyx/PolicyCategory';
import type {OnyxData} from '@src/types/onyx/Request';

type CreatePolicyCategoryParams = {
policyID: string;
categoryName: string;
isSetupCategoriesTaskParentReportArchived: boolean;
setupCategoryTaskReport: OnyxEntry<Report>;
setupCategoryTaskParentReport: OnyxEntry<Report>;
currentUserAccountID: number;
hasOutstandingChildTask: boolean;
parentReportAction: OnyxEntry<ReportAction>;
setupCategoriesAndTagsTaskReport?: OnyxEntry<Report>;
setupCategoriesAndTagsTaskParentReport?: OnyxEntry<Report>;
isSetupCategoriesAndTagsTaskParentReportArchived?: boolean;
setupCategoriesAndTagsHasOutstandingChildTask?: boolean;
setupCategoriesAndTagsParentReportAction?: OnyxEntry<ReportAction>;
policyHasTags?: boolean;
};

type SetWorkspaceCategoryEnabledParams = {
policyData: PolicyData;
categoriesToUpdate: Record<string, {name: string; enabled: boolean}>;
isSetupCategoriesTaskParentReportArchived: boolean;
setupCategoryTaskReport: OnyxEntry<Report>;
setupCategoryTaskParentReport: OnyxEntry<Report>;
currentUserAccountID: number;
hasOutstandingChildTask: boolean;
parentReportAction: OnyxEntry<ReportAction> | undefined;
setupCategoriesAndTagsTaskReport?: OnyxEntry<Report>;
setupCategoriesAndTagsTaskParentReport?: OnyxEntry<Report>;
isSetupCategoriesAndTagsTaskParentReportArchived?: boolean;
setupCategoriesAndTagsHasOutstandingChildTask?: boolean;
setupCategoriesAndTagsParentReportAction?: OnyxEntry<ReportAction>;
policyHasTags?: boolean;
};

function appendSetupCategoriesOnboardingData(
onyxData: OnyxData<
typeof ONYXKEYS.COLLECTION.POLICY_CATEGORIES | typeof ONYXKEYS.COLLECTION.POLICY_CATEGORIES_DRAFT | typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS
Expand Down Expand Up @@ -328,16 +362,22 @@ function getPolicyCategories(policyID: string) {
API.read(READ_COMMANDS.GET_POLICY_CATEGORIES, params);
}

function setWorkspaceCategoryEnabled(
policyData: PolicyData,
categoriesToUpdate: Record<string, {name: string; enabled: boolean}>,
isSetupCategoriesTaskParentReportArchived: boolean,
setupCategoryTaskReport: OnyxEntry<Report>,
setupCategoryTaskParentReport: OnyxEntry<Report>,
currentUserAccountID: number,
hasOutstandingChildTask: boolean,
parentReportAction: OnyxEntry<ReportAction> | undefined,
) {
function setWorkspaceCategoryEnabled({
policyData,
categoriesToUpdate,
isSetupCategoriesTaskParentReportArchived,
setupCategoryTaskReport,
setupCategoryTaskParentReport,
currentUserAccountID,
hasOutstandingChildTask,
parentReportAction,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived,
setupCategoriesAndTagsHasOutstandingChildTask,
setupCategoriesAndTagsParentReportAction,
policyHasTags,
}: SetWorkspaceCategoryEnabledParams) {
const policyID = policyData.policy?.id;
const policyCategoriesOptimisticData = {
...Object.keys(categoriesToUpdate).reduce<PolicyCategories>((acc, key) => {
Expand Down Expand Up @@ -414,6 +454,18 @@ function setWorkspaceCategoryEnabled(
parentReportAction,
);

if (setupCategoriesAndTagsTaskReport && policyHasTags) {
appendSetupCategoriesOnboardingData(
onyxData,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived ?? false,
currentUserAccountID,
setupCategoriesAndTagsHasOutstandingChildTask ?? false,
setupCategoriesAndTagsParentReportAction,
);
}

const parameters = {
policyID,
categories: JSON.stringify(Object.keys(categoriesToUpdate).map((key) => categoriesToUpdate[key])),
Expand Down Expand Up @@ -620,16 +672,22 @@ function removePolicyCategoryReceiptsRequired(policyData: PolicyData, categoryNa
API.write(WRITE_COMMANDS.REMOVE_POLICY_CATEGORY_RECEIPTS_REQUIRED, parameters, onyxData);
}

function createPolicyCategory(
policyID: string,
categoryName: string,
isSetupCategoriesTaskParentReportArchived: boolean,
setupCategoryTaskReport: OnyxEntry<Report>,
setupCategoryTaskParentReport: OnyxEntry<Report>,
currentUserAccountID: number,
hasOutstandingChildTask: boolean,
parentReportAction: OnyxEntry<ReportAction>,
) {
function createPolicyCategory({
policyID,
categoryName,
isSetupCategoriesTaskParentReportArchived,
setupCategoryTaskReport,
setupCategoryTaskParentReport,
currentUserAccountID,
hasOutstandingChildTask,
parentReportAction,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived,
setupCategoriesAndTagsHasOutstandingChildTask,
setupCategoriesAndTagsParentReportAction,
policyHasTags,
}: CreatePolicyCategoryParams) {
const onyxData = buildOptimisticPolicyCategories(policyID, [categoryName]);
appendSetupCategoriesOnboardingData(
onyxData,
Expand All @@ -640,6 +698,18 @@ function createPolicyCategory(
hasOutstandingChildTask,
parentReportAction,
);
// Complete the combined "Set up categories and tags" task only if tags already exist
if (setupCategoriesAndTagsTaskReport && policyHasTags) {
appendSetupCategoriesOnboardingData(
onyxData,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived ?? false,
currentUserAccountID,
setupCategoriesAndTagsHasOutstandingChildTask ?? false,
setupCategoriesAndTagsParentReportAction,
);
}
const parameters = {
policyID,
categories: JSON.stringify([{name: categoryName}]),
Expand Down
25 changes: 23 additions & 2 deletions src/libs/actions/Policy/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
import {pushTransactionViolationsOnyxData} from '@libs/ReportUtils';
import {getTagArrayFromName} from '@libs/TransactionUtils';
import type {PolicyTagList} from '@pages/workspace/tags/types';
import {completeTask} from '@userActions/Task';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ImportedSpreadsheet, Policy, PolicyTag, PolicyTagLists, PolicyTags, RecentlyUsedTags} from '@src/types/onyx';
import type {ImportedSpreadsheet, Policy, PolicyTag, PolicyTagLists, PolicyTags, RecentlyUsedTags, Report} from '@src/types/onyx';
import type {OnyxValueWithOfflineFeedback} from '@src/types/onyx/OnyxCommon';
import type {ApprovalRule} from '@src/types/onyx/Policy';
import type {OnyxData} from '@src/types/onyx/Request';

let allPolicyTags: OnyxCollection<PolicyTagLists> = {};
Onyx.connect({

Check warning on line 41 in src/libs/actions/Policy/Tag.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY_TAGS,
waitForCollectionCallback: true,
callback: (value) => {
Expand Down Expand Up @@ -126,7 +127,14 @@
return onyxData;
}

function createPolicyTag(policyID: string, tagName: string, policyTags: PolicyTagLists = {}) {
function createPolicyTag(
policyID: string,
tagName: string,
policyTags: PolicyTagLists = {},
setupTagsTaskReport?: OnyxEntry<Report>,
setupCategoriesAndTagsTaskReport?: OnyxEntry<Report>,
policyHasCustomCategories?: boolean,
) {
const policyTag = PolicyUtils.getTagLists(policyTags)?.at(0) ?? ({} as PolicyTagList);
const newTagName = PolicyUtils.escapeTagName(tagName);

Expand Down Expand Up @@ -188,6 +196,19 @@
};

API.write(WRITE_COMMANDS.CREATE_POLICY_TAG, parameters, onyxData);

if (setupTagsTaskReport && (setupTagsTaskReport.stateNum !== CONST.REPORT.STATE_NUM.APPROVED || setupTagsTaskReport.statusNum !== CONST.REPORT.STATUS_NUM.APPROVED)) {
completeTask(setupTagsTaskReport, false, false, undefined);
}

// Complete the combined "Set up categories and tags" task only if categories already exist
if (
setupCategoriesAndTagsTaskReport &&
policyHasCustomCategories &&
(setupCategoriesAndTagsTaskReport.stateNum !== CONST.REPORT.STATE_NUM.APPROVED || setupCategoriesAndTagsTaskReport.statusNum !== CONST.REPORT.STATUS_NUM.APPROVED)
) {
completeTask(setupCategoriesAndTagsTaskReport, false, false, undefined);
}
}

function importPolicyTags(policyID: string, tags: PolicyTag[]) {
Expand Down
72 changes: 57 additions & 15 deletions src/pages/workspace/categories/CategorySettingsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {useIsFocused} from '@react-navigation/native';
import React, {useEffect, useMemo, useState} from 'react';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import {Trashcan} from '@components/Icon/Expensicons';

Check warning on line 6 in src/pages/workspace/categories/CategorySettingsPage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'@components/Icon/Expensicons' import is restricted from being used by a pattern. Direct imports from Icon/Expensicons are deprecated. Please use lazy loading hooks instead. Use `useMemoizedLazyExpensifyIcons` from @hooks/useLazyAsset. See docs/LAZY_ICONS_AND_ILLUSTRATIONS.md for details

Check warning on line 6 in src/pages/workspace/categories/CategorySettingsPage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'@components/Icon/Expensicons' import is restricted from being used. Direct imports from @components/Icon/Expensicons are deprecated. Please use lazy loading hooks instead. Use `useMemoizedLazyExpensifyIcons` from @hooks/useLazyAsset. See docs/LAZY_ICONS_AND_ILLUSTRATIONS.md for details
import MenuItem from '@components/MenuItem';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
Expand All @@ -15,6 +15,7 @@
import useEnvironment from '@hooks/useEnvironment';
import useLocalize from '@hooks/useLocalize';
import useOnboardingTaskInformation from '@hooks/useOnboardingTaskInformation';
import useOnyx from '@hooks/useOnyx';
import usePolicyData from '@hooks/usePolicyData';
import useThemeStyles from '@hooks/useThemeStyles';
import {formatRequiredFieldsTitle} from '@libs/AttendeeUtils';
Expand All @@ -25,12 +26,13 @@
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import {isDisablingOrDeletingLastEnabledCategory} from '@libs/OptionsListUtils';
import {getPersonalDetailByEmail} from '@libs/PersonalDetailsUtils';
import {getWorkflowApprovalsUnavailable, isControlPolicy} from '@libs/PolicyUtils';
import {getTagLists, getWorkflowApprovalsUnavailable, isControlPolicy} from '@libs/PolicyUtils';
import type {SettingsNavigatorParamList} from '@navigation/types';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import {clearCategoryErrors, deleteWorkspaceCategories, setWorkspaceCategoryEnabled} from '@userActions/Policy/Category';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';

Expand Down Expand Up @@ -70,6 +72,21 @@
parentReportAction,
} = useOnboardingTaskInformation(CONST.ONBOARDING_TASK_TYPE.SETUP_CATEGORIES);

const {
taskReport: setupCategoriesAndTagsTaskReport,
taskParentReport: setupCategoriesAndTagsTaskParentReport,
isOnboardingTaskParentReportArchived: isSetupCategoriesAndTagsTaskParentReportArchived,
hasOutstandingChildTask: setupCategoriesAndTagsHasOutstandingChildTask,
parentReportAction: setupCategoriesAndTagsParentReportAction,
} = useOnboardingTaskInformation(CONST.ONBOARDING_TASK_TYPE.SETUP_CATEGORIES_AND_TAGS);

const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, {canBeMissing: true});

const policyHasTags = useMemo(() => {
const tagLists = getTagLists(policyTags);
return tagLists.some((tagList) => Object.keys(tagList.tags ?? {}).length > 0);
}, [policyTags]);

const navigateBack = () => {
Navigation.goBack(isQuickSettingsFlow ? ROUTES.SETTINGS_CATEGORIES_ROOT.getRoute(policyID, backTo) : undefined);
};
Expand Down Expand Up @@ -138,26 +155,47 @@
return policyCategory?.pendingFields?.areCommentsRequired;
}, [policyCategory?.pendingFields, policy?.isAttendeeTrackingEnabled]);

if (!policyCategory) {
return <NotFoundPage />;
}

const updateWorkspaceCategoryEnabled = (value: boolean) => {
if (shouldPreventDisableOrDelete) {
setIsCannotDeleteOrDisableLastCategoryModalVisible(true);
return;
}
setWorkspaceCategoryEnabled(
const updateWorkspaceCategoryEnabled = useCallback(
(value: boolean) => {
if (shouldPreventDisableOrDelete) {
setIsCannotDeleteOrDisableLastCategoryModalVisible(true);
return;
}
setWorkspaceCategoryEnabled({
policyData,
categoriesToUpdate: {[policyCategory.name]: {name: policyCategory.name, enabled: value}},
isSetupCategoriesTaskParentReportArchived: isSetupCategoryTaskParentReportArchived,
setupCategoryTaskReport,
setupCategoryTaskParentReport,
currentUserAccountID: currentUserPersonalDetails.accountID,
hasOutstandingChildTask,
parentReportAction,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived,
setupCategoriesAndTagsHasOutstandingChildTask,
setupCategoriesAndTagsParentReportAction,
policyHasTags,
});
},
[
shouldPreventDisableOrDelete,
policyData,
{[policyCategory.name]: {name: policyCategory.name, enabled: value}},
policyCategory.name,
isSetupCategoryTaskParentReportArchived,
setupCategoryTaskReport,
setupCategoryTaskParentReport,
currentUserPersonalDetails.accountID,
hasOutstandingChildTask,
parentReportAction,
);
};
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived,
setupCategoriesAndTagsHasOutstandingChildTask,
setupCategoriesAndTagsParentReportAction,
policyHasTags,
],
);

const navigateToEditCategory = () => {
Navigation.navigate(
Expand All @@ -184,6 +222,10 @@
const workflowApprovalsUnavailable = getWorkflowApprovalsUnavailable(policy);
const approverDisabled = !policy?.areWorkflowsEnabled || workflowApprovalsUnavailable;

if (!policyCategory) {
return <NotFoundPage />;
}

return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
Expand Down
Loading
Loading