@@ -44,7 +44,7 @@ type ShareDestination = {
4444} ;
4545
4646type 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 */
9687function 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 */
434430function 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
619621function 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 */
10461036function 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 }
0 commit comments