diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index bda8bec4f666..f4e333fa003c 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -424,6 +424,23 @@ function setShareDestinationValue(shareDestination) { Onyx.merge(ONYXKEYS.TASK, {shareDestination}); } +/** + * Auto-assign participant when creating a task in a DM + * @param {String} reportID + */ + +function setAssigneeValueWithParentReportID(reportID) { + const report = ReportUtils.getReport(reportID); + const isDefault = !(ReportUtils.isChatRoom(report) || ReportUtils.isPolicyExpenseChat(report)); + const participants = lodashGet(report, 'participants', []); + const hasMultipleParticipants = participants.length > 1; + if (!isDefault || hasMultipleParticipants || report.parentReportID) { + return; + } + + Onyx.merge(ONYXKEYS.TASK, {assignee: participants[0]}); +} + /** * Sets the assignee value for the task and checks for an existing chat with the assignee * If there is no existing chat, it creates an optimistic chat report @@ -591,6 +608,7 @@ export { setTaskReport, setDetailsValue, setAssigneeValue, + setAssigneeValueWithParentReportID, setShareDestinationValue, clearOutTaskInfo, reopenTask, diff --git a/src/pages/tasks/NewTaskPage.js b/src/pages/tasks/NewTaskPage.js index ad936b900c1e..99fb6dbc5811 100644 --- a/src/pages/tasks/NewTaskPage.js +++ b/src/pages/tasks/NewTaskPage.js @@ -86,6 +86,12 @@ const NewTaskPage = (props) => { setAssignee(displayDetails); } + // If we don't have an assignee and we are creating a task from a report + // this allows us to auto assign a participant of the report. + if (!props.task.assignee && props.task.parentReportID) { + TaskUtils.setAssigneeValueWithParentReportID(props.task.parentReportID); + } + // We only set the parentReportID if we are creating a task from a report // this allows us to go ahead and set that report as the share destination // and disable the share destination selector