diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 3a7e29be09a3..2bde852d3813 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -649,23 +649,32 @@ function setAssigneeChatReport(chatReport: OnyxTypes.Report) { * If there is no existing chat, it creates an optimistic chat report * It also sets the shareDestination as that chat report if a share destination isn't already set */ -function setAssigneeValue(assigneeEmail: string, assigneeAccountID: number, shareDestination: string, isCurrentUser = false): OnyxEntry { - let chatReport: OnyxEntry = null; - +function setAssigneeValue( + assigneeEmail: string, + assigneeAccountID: number, + shareToReportID: string, + chatReport: OnyxEntry, + isCurrentUser = false, +): OnyxEntry { + let report = chatReport; if (!isCurrentUser) { - chatReport = ReportUtils.getChatByParticipants([assigneeAccountID]); - if (!chatReport) { - chatReport = ReportUtils.buildOptimisticChatReport([assigneeAccountID]); - chatReport.isOptimisticReport = true; + // Check for the chatReport by participants IDs + if (!report) { + report = ReportUtils.getChatByParticipants([assigneeAccountID]); + } + // If chat report is still not found we need to build new optimistic chat report + if (!report) { + report = ReportUtils.buildOptimisticChatReport([assigneeAccountID]); + report.isOptimisticReport = true; // When assigning a task to a new user, by default we share the task in their DM // However, the DM doesn't exist yet - and will be created optimistically once the task is created // We don't want to show the new DM yet, because if you select an assignee and then change the assignee, the previous DM will still be shown // So here, we create it optimistically to share it with the assignee, but we have to hide it until the task is created - if (chatReport) { - chatReport.isHidden = true; + if (report) { + report.isHidden = true; } - Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport); + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report); // If this is an optimistic report, we likely don't have their personal details yet so we set it here optimistically as well const optimisticPersonalDetailsListAction = { @@ -677,12 +686,12 @@ function setAssigneeValue(assigneeEmail: string, assigneeAccountID: number, shar Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[assigneeAccountID]: optimisticPersonalDetailsListAction}); } - setAssigneeChatReport(chatReport); + setAssigneeChatReport(report); // If there is no share destination set, automatically set it to the assignee chat report // This allows for a much quicker process when creating a new task and is likely the desired share destination most times - if (!shareDestination) { - setShareDestinationValue(chatReport?.reportID ?? ''); + if (!shareToReportID) { + setShareDestinationValue(report?.reportID ?? ''); } } @@ -692,7 +701,7 @@ function setAssigneeValue(assigneeEmail: string, assigneeAccountID: number, shar // When we're editing the assignee, we immediately call editTaskAssignee. Since setting the assignee is async, // the chatReport is not yet set when editTaskAssignee is called. So we return the chatReport here so that // editTaskAssignee can use it. - return chatReport; + return report; } /** @@ -706,14 +715,14 @@ function setParentReportID(parentReportID: string) { /** * Clears out the task info from the store and navigates to the NewTaskDetails page */ -function clearOutTaskInfoAndNavigate(reportID: string, accountID = 0) { +function clearOutTaskInfoAndNavigate(reportID: string, chatReport: OnyxEntry, accountID = 0) { clearOutTaskInfo(); if (reportID && reportID !== '0') { setParentReportID(reportID); } if (accountID > 0) { const accountLogin = allPersonalDetails?.[accountID]?.login ?? ''; - setAssigneeValue(accountLogin, accountID, reportID); + setAssigneeValue(accountLogin, accountID, reportID, chatReport); } Navigation.navigate(ROUTES.NEW_TASK_DETAILS); } diff --git a/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx b/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx index 4bef463a3c55..11a84c17f9ee 100644 --- a/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx +++ b/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx @@ -162,7 +162,7 @@ function AttachmentPickerWithMenuItems({ { icon: Expensicons.Task, text: translate('newTaskPage.assignTask'), - onSelected: () => Task.clearOutTaskInfoAndNavigate(reportID), + onSelected: () => Task.clearOutTaskInfoAndNavigate(reportID, report), }, ]; }, [report, reportID, translate]); diff --git a/src/pages/home/sidebar/SidebarScreen/FloatingActionButtonAndPopover.js b/src/pages/home/sidebar/SidebarScreen/FloatingActionButtonAndPopover.js index 24603de5679c..38532830d43b 100644 --- a/src/pages/home/sidebar/SidebarScreen/FloatingActionButtonAndPopover.js +++ b/src/pages/home/sidebar/SidebarScreen/FloatingActionButtonAndPopover.js @@ -178,7 +178,7 @@ function FloatingActionButtonAndPopover(props) { IOU.startMoneyRequest(CONST.IOU.TYPE.SEND, props.quickAction.chatReportID); return; case CONST.QUICK_ACTIONS.ASSIGN_TASK: - Task.clearOutTaskInfoAndNavigate(props.quickAction.chatReportID, _.get(props.quickAction, 'targetAccountID', 0)); + Task.clearOutTaskInfoAndNavigate(props.quickAction.chatReportID, quickActionReport, _.get(props.quickAction, 'targetAccountID', 0)); return; default: return ''; diff --git a/src/pages/tasks/TaskAssigneeSelectorModal.tsx b/src/pages/tasks/TaskAssigneeSelectorModal.tsx index 0ffb33b7590b..6685a1bf18da 100644 --- a/src/pages/tasks/TaskAssigneeSelectorModal.tsx +++ b/src/pages/tasks/TaskAssigneeSelectorModal.tsx @@ -180,6 +180,7 @@ function TaskAssigneeSelectorModal({reports, task}: TaskAssigneeSelectorModalPro option?.login ?? '', option?.accountID ?? -1, report.reportID, + report, OptionsListUtils.isCurrentUser({...option, accountID: option?.accountID ?? -1, login: option?.login ?? ''}), ); @@ -193,6 +194,7 @@ function TaskAssigneeSelectorModal({reports, task}: TaskAssigneeSelectorModalPro option?.login ?? '', option.accountID, task?.shareDestination ?? '', + report, OptionsListUtils.isCurrentUser({...option, accountID: option?.accountID ?? -1, login: option?.login ?? undefined}), ); Navigation.goBack(ROUTES.NEW_TASK);