diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 59ea33e0d722..4eb501b289e6 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 e9ba781f046f..65c5b1e5959f 100644 --- a/src/pages/tasks/NewTaskPage.js +++ b/src/pages/tasks/NewTaskPage.js @@ -85,6 +85,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