Skip to content

Commit d264462

Browse files
authored
Merge pull request Expensify#76703 from DylanDylann/refactor-54
Remove Onyx.connect() for the key: ONYXKEYS.COLLECTION.REPORT in src/libs/actions/Task.ts
2 parents 2d63fcd + 75f772b commit d264462

13 files changed

Lines changed: 69 additions & 67 deletions

File tree

src/components/ReportActionItem/TaskPreview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ function TaskPreview({
148148
disabled={!isTaskActionable}
149149
onPress={callFunctionIfActionIsAllowed(() => {
150150
if (isTaskCompleted) {
151-
reopenTask(taskReport, currentUserPersonalDetails.accountID, taskReportID);
151+
reopenTask(taskReport, parentReport, currentUserPersonalDetails.accountID, taskReportID);
152152
} else {
153-
completeTask(taskReport, hasOutstandingChildTask, parentReportAction, taskReportID);
153+
completeTask(taskReport, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction, taskReportID);
154154
}
155155
})}
156156
accessibilityLabel={translate('task.task')}

src/components/ReportActionItem/TaskView.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function TaskView({report, parentReport, action}: TaskViewProps) {
9696
<OfflineWithFeedback
9797
shouldShowErrorMessages
9898
errors={report?.errorFields?.editTask ?? report?.errorFields?.createTask}
99-
onClose={() => clearTaskErrors(report?.reportID)}
99+
onClose={() => clearTaskErrors(report)}
100100
errorRowStyles={styles.ph5}
101101
>
102102
<Hoverable>
@@ -132,9 +132,9 @@ function TaskView({report, parentReport, action}: TaskViewProps) {
132132
return;
133133
}
134134
if (isCompleted) {
135-
reopenTask(report, currentUserPersonalDetails.accountID);
135+
reopenTask(report, parentReport, currentUserPersonalDetails.accountID);
136136
} else {
137-
completeTask(report, hasOutstandingChildTask, parentReportAction);
137+
completeTask(report, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction);
138138
}
139139
})}
140140
isChecked={isCompleted}

src/components/SelectionListWithSections/Search/TaskListItemRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function ActionCell({taskItem, isLargeScreenWidth}: TaskCellProps) {
110110
style={[styles.w100]}
111111
isDisabled={!isTaskActionable}
112112
onPress={callFunctionIfActionIsAllowed(() => {
113-
completeTask(taskItem, hasOutstandingChildTask, parentReportAction, taskItem.reportID);
113+
completeTask(taskItem, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction, taskItem.reportID);
114114
})}
115115
/>
116116
);

src/components/TaskHeaderActionButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ function TaskHeaderActionButton({report}: TaskHeaderActionButtonProps) {
4545
return;
4646
}
4747
if (isCompletedTaskReport(report)) {
48-
reopenTask(report, currentUserPersonalDetails.accountID);
48+
reopenTask(report, parentReport, currentUserPersonalDetails.accountID);
4949
} else {
50-
completeTask(report, hasOutstandingChildTask, parentReportAction);
50+
completeTask(report, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction);
5151
}
5252
})}
5353
style={styles.flex1}

src/libs/actions/Policy/Policy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2507,7 +2507,7 @@ function buildPolicyData(options: BuildPolicyDataOptions = {}) {
25072507
optimisticData: optimisticCreateWorkspaceTaskData,
25082508
successData: successCreateWorkspaceTaskData,
25092509
failureData: failureCreateWorkspaceTaskData,
2510-
} = buildTaskData(createWorkspaceTaskReport, deprecatedIntroSelected.createWorkspace, false, undefined);
2510+
} = buildTaskData(createWorkspaceTaskReport, deprecatedIntroSelected.createWorkspace, false, false, undefined);
25112511
optimisticData.push(...optimisticCreateWorkspaceTaskData);
25122512
successData.push(...successCreateWorkspaceTaskData);
25132513
failureData.push(...failureCreateWorkspaceTaskData);

src/libs/actions/Task.ts

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type ShareDestination = {
4444
};
4545

4646
type CreateTaskAndNavigateParams = {
47-
parentReportID: string | undefined;
47+
parentReport: OnyxEntry<OnyxTypes.Report>;
4848
title: string;
4949
description: string;
5050
assigneeEmail: string;
@@ -58,15 +58,6 @@ type CreateTaskAndNavigateParams = {
5858
ancestors?: ReportUtils.Ancestor[];
5959
};
6060

61-
let allReports: OnyxCollection<OnyxTypes.Report>;
62-
Onyx.connect({
63-
key: ONYXKEYS.COLLECTION.REPORT,
64-
waitForCollectionCallback: true,
65-
callback: (value) => {
66-
allReports = value;
67-
},
68-
});
69-
7061
/**
7162
* Clears out the task info from the store
7263
*/
@@ -95,7 +86,7 @@ function clearOutTaskInfo(skipConfirmation = false) {
9586
*/
9687
function createTaskAndNavigate(params: CreateTaskAndNavigateParams) {
9788
const {
98-
parentReportID,
89+
parentReport,
9990
title,
10091
description,
10192
assigneeEmail,
@@ -108,6 +99,7 @@ function createTaskAndNavigate(params: CreateTaskAndNavigateParams) {
10899
quickAction = {},
109100
ancestors = [],
110101
} = params;
102+
const parentReportID = parentReport?.reportID;
111103
if (!parentReportID) {
112104
return;
113105
}
@@ -125,7 +117,6 @@ function createTaskAndNavigate(params: CreateTaskAndNavigateParams) {
125117

126118
const currentTime = NetworkConnection.getDBTimeWithSkew();
127119
const lastCommentText = ReportUtils.formatReportLastMessageText(ReportActionsUtils.getReportActionText(optimisticAddCommentReport.reportAction));
128-
const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`];
129120
const optimisticParentReport = {
130121
lastVisibleActionCreated: optimisticAddCommentReport.reportAction.created,
131122
lastMessageText: lastCommentText,
@@ -325,10 +316,15 @@ function createTaskAndNavigate(params: CreateTaskAndNavigateParams) {
325316
notifyNewAction(parentReportID, currentUserAccountID, optimisticAddCommentReport.reportAction);
326317
}
327318

328-
function buildTaskData(taskReport: OnyxEntry<OnyxTypes.Report>, taskReportID: string, hasOutstandingChildTask: boolean, parentReportAction: OnyxEntry<ReportAction> | undefined) {
319+
function buildTaskData(
320+
taskReport: OnyxEntry<OnyxTypes.Report>,
321+
taskReportID: string,
322+
hasOutstandingChildTaskInParentReport: boolean,
323+
hasOutstandingChildTask: boolean,
324+
parentReportAction: OnyxEntry<ReportAction> | undefined,
325+
) {
329326
const message = `marked as complete`;
330327
const completedTaskReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASK_COMPLETED, message);
331-
const parentReport = getParentReport(taskReport);
332328
const optimisticData: OnyxUpdate[] = [
333329
{
334330
onyxMethod: Onyx.METHOD.MERGE,
@@ -382,7 +378,7 @@ function buildTaskData(taskReport: OnyxEntry<OnyxTypes.Report>, taskReportID: st
382378
if (parentReportAction) {
383379
optimisticData.push({
384380
onyxMethod: Onyx.METHOD.MERGE,
385-
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReport?.reportID}`,
381+
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReport?.parentReportID}`,
386382
value: {
387383
[parentReportAction.reportActionID]: {
388384
childStateNum: CONST.REPORT.STATE_NUM.APPROVED,
@@ -393,7 +389,7 @@ function buildTaskData(taskReport: OnyxEntry<OnyxTypes.Report>, taskReportID: st
393389

394390
failureData.push({
395391
onyxMethod: Onyx.METHOD.MERGE,
396-
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReport?.reportID}`,
392+
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReport?.parentReportID}`,
397393
value: {
398394
[parentReportAction.reportActionID]: {
399395
childStateNum: CONST.REPORT.STATE_NUM.OPEN,
@@ -403,7 +399,7 @@ function buildTaskData(taskReport: OnyxEntry<OnyxTypes.Report>, taskReportID: st
403399
});
404400
}
405401

406-
if (parentReport?.hasOutstandingChildTask) {
402+
if (hasOutstandingChildTaskInParentReport) {
407403
optimisticData.push({
408404
onyxMethod: Onyx.METHOD.MERGE,
409405
key: `${ONYXKEYS.COLLECTION.REPORT}${taskReport?.parentReportID}`,
@@ -415,7 +411,7 @@ function buildTaskData(taskReport: OnyxEntry<OnyxTypes.Report>, taskReportID: st
415411
onyxMethod: Onyx.METHOD.MERGE,
416412
key: `${ONYXKEYS.COLLECTION.REPORT}${taskReport?.parentReportID}`,
417413
value: {
418-
hasOutstandingChildTask: parentReport?.hasOutstandingChildTask,
414+
hasOutstandingChildTask: hasOutstandingChildTaskInParentReport,
419415
},
420416
});
421417
}
@@ -433,6 +429,7 @@ function buildTaskData(taskReport: OnyxEntry<OnyxTypes.Report>, taskReportID: st
433429
*/
434430
function completeTask(
435431
taskReport: OnyxEntry<OnyxTypes.Report>,
432+
hasOutstandingChildTaskInParentReport: boolean,
436433
hasOutstandingChildTask: boolean,
437434
parentReportAction: OnyxEntry<ReportAction> | undefined,
438435
reportIDFromAction?: string,
@@ -443,7 +440,13 @@ function completeTask(
443440
return {};
444441
}
445442

446-
const {optimisticData, successData, failureData, parameters} = buildTaskData(taskReport, taskReportID, hasOutstandingChildTask, parentReportAction);
443+
const {optimisticData, successData, failureData, parameters} = buildTaskData(
444+
taskReport,
445+
taskReportID,
446+
hasOutstandingChildTaskInParentReport,
447+
hasOutstandingChildTask,
448+
parentReportAction,
449+
);
447450

448451
playSound(SOUNDS.SUCCESS);
449452
API.write(WRITE_COMMANDS.COMPLETE_TASK, parameters, {optimisticData, successData, failureData});
@@ -453,14 +456,13 @@ function completeTask(
453456
/**
454457
* Reopen a closed task
455458
*/
456-
function reopenTask(taskReport: OnyxEntry<OnyxTypes.Report>, currentUserAccountID: number, reportIDFromAction?: string) {
459+
function reopenTask(taskReport: OnyxEntry<OnyxTypes.Report>, parentReport: OnyxEntry<OnyxTypes.Report>, currentUserAccountID: number, reportIDFromAction?: string) {
457460
const taskReportID = taskReport?.reportID ?? reportIDFromAction;
458461
if (!taskReportID) {
459462
return;
460463
}
461464
const message = `marked as incomplete`;
462465
const reopenedTaskReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASK_REOPENED, message);
463-
const parentReport = getParentReport(taskReport);
464466
const hasOutstandingChildTask = taskReport?.managerID === currentUserAccountID ? true : parentReport?.hasOutstandingChildTask;
465467

466468
const optimisticData: OnyxUpdate[] = [
@@ -618,6 +620,7 @@ function editTask(report: OnyxTypes.Report, {title, description}: OnyxTypes.Task
618620

619621
function editTaskAssignee(
620622
report: OnyxTypes.Report,
623+
parentReport: OnyxEntry<OnyxTypes.Report>,
621624
sessionAccountID: number,
622625
assigneeEmail: string,
623626
currentUserAccountID: number,
@@ -633,7 +636,6 @@ function editTaskAssignee(
633636
let assigneeChatReportOnyxData;
634637
const assigneeChatReportID = assigneeChatReport?.reportID;
635638
const assigneeChatReportMetadata = ReportUtils.getReportMetadata(assigneeChatReportID);
636-
const parentReport = getParentReport(report);
637639
const taskOwnerAccountID = report?.ownerAccountID;
638640
const optimisticReport: OptimisticReport = {
639641
reportName,
@@ -1000,16 +1002,6 @@ function getShareDestination(
10001002
};
10011003
}
10021004

1003-
/**
1004-
* Returns the parentReport if the given report is a thread
1005-
*/
1006-
function getParentReport(report: OnyxEntry<OnyxTypes.Report>): OnyxEntry<OnyxTypes.Report> {
1007-
if (!report?.parentReportID) {
1008-
return undefined;
1009-
}
1010-
return allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report.parentReportID}`];
1011-
}
1012-
10131005
/**
10141006
* Calculate the URL to navigate to after a task deletion
10151007
* @param report - The task report being deleted
@@ -1025,10 +1017,8 @@ function getNavigationUrlOnTaskDelete(report: OnyxEntry<OnyxTypes.Report>): stri
10251017
return undefined;
10261018
}
10271019

1028-
// First try to navigate to parent report
1029-
const parentReport = getParentReport(report);
1030-
if (parentReport?.reportID) {
1031-
return ROUTES.REPORT_WITH_ID.getRoute(parentReport.reportID);
1020+
if (report?.parentReportID) {
1021+
return ROUTES.REPORT_WITH_ID.getRoute(report.parentReportID);
10321022
}
10331023

10341024
// If no parent report, try to navigate to most recent report
@@ -1045,6 +1035,7 @@ function getNavigationUrlOnTaskDelete(report: OnyxEntry<OnyxTypes.Report>): stri
10451035
*/
10461036
function deleteTask(
10471037
report: OnyxEntry<OnyxTypes.Report>,
1038+
parentReport: OnyxEntry<OnyxTypes.Report>,
10481039
isReportArchived: boolean,
10491040
currentUserAccountID: number,
10501041
hasOutstandingChildTask: boolean,
@@ -1057,7 +1048,6 @@ function deleteTask(
10571048
const message = `deleted task: ${report.reportName}`;
10581049
const optimisticCancelReportAction = ReportUtils.buildOptimisticTaskReportAction(report.reportID, CONST.REPORT.ACTIONS.TYPE.TASK_CANCELLED, message);
10591050
const optimisticReportActionID = optimisticCancelReportAction.reportActionID;
1060-
const parentReport = getParentReport(report);
10611051
const canUserPerformWriteAction = ReportUtils.canUserPerformWriteAction(report, isReportArchived);
10621052

10631053
// If the task report is the last visible action in the parent report, we should navigate back to the parent report
@@ -1265,13 +1255,12 @@ function canActionTask(
12651255
return sessionAccountID === taskReport?.ownerAccountID || sessionAccountID === getTaskAssigneeAccountID(taskReport, parentReportAction);
12661256
}
12671257

1268-
function clearTaskErrors(reportID: string | undefined) {
1258+
function clearTaskErrors(report: OnyxEntry<OnyxTypes.Report>) {
1259+
const reportID = report?.reportID;
12691260
if (!reportID) {
12701261
return;
12711262
}
12721263

1273-
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
1274-
12751264
// Delete the task preview in the parent report
12761265
if (report?.pendingFields?.createChat === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
12771266
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, report.parentReportActionID ? {[report.parentReportActionID]: null} : {});
@@ -1297,7 +1286,7 @@ function getFinishOnboardingTaskOnyxData(
12971286
if (taskReport && canActionTask(taskReport, parentReportAction, currentUserAccountID, taskParentReport, isParentReportArchived)) {
12981287
if (taskReport) {
12991288
if (taskReport.stateNum !== CONST.REPORT.STATE_NUM.APPROVED || taskReport.statusNum !== CONST.REPORT.STATUS_NUM.APPROVED) {
1300-
return completeTask(taskReport, hasOutstandingChildTask, parentReportAction);
1289+
return completeTask(taskReport, taskParentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction);
13011290
}
13021291
}
13031292
}

src/pages/ReportDetailsPage.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
490490
isAnonymousAction: false,
491491
action: callFunctionIfActionIsAllowed(() => {
492492
Navigation.goBack(backTo);
493-
reopenTask(report, currentUserPersonalDetails?.accountID);
493+
reopenTask(report, parentReport, currentUserPersonalDetails?.accountID);
494494
}),
495495
});
496496
}
@@ -581,6 +581,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
581581
isRestrictedToPreferredPolicy,
582582
preferredPolicyID,
583583
introSelected,
584+
parentReport,
584585
]);
585586

586587
const displayNamesWithTooltips = useMemo(() => {
@@ -819,7 +820,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
819820

820821
const deleteTransaction = useCallback(() => {
821822
if (caseID === CASES.DEFAULT) {
822-
deleteTask(report, isReportArchived, currentUserPersonalDetails.accountID, hasOutstandingChildTask, parentReportAction, ancestors);
823+
deleteTask(report, parentReport, isReportArchived, currentUserPersonalDetails.accountID, hasOutstandingChildTask, parentReportAction, ancestors);
823824
return;
824825
}
825826

@@ -868,6 +869,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
868869
isChatIOUReportArchived,
869870
hasOutstandingChildTask,
870871
parentReportAction,
872+
parentReport,
871873
]);
872874

873875
// Where to navigate back to after deleting the transaction and its report.

src/pages/home/HeaderView.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,15 @@ function HeaderView({report, parentReportAction, onNavigationMenuButtonClicked,
376376
isVisible={isDeleteTaskConfirmModalVisible}
377377
onConfirm={() => {
378378
setIsDeleteTaskConfirmModalVisible(false);
379-
deleteTask(report, isReportArchived, currentUserPersonalDetails.accountID, hasOutstandingChildTask, parentReportAction ?? undefined, ancestors);
379+
deleteTask(
380+
report,
381+
parentReport,
382+
isReportArchived,
383+
currentUserPersonalDetails.accountID,
384+
hasOutstandingChildTask,
385+
parentReportAction ?? undefined,
386+
ancestors,
387+
);
380388
}}
381389
onCancel={() => setIsDeleteTaskConfirmModalVisible(false)}
382390
title={translate('task.deleteTask')}

src/pages/home/report/ReportFooter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function ReportFooter({
162162
}
163163
}
164164
createTaskAndNavigate({
165-
parentReportID: report.reportID,
165+
parentReport: report,
166166
title,
167167
description: '',
168168
assigneeEmail: assignee?.login ?? '',
@@ -177,7 +177,7 @@ function ReportFooter({
177177
});
178178
return true;
179179
},
180-
[allPersonalDetails, ancestors, availableLoginsList, currentUserEmail, personalDetail.accountID, quickAction, report.policyID, report.reportID],
180+
[allPersonalDetails, ancestors, availableLoginsList, currentUserEmail, personalDetail.accountID, quickAction, report],
181181
);
182182

183183
const [targetReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID ?? report.reportID}`, {canBeMissing: true});

src/pages/tasks/NewTaskDetailsPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function NewTaskDetailsPage({route}: NewTaskDetailsPageProps) {
7676
if (skipConfirmation) {
7777
setShareDestinationValue(task?.parentReportID);
7878
createTaskAndNavigate({
79-
parentReportID: task?.parentReportID,
79+
parentReport,
8080
title: values.taskTitle,
8181
description: values.taskDescription ?? '',
8282
assigneeEmail: task?.assignee ?? '',

0 commit comments

Comments
 (0)