Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 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
75 changes: 56 additions & 19 deletions src/libs/actions/Policy/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,30 @@ import {goBackWhenEnableFeature} from '@libs/PolicyUtils';
import {pushTransactionViolationsOnyxData} from '@libs/ReportUtils';
import {getTagArrayFromName} from '@libs/TransactionUtils';
import type {PolicyTagList} from '@pages/workspace/tags/types';
import {completeTask} from '@userActions/Task';
import {getFinishOnboardingTaskOnyxData} from '@userActions/Task';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ImportedSpreadsheet, Policy, PolicyTag, PolicyTagLists, PolicyTags, RecentlyUsedTags, Report} from '@src/types/onyx';
import type {ImportedSpreadsheet, Policy, PolicyTag, PolicyTagLists, PolicyTags, RecentlyUsedTags, Report, ReportAction} 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';

/**
* Checks if a task report is incomplete (not approved)
*/
function isTaskIncomplete(taskReport: OnyxEntry<Report>): boolean {
return !!taskReport && (taskReport.stateNum !== CONST.REPORT.STATE_NUM.APPROVED || taskReport.statusNum !== CONST.REPORT.STATUS_NUM.APPROVED);
}
type CreatePolicyTagParams = {
policyData: PolicyData;
tagName: string;
setupTagsTaskReport: OnyxEntry<Report>;
setupTagsTaskParentReport: OnyxEntry<Report>;
isSetupTagsTaskParentReportArchived: boolean;
setupTagsHasOutstandingChildTask: boolean;
setupTagsParentReportAction: OnyxEntry<ReportAction>;
setupCategoriesAndTagsTaskReport: OnyxEntry<Report>;
setupCategoriesAndTagsTaskParentReport: OnyxEntry<Report>;
isSetupCategoriesAndTagsTaskParentReportArchived: boolean;
setupCategoriesAndTagsHasOutstandingChildTask: boolean;
setupCategoriesAndTagsParentReportAction: OnyxEntry<ReportAction>;
currentUserAccountID: number;
policyHasCustomCategories: boolean;
};

function openPolicyTagsPage(policyID: string) {
if (!policyID) {
Expand Down Expand Up @@ -120,13 +130,22 @@ function updateImportSpreadsheetData(tagsLength: number): OnyxData<typeof ONYXKE
return onyxData;
}

function createPolicyTag(
policyData: PolicyData,
tagName: string,
setupTagsTaskReport?: OnyxEntry<Report>,
setupCategoriesAndTagsTaskReport?: OnyxEntry<Report>,
policyHasCustomCategories?: boolean,
) {
function createPolicyTag({
policyData,
tagName,
setupTagsTaskReport,
setupTagsTaskParentReport,
isSetupTagsTaskParentReportArchived,
setupTagsHasOutstandingChildTask,
setupTagsParentReportAction,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived,
setupCategoriesAndTagsHasOutstandingChildTask,
setupCategoriesAndTagsParentReportAction,
currentUserAccountID,
policyHasCustomCategories,
}: CreatePolicyTagParams) {
const {policy, tags: policyTags} = policyData;
const policyID = policy?.id;
const policyTag = PolicyUtils.getTagLists(policyTags)?.at(0) ?? ({} as PolicyTagList);
Expand Down Expand Up @@ -194,12 +213,30 @@ function createPolicyTag(

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

if (isTaskIncomplete(setupTagsTaskReport)) {
completeTask(setupTagsTaskReport, false, false, undefined);
const isTaskForCurrentWorkspace = (taskReport: OnyxEntry<Report>) => !taskReport?.policyID || taskReport.policyID === policyID;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This always returns false because taskReport.policyID is _FAKE_

Image


// Complete the "Set up tags" onboarding task
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ CONSISTENCY-6 (docs)

The return value of getFinishOnboardingTaskOnyxData is not being used. This function returns OnyxData that must be merged into the onyxData object passed to the API call, otherwise the onboarding task completion will not work correctly.

Looking at the correct implementation in src/libs/actions/Policy/Category.ts (lines 89-99):

const finishOnboardingTaskData = getFinishOnboardingTaskOnyxData(
    setupCategoryTaskReport,
    setupCategoryTaskParentReport,
    isSetupCategoriesTaskParentReportArchived,
    currentUserAccountID,
    hasOutstandingChildTask,
    parentReportAction,
);
onyxData.optimisticData?.push(...(finishOnboardingTaskData.optimisticData ?? []));
onyxData.successData?.push(...(finishOnboardingTaskData.successData ?? []));
onyxData.failureData?.push(...(finishOnboardingTaskData.failureData ?? []));

The same pattern should be applied here for both task completions.


Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you resolve this comment?

if (setupTagsTaskReport && currentUserAccountID && isTaskForCurrentWorkspace(setupTagsTaskReport)) {
getFinishOnboardingTaskOnyxData(
setupTagsTaskReport,
setupTagsTaskParentReport,
isSetupTagsTaskParentReportArchived ?? false,
currentUserAccountID,
setupTagsHasOutstandingChildTask ?? false,
setupTagsParentReportAction,
);
}

if (isTaskIncomplete(setupCategoriesAndTagsTaskReport) && policyHasCustomCategories) {
completeTask(setupCategoriesAndTagsTaskReport, false, false, undefined);
// Complete the combined "Set up categories and tags" task only if categories already exist
if (setupCategoriesAndTagsTaskReport && policyHasCustomCategories && currentUserAccountID && isTaskForCurrentWorkspace(setupCategoriesAndTagsTaskReport)) {
getFinishOnboardingTaskOnyxData(
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived ?? false,
currentUserAccountID,
setupCategoriesAndTagsHasOutstandingChildTask ?? false,
setupCategoriesAndTagsParentReportAction,
);
}
}

Expand Down
91 changes: 57 additions & 34 deletions src/pages/workspace/tags/WorkspaceCreateTagPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useCallback} from 'react';
import React from 'react';
import {Keyboard} from 'react-native';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
Expand All @@ -7,9 +7,10 @@
import ScreenWrapper from '@components/ScreenWrapper';
import TextInput from '@components/TextInput';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useOnboardingTaskInformation from '@hooks/useOnboardingTaskInformation';
import useOnyx from '@hooks/useOnyx';

Check failure on line 13 in src/pages/workspace/tags/WorkspaceCreateTagPage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'useOnyx' is defined but never used

Check failure on line 13 in src/pages/workspace/tags/WorkspaceCreateTagPage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'useOnyx' is defined but never used

Check failure on line 13 in src/pages/workspace/tags/WorkspaceCreateTagPage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

'useOnyx' is defined but never used

Check failure on line 13 in src/pages/workspace/tags/WorkspaceCreateTagPage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

'useOnyx' is defined but never used
import usePolicyData from '@hooks/usePolicyData';
import useThemeStyles from '@hooks/useThemeStyles';
import {addErrorMessage} from '@libs/ErrorUtils';
Expand All @@ -31,51 +32,73 @@
| PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS_TAGS.SETTINGS_TAG_CREATE>;

function WorkspaceCreateTagPage({route}: WorkspaceCreateTagPageProps) {
const policyID = route.params.policyID;
const {policyID, backTo} = route.params;
const policyData = usePolicyData(policyID);
const {tags: policyTagLists, categories: policyCategories} = policyData;
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
const setupTagsTaskReportID = introSelected?.setupTags;
const [setupTagsTaskReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${setupTagsTaskReportID}`);
const {taskReport: setupCategoriesAndTagsTaskReport} = useOnboardingTaskInformation(CONST.ONBOARDING_TASK_TYPE.SETUP_CATEGORIES_AND_TAGS);
const styles = useThemeStyles();
const {translate} = useLocalize();
const {inputCallbackRef} = useAutoFocusInput();
const backTo = route.params.backTo;
const isQuickSettingsFlow = route.name === SCREENS.SETTINGS_TAGS.SETTINGS_TAG_CREATE;

const policyHasCustomCategories = hasCustomCategories(policyCategories);

const validate = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAG_FORM>) => {
const errors: FormInputErrors<typeof ONYXKEYS.FORMS.WORKSPACE_TAG_FORM> = {};
const tagName = escapeTagName(values.tagName.trim());
const {tags} = getTagList(policyTagLists, 0);
const validate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAG_FORM>) => {
const errors: FormInputErrors<typeof ONYXKEYS.FORMS.WORKSPACE_TAG_FORM> = {};
const tagName = escapeTagName(values.tagName.trim());
const {tags} = getTagList(policyTagLists, 0);

if (!isRequiredFulfilled(tagName)) {
errors.tagName = translate('workspace.tags.tagRequiredError');
} else if (tagName === '0') {
errors.tagName = translate('workspace.tags.invalidTagNameError');
} else if (tags?.[tagName]) {
errors.tagName = translate('workspace.tags.existingTagError');
} else if ([...tagName].length > CONST.API_TRANSACTION_TAG_MAX_LENGTH) {
// Uses the spread syntax to count the number of Unicode code points instead of the number of UTF-16 code units.
addErrorMessage(errors, 'tagName', translate('common.error.characterLimitExceedCounter', [...tagName].length, CONST.API_TRANSACTION_TAG_MAX_LENGTH));
}
if (!isRequiredFulfilled(tagName)) {
errors.tagName = translate('workspace.tags.tagRequiredError');
} else if (tagName === '0') {
errors.tagName = translate('workspace.tags.invalidTagNameError');
} else if (tags?.[tagName]) {
errors.tagName = translate('workspace.tags.existingTagError');
} else if ([...tagName].length > CONST.API_TRANSACTION_TAG_MAX_LENGTH) {
// Uses the spread syntax to count the number of Unicode code points instead of the number of UTF-16 code units.
addErrorMessage(errors, 'tagName', translate('common.error.characterLimitExceedCounter', [...tagName].length, CONST.API_TRANSACTION_TAG_MAX_LENGTH));
}

return errors;
},
[policyTagLists, translate],
);
return errors;
};

const createTag = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAG_FORM>) => {
createPolicyTag(policyData, values.tagName.trim(), setupTagsTaskReport, setupCategoriesAndTagsTaskReport, policyHasCustomCategories);
Keyboard.dismiss();
Navigation.goBack(isQuickSettingsFlow ? ROUTES.SETTINGS_TAGS_ROOT.getRoute(policyID, backTo) : undefined);
},
[policyID, policyData, isQuickSettingsFlow, backTo, setupTagsTaskReport, setupCategoriesAndTagsTaskReport, policyHasCustomCategories],
);
const currentUserPersonalDetails = useCurrentUserPersonalDetails();

const {
taskReport: setupTagsTaskReport,
taskParentReport: setupTagsTaskParentReport,
isOnboardingTaskParentReportArchived: isSetupTagsTaskParentReportArchived,
hasOutstandingChildTask: setupTagsHasOutstandingChildTask,
parentReportAction: setupTagsParentReportAction,
} = useOnboardingTaskInformation(CONST.ONBOARDING_TASK_TYPE.SETUP_TAGS);

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

const createTag = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAG_FORM>) => {
createPolicyTag({
policyData,
tagName: values.tagName.trim(),
setupTagsTaskReport,
setupTagsTaskParentReport,
isSetupTagsTaskParentReportArchived,
setupTagsHasOutstandingChildTask,
setupTagsParentReportAction,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived,
setupCategoriesAndTagsHasOutstandingChildTask,
setupCategoriesAndTagsParentReportAction,
currentUserAccountID: currentUserPersonalDetails.accountID,
policyHasCustomCategories,
});
Keyboard.dismiss();
Navigation.goBack(isQuickSettingsFlow ? ROUTES.SETTINGS_TAGS_ROOT.getRoute(policyID, backTo) : undefined);
};

return (
<AccessOrNotFoundWrapper
Expand Down
2 changes: 1 addition & 1 deletion src/types/onyx/IntroSelected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {OnboardingInvite} from '@src/CONST';
import type {OnboardingPurpose} from './index';

/** The tasks of IntroSelected model */
type IntroSelectedTask = 'viewTour' | 'createWorkspace' | 'setupCategories' | 'setupCategoriesAndTags';
type IntroSelectedTask = 'viewTour' | 'createWorkspace' | 'setupCategories' | 'setupTags' | 'setupCategoriesAndTags';

/** Model of onboarding */
type IntroSelected = {
Expand Down
Loading
Loading