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
13 changes: 12 additions & 1 deletion src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {hasEnabledOptions} from '@libs/OptionsListUtils';
import {getPolicy, goBackWhenEnableFeature} from '@libs/PolicyUtils';
import {getAllPolicyReports} from '@libs/ReportUtils';
import {resolveEnableFeatureConflicts} from '@userActions/RequestConflictUtils';
import {getFinishOnboardingTaskOnyxData} from '@userActions/Task';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyCategories, PolicyCategory, RecentlyUsedCategories, Report} from '@src/types/onyx';
Expand Down Expand Up @@ -85,6 +86,14 @@ Onyx.connect({
callback: (val) => (allPolicyCategories = val),
});

function appendSetupCategoriesOnboardingData(onyxData: OnyxData) {
const finishOnboardingTaskData = getFinishOnboardingTaskOnyxData('setupCategories');
onyxData.optimisticData?.push(...(finishOnboardingTaskData.optimisticData ?? []));
onyxData.successData?.push(...(finishOnboardingTaskData.successData ?? []));
onyxData.failureData?.push(...(finishOnboardingTaskData.failureData ?? []));
return onyxData;
}

function buildOptimisticPolicyCategories(policyID: string, categories: readonly string[]) {
const optimisticCategoryMap = categories.reduce<Record<string, Partial<PolicyCategory>>>((acc, category) => {
acc[category] = {
Expand Down Expand Up @@ -359,6 +368,7 @@ function setWorkspaceCategoryEnabled(policyID: string, categoriesToUpdate: Recor
},
],
};
appendSetupCategoriesOnboardingData(onyxData);
if (shouldDisableRequiresCategory) {
onyxData.optimisticData?.push({
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -591,7 +601,7 @@ function removePolicyCategoryReceiptsRequired(policyID: string, categoryName: st

function createPolicyCategory(policyID: string, categoryName: string) {
const onyxData = buildOptimisticPolicyCategories(policyID, [categoryName]);

appendSetupCategoriesOnboardingData(onyxData);
const parameters = {
policyID,
categories: JSON.stringify([{name: categoryName}]),
Expand Down Expand Up @@ -999,6 +1009,7 @@ function deleteWorkspaceCategories(policyID: string, categoryNamesToDelete: stri
},
],
};
appendSetupCategoriesOnboardingData(onyxData);
if (shouldDisableRequiresCategory) {
onyxData.optimisticData?.push({
onyxMethod: Onyx.METHOD.MERGE,
Expand Down
26 changes: 24 additions & 2 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type * as OnyxTypes from '@src/types/onyx';
import type {Icon} from '@src/types/onyx/OnyxCommon';
import type {ReportActions} from '@src/types/onyx/ReportAction';
import type ReportAction from '@src/types/onyx/ReportAction';
import type {OnyxData} from '@src/types/onyx/Request';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import {getMostRecentReportID, navigateToConciergeChatAndDeleteReport, notifyNewAction} from './Report';

Expand Down Expand Up @@ -87,6 +88,12 @@ Onyx.connect({
},
});

let introSelected: OnyxEntry<OnyxTypes.IntroSelected> = {};
Onyx.connect({
key: ONYXKEYS.NVP_INTRO_SELECTED,
callback: (val) => (introSelected = val),
});

/**
* Clears out the task info from the store
*/
Expand Down Expand Up @@ -370,11 +377,11 @@ function getOutstandingChildTask(taskReport: OnyxEntry<OnyxTypes.Report>) {
/**
* Complete a task
*/
function completeTask(taskReport: OnyxEntry<OnyxTypes.Report>, reportIDFromAction?: string) {
function completeTask(taskReport: OnyxEntry<OnyxTypes.Report>, reportIDFromAction?: string): OnyxData {
const taskReportID = taskReport?.reportID ?? reportIDFromAction;

if (!taskReportID) {
return;
return {};
}

const message = `marked as complete`;
Expand Down Expand Up @@ -455,6 +462,7 @@ function completeTask(taskReport: OnyxEntry<OnyxTypes.Report>, reportIDFromActio

playSound(SOUNDS.SUCCESS);
API.write(WRITE_COMMANDS.COMPLETE_TASK, parameters, {optimisticData, successData, failureData});
return {optimisticData, successData, failureData};
}

/**
Expand Down Expand Up @@ -1266,6 +1274,19 @@ function clearTaskErrors(reportID: string | undefined) {
});
}

function getFinishOnboardingTaskOnyxData(taskName: keyof OnyxTypes.IntroSelected): OnyxData {

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.

@thelullabyy Would it be possible to make this into a pure function? The way it is now, it is accessing data from two globals:

  • introSelected
  • allReports

In my PR #60869 I am having to add one more due to the way this function was initially written:

  • reportNameValuePairs

This is not ideal because it means this function is making a lot of assumptions about data existing out in the global scope that it doesn't have very much control over.

Ideally, this method would accept the taskReport as an argument, and then no globals would need to be accessed.

Is this something that you can help me clean up?

const taskReportID = introSelected?.[taskName];
if (taskReportID) {
const taskReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`];
if (taskReport) {

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.

We should also check if the taskReport is not archived before marking it as completed.
More details:
#61006 (comment)

if (taskReport.stateNum !== CONST.REPORT.STATE_NUM.APPROVED || taskReport.statusNum !== CONST.REPORT.STATUS_NUM.APPROVED) {
return completeTask(taskReport);
}
}
}

return {};
}
function completeTestDriveTask() {
const onboardingReport = ReportUtils.getChatUsedForOnboarding();
if (!onboardingReport) {
Expand Down Expand Up @@ -1312,5 +1333,6 @@ export {
setNewOptimisticAssignee,
getNavigationUrlOnTaskDelete,
canActionTask,
getFinishOnboardingTaskOnyxData,
completeTestDriveTask,
};
3 changes: 3 additions & 0 deletions src/types/onyx/IntroSelected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type IntroSelected = {

/** Company size selected during onboarding */
companySize?: string;

/** Task reportID for 'setupCategories' type */
setupCategories?: string;
};

export default IntroSelected;