From 0ce8bec4ac1efcd980cfd2c392a438a9804f48b6 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Wed, 12 Jul 2023 12:51:36 -0700 Subject: [PATCH 01/34] Update the comments / remove the call to openReport when we select an assignee --- src/libs/actions/Task.js | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index bc1d4d6c3536..6e5b6be00144 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -32,9 +32,20 @@ function clearOutTaskInfo() { } /** - * Assign a task to a user - * Function title is createTask for consistency with the rest of the actions - * and also because we can create a task without assigning it to anyone + * A task needs two things to be created - a title and a parent report + * When you create a task report, there are a few things that happen: + * A task report is created, along with a CreatedReportAction for that new task report + * A reportAction indicating that a task was created is added to the parent report (share destination) + * If you assign the task to someone, a reportAction is created in the chat between you and the assignee to inform them of the task + * + * So you have the following optimistic items potentially being created: + * 1. The task report + * 1a. The CreatedReportAction for the task report + * 2. The TaskReportAction on the parent report + * 3. The chat report between you and the assignee + * 3a. The CreatedReportAction for the assignee chat report + * 3b. The TaskReportAction on the assignee chat report + * * @param {String} parentReportID * @param {String} title * @param {String} description @@ -43,13 +54,14 @@ function clearOutTaskInfo() { * */ -function createTaskAndNavigate(parentReportID, title, description, assignee, assigneeAccountID = 0) { - // Create the task report +function createTaskAndNavigate(parentReportID, title, description, assignee, assigneeAccountID = 0, assigneeChatReport = {}) { + const optimisticTaskReport = ReportUtils.buildOptimisticTaskReport(currentUserEmail, currentUserAccountID, assigneeAccountID, parentReportID, title, description); // Grab the assigneeChatReportID if there is an assignee and if it's not the same as the parentReportID // then we create an optimistic add comment report action on the assignee's chat to notify them of the task - const assigneeChatReportID = lodashGet(ReportUtils.getChatByParticipants([assigneeAccountID]), 'reportID'); + // You can share a task to various locations. If you're choosing to share it in the same DM as the assignee + const assigneeChatReportID = assigneeChatReport.reportID; const taskReportID = optimisticTaskReport.reportID; let optimisticAssigneeAddComment; if (assigneeChatReportID && assigneeChatReportID !== parentReportID) { @@ -464,6 +476,14 @@ function setShareDestinationValue(shareDestination) { Onyx.merge(ONYXKEYS.TASK, {shareDestination}); } +/** + * Sets the assigneeChatReport details for the task + * @param {Object} chatReport + */ +function setAssigneeChatReport(chatReport) { + Onyx.merge(ONYXKEYS.TASK, {assigneeChatReport: chatReport}); +} + /** * Auto-assign participant when creating a task in a DM * @param {String} reportID @@ -505,12 +525,11 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre newChat = ReportUtils.buildOptimisticChatReport([newAssigneeAccountID]); } const reportID = chat ? chat.reportID : newChat.reportID; + setAssigneeChatReport(chat || newChat); if (!shareDestination) { setShareDestinationValue(reportID); } - - Report.openReport(reportID, [assignee], newChat); } // This is only needed for creation of a new task and so it should only be stored locally From 159f0c4624b8ffa610c7ae6d22788732e2954bfe Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Wed, 12 Jul 2023 12:52:53 -0700 Subject: [PATCH 02/34] Update to chatReport --- src/libs/actions/Task.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 6e5b6be00144..49fe0c16e4d0 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -519,13 +519,12 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre newAssigneeAccountID = UserUtils.generateAccountID(); } if (!isCurrentUser) { - let newChat = {}; - const chat = ReportUtils.getChatByParticipants([newAssigneeAccountID]); - if (!chat) { - newChat = ReportUtils.buildOptimisticChatReport([newAssigneeAccountID]); + let chatReport = ReportUtils.getChatByParticipants([newAssigneeAccountID]); + if (!chatReport) { + chatReport = ReportUtils.buildOptimisticChatReport([newAssigneeAccountID]); } - const reportID = chat ? chat.reportID : newChat.reportID; - setAssigneeChatReport(chat || newChat); + const reportID = chatReport.reportID; + setAssigneeChatReport(chatReport); if (!shareDestination) { setShareDestinationValue(reportID); From 34f32dab123eb497966f8d3aa9738d10fb944252 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Wed, 12 Jul 2023 14:03:40 -0700 Subject: [PATCH 03/34] Clean up / add comments --- src/libs/actions/Task.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 49fe0c16e4d0..73c6b11614e5 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -55,16 +55,18 @@ function clearOutTaskInfo() { */ function createTaskAndNavigate(parentReportID, title, description, assignee, assigneeAccountID = 0, assigneeChatReport = {}) { - const optimisticTaskReport = ReportUtils.buildOptimisticTaskReport(currentUserEmail, currentUserAccountID, assigneeAccountID, parentReportID, title, description); - - // Grab the assigneeChatReportID if there is an assignee and if it's not the same as the parentReportID - // then we create an optimistic add comment report action on the assignee's chat to notify them of the task - // You can share a task to various locations. If you're choosing to share it in the same DM as the assignee + const optimisticTaskCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(optimisticTaskReport.reportID); + const assigneeChatReportID = assigneeChatReport.reportID; const taskReportID = optimisticTaskReport.reportID; let optimisticAssigneeAddComment; + + // If you're choosing to share the task in the same DM as the assignee then + // we don't need to create another reportAction indicating that you've been assigned + // Additionally, tasks can be unassigned in which case we don't need to create a reportAction either if (assigneeChatReportID && assigneeChatReportID !== parentReportID) { + // ReportAction indicating that the assignee has been assigned a task optimisticAssigneeAddComment = ReportUtils.buildOptimisticTaskCommentReportAction( taskReportID, title, @@ -75,16 +77,14 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass ); } - // Create the CreatedReportAction on the task - const optimisticTaskCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(optimisticTaskReport.reportID); + // Parent ReportAction indicating that a task has been created const optimisticAddCommentReport = ReportUtils.buildOptimisticTaskCommentReportAction(taskReportID, title, assignee, assigneeAccountID, `Created a task: ${title}`, parentReportID); optimisticTaskReport.parentReportActionID = optimisticAddCommentReport.reportAction.reportActionID; const currentTime = DateUtils.getDBTime(); - const lastCommentText = ReportUtils.formatReportLastMessageText(optimisticAddCommentReport.reportAction.message[0].text); - const optimisticReport = { + const optimisticParentReport = { lastVisibleActionCreated: currentTime, lastMessageText: lastCommentText, lastActorAccountID: currentUserAccountID, @@ -92,6 +92,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass }; const optimisticData = [ + // Task Report { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticTaskReport.reportID}`, @@ -103,20 +104,23 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass isOptimisticReport: true, }, }, + // Task Report Actions { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTaskReport.reportID}`, value: {[optimisticTaskCreatedAction.reportActionID]: optimisticTaskCreatedAction}, }, + // Parent Report { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, - value: {[optimisticAddCommentReport.reportAction.reportActionID]: optimisticAddCommentReport.reportAction}, + key: `${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, + value: optimisticParentReport, }, + // Parent Report Actions { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, - value: optimisticReport, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, + value: {[optimisticAddCommentReport.reportAction.reportActionID]: optimisticAddCommentReport.reportAction}, }, ]; @@ -522,6 +526,7 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre let chatReport = ReportUtils.getChatByParticipants([newAssigneeAccountID]); if (!chatReport) { chatReport = ReportUtils.buildOptimisticChatReport([newAssigneeAccountID]); + chatReport.isOptimisticReport = true; } const reportID = chatReport.reportID; setAssigneeChatReport(chatReport); From 420084cbd16a8a1c5676ad20e60117f6608cca7b Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Wed, 12 Jul 2023 14:12:32 -0700 Subject: [PATCH 04/34] add optimistic onyx data for optimistic chat with assignee --- src/libs/actions/Task.js | 65 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 73c6b11614e5..d098a05db958 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -165,17 +165,72 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass lastReadTime: currentTime, }; + // If we're optimistically creating a new chat report for the assignee then we need to create the report and reportAction + if (assigneeChatReport && assigneeChatReport.isOptimisticReport) { + const optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); + optimisticData.push( + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, + value: { + ...assigneeChatReport, + pendingFields: { + createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + ...optimisticAssigneeReport + } + }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, + value: {[optimisticChatCreatedReportAction.reportActionID]: optimisticChatCreatedReportAction}, + + } + ); + + successData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, + value: { + pendingFields: { + createChat: null, + }, + isOptimisticReport: false, + }, + } + ); + + failureData.push( + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, + value: null, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, + value: {[optimisticChatCreatedReportAction.reportActionID]: {pendingAction: null}}, + } + ); + + } else { + // If the assignee already has a chat report, we just need to update some fields + optimisticData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, + value: optimisticAssigneeReport, + }, + ); + } + optimisticData.push( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: optimisticAssigneeAddComment.reportAction}, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, - value: optimisticAssigneeReport, - }, ); failureData.push({ From b9c4c71f55b251d16a3476ed420a2574201a27ba Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Wed, 12 Jul 2023 14:18:40 -0700 Subject: [PATCH 05/34] Hook up CreateTask --- src/libs/actions/Task.js | 6 ++++-- src/pages/tasks/NewTaskPage.js | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index d098a05db958..4030384befd4 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -61,6 +61,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass const assigneeChatReportID = assigneeChatReport.reportID; const taskReportID = optimisticTaskReport.reportID; let optimisticAssigneeAddComment; + let optimisticChatCreatedReportAction; // If you're choosing to share the task in the same DM as the assignee then // we don't need to create another reportAction indicating that you've been assigned @@ -167,7 +168,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass // If we're optimistically creating a new chat report for the assignee then we need to create the report and reportAction if (assigneeChatReport && assigneeChatReport.isOptimisticReport) { - const optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); + optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); optimisticData.push( { onyxMethod: Onyx.METHOD.SET, @@ -247,13 +248,14 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass parentReportID, taskReportID: optimisticTaskReport.reportID, createdTaskReportActionID: optimisticTaskCreatedAction.reportActionID, - reportName: optimisticTaskReport.reportName, title: optimisticTaskReport.reportName, description: optimisticTaskReport.description, assignee, assigneeAccountID, assigneeChatReportID, assigneeChatReportActionID: optimisticAssigneeAddComment ? optimisticAssigneeAddComment.reportAction.reportActionID : 0, + assigneeChatCreatedReportActionID: optimisticChatCreatedReportAction ? optimisticChatCreatedReportAction.reportActionID : 0, + }, {optimisticData, successData, failureData}, ); diff --git a/src/pages/tasks/NewTaskPage.js b/src/pages/tasks/NewTaskPage.js index df00ab7b7dfa..555b8657d7fc 100644 --- a/src/pages/tasks/NewTaskPage.js +++ b/src/pages/tasks/NewTaskPage.js @@ -118,7 +118,7 @@ function NewTaskPage(props) { return; } - Task.createTaskAndNavigate(parentReport.reportID, props.task.title, props.task.description, props.task.assignee, props.task.assigneeAccountID); + Task.createTaskAndNavigate(parentReport.reportID, props.task.title, props.task.description, props.task.assignee, props.task.assigneeAccountID, props.task.assigneeChatReport); } if (!Permissions.canUseTasks(props.betas)) { From c748622b343845608b8627653b95b5cbdc4d5bc6 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Wed, 12 Jul 2023 15:00:33 -0700 Subject: [PATCH 06/34] make sure we grab the shareDestination --- src/libs/actions/Task.js | 5 +++-- src/pages/tasks/NewTaskPage.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 4030384befd4..6f4e97028e18 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -585,11 +585,12 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre chatReport = ReportUtils.buildOptimisticChatReport([newAssigneeAccountID]); chatReport.isOptimisticReport = true; } - const reportID = chatReport.reportID; setAssigneeChatReport(chatReport); + // 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(reportID); + setShareDestinationValue(chatReport.reportID); } } diff --git a/src/pages/tasks/NewTaskPage.js b/src/pages/tasks/NewTaskPage.js index 555b8657d7fc..4ad00fe9accb 100644 --- a/src/pages/tasks/NewTaskPage.js +++ b/src/pages/tasks/NewTaskPage.js @@ -118,7 +118,7 @@ function NewTaskPage(props) { return; } - Task.createTaskAndNavigate(parentReport.reportID, props.task.title, props.task.description, props.task.assignee, props.task.assigneeAccountID, props.task.assigneeChatReport); + Task.createTaskAndNavigate(props.task.shareDestination, props.task.title, props.task.description, props.task.assignee, props.task.assigneeAccountID, props.task.assigneeChatReport); } if (!Permissions.canUseTasks(props.betas)) { From c91a06ea6f5ecc02b266c7a9d084babed02c53e6 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Fri, 14 Jul 2023 16:51:52 -0700 Subject: [PATCH 07/34] Add hidden reports --- src/libs/ReportUtils.js | 1 + src/libs/actions/Task.js | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 70203539e92e..be9a57cdb6de 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2057,6 +2057,7 @@ function shouldReportBeInOptionList(report, currentReportId, isInGSDMode, iouRep if ( !report || !report.reportID || + report.isHidden || (_.isEmpty(report.participantAccountIDs) && !isChatThread(report) && !isPublicRoom(report) && !isArchivedRoom(report) && !isMoneyRequestReport(report) && !isTaskReport(report)) ) { return false; diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 6f4e97028e18..d8b2dd46b794 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -584,6 +584,13 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre if (!chatReport) { chatReport = ReportUtils.buildOptimisticChatReport([newAssigneeAccountID]); chatReport.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 + chatReport.isHidden = true; + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport); } setAssigneeChatReport(chatReport); From 9528ad3ec2a6d1214886c322a0902bcc348ee65d Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Fri, 14 Jul 2023 17:49:58 -0700 Subject: [PATCH 08/34] reorder quite a bit of logic --- src/libs/actions/Task.js | 95 +++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index d8b2dd46b794..6725f9c74659 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -63,21 +63,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass let optimisticAssigneeAddComment; let optimisticChatCreatedReportAction; - // If you're choosing to share the task in the same DM as the assignee then - // we don't need to create another reportAction indicating that you've been assigned - // Additionally, tasks can be unassigned in which case we don't need to create a reportAction either - if (assigneeChatReportID && assigneeChatReportID !== parentReportID) { - // ReportAction indicating that the assignee has been assigned a task - optimisticAssigneeAddComment = ReportUtils.buildOptimisticTaskCommentReportAction( - taskReportID, - title, - assignee, - assigneeAccountID, - `Assigned a task to you: ${title}`, - parentReportID, - ); - } - // Parent ReportAction indicating that a task has been created const optimisticAddCommentReport = ReportUtils.buildOptimisticTaskCommentReportAction(taskReportID, title, assignee, assigneeAccountID, `Created a task: ${title}`, parentReportID); optimisticTaskReport.parentReportActionID = optimisticAddCommentReport.reportAction.reportActionID; @@ -156,18 +141,17 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass }, ]; - if (optimisticAssigneeAddComment) { - const lastAssigneeCommentText = ReportUtils.formatReportLastMessageText(optimisticAssigneeAddComment.reportAction.message[0].text); - - const optimisticAssigneeReport = { - lastVisibleActionCreated: currentTime, - lastMessageText: lastAssigneeCommentText, - lastActorAccountID: currentUserAccountID, - lastReadTime: currentTime, - }; - + if (assigneeChatReport) { // If we're optimistically creating a new chat report for the assignee then we need to create the report and reportAction - if (assigneeChatReport && assigneeChatReport.isOptimisticReport) { + if (assigneeChatReport.isOptimisticReport) { + // 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 = { + accountID: assigneeAccountID, + avatar: UserUtils.getDefaultAvatarURL(assigneeAccountID), + displayName: assignee, + login: assignee, + }; + optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); optimisticData.push( { @@ -178,7 +162,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass pendingFields: { createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }, - ...optimisticAssigneeReport + isHidden: false, } }, { @@ -186,6 +170,13 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, value: {[optimisticChatCreatedReportAction.reportActionID]: optimisticChatCreatedReportAction}, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: { + [assigneeAccountID]: optimisticPersonalDetailsListAction, + } } ); @@ -214,32 +205,48 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass value: {[optimisticChatCreatedReportAction.reportActionID]: {pendingAction: null}}, } ); + } + + // If you're choosing to share the task in the same DM as the assignee then + // we don't need to create another reportAction indicating that you've been assigned + // Additionally, tasks can be unassigned in which case we don't need to create a reportAction either + if (assigneeChatReportID && assigneeChatReportID !== parentReportID) { + optimisticAssigneeAddComment = ReportUtils.buildOptimisticTaskCommentReportAction( + taskReportID, + title, + assignee, + assigneeAccountID, + `Assigned a task to you: ${title}`, + parentReportID, + ); + + const lastAssigneeCommentText = ReportUtils.formatReportLastMessageText(optimisticAssigneeAddComment.reportAction.message[0].text); + const optimisticAssigneeReport = { + lastVisibleActionCreated: currentTime, + lastMessageText: lastAssigneeCommentText, + lastActorAccountID: currentUserAccountID, + lastReadTime: currentTime, + }; - } else { - // If the assignee already has a chat report, we just need to update some fields + // Indicates in the DM between you and the assignee that a task has been assigned to them optimisticData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, + value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: optimisticAssigneeAddComment.reportAction}, + }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, value: optimisticAssigneeReport, - }, + } ); - } - - optimisticData.push( - { + failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, - value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: optimisticAssigneeAddComment.reportAction}, - }, - ); - - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, - value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: {pendingAction: null}}, - }); - } + value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: {pendingAction: null}}, + }); + } API.write( 'CreateTask', From 5bc675758ac47cee2827b3395f7f8505447375d1 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Fri, 14 Jul 2023 17:50:28 -0700 Subject: [PATCH 09/34] prettier --- src/libs/ReportUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index be9a57cdb6de..cf8445fbfb49 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2057,7 +2057,7 @@ function shouldReportBeInOptionList(report, currentReportId, isInGSDMode, iouRep if ( !report || !report.reportID || - report.isHidden || + report.isHidden || (_.isEmpty(report.participantAccountIDs) && !isChatThread(report) && !isPublicRoom(report) && !isArchivedRoom(report) && !isMoneyRequestReport(report) && !isTaskReport(report)) ) { return false; From ec76615be3442950cb51d59610c258f35db61e5c Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Fri, 14 Jul 2023 18:06:35 -0700 Subject: [PATCH 10/34] comments and logic --- src/libs/actions/Task.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 6725f9c74659..fbe111ed5f45 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -142,7 +142,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass ]; if (assigneeChatReport) { - // If we're optimistically creating a new chat report for the assignee then we need to create the report and reportAction + // You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions if (assigneeChatReport.isOptimisticReport) { // 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 = { @@ -155,7 +155,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); optimisticData.push( { - onyxMethod: Onyx.METHOD.SET, + onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, value: { ...assigneeChatReport, @@ -166,7 +166,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass } }, { - onyxMethod: Onyx.METHOD.SET, + onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, value: {[optimisticChatCreatedReportAction.reportActionID]: optimisticChatCreatedReportAction}, @@ -179,6 +179,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass } } ); + console.log('What is in optimisticData', optimisticData); successData.push( { @@ -207,10 +208,8 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass ); } - // If you're choosing to share the task in the same DM as the assignee then - // we don't need to create another reportAction indicating that you've been assigned - // Additionally, tasks can be unassigned in which case we don't need to create a reportAction either - if (assigneeChatReportID && assigneeChatReportID !== parentReportID) { + // If you're choosing to share the task in the same DM as the assignee then we don't need to create another reportAction indicating that you've been assigned + if (assigneeChatReportID !== parentReportID) { optimisticAssigneeAddComment = ReportUtils.buildOptimisticTaskCommentReportAction( taskReportID, title, @@ -220,6 +219,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass parentReportID, ); + // We update the lastMessage text of the assignee chat report optimistically as well const lastAssigneeCommentText = ReportUtils.formatReportLastMessageText(optimisticAssigneeAddComment.reportAction.message[0].text); const optimisticAssigneeReport = { lastVisibleActionCreated: currentTime, @@ -228,7 +228,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass lastReadTime: currentTime, }; - // Indicates in the DM between you and the assignee that a task has been assigned to them optimisticData.push( { onyxMethod: Onyx.METHOD.MERGE, @@ -247,6 +246,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: {pendingAction: null}}, }); } + } API.write( 'CreateTask', From ac382ea937acbe8167a1caba6953362d62771a82 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Fri, 14 Jul 2023 18:18:39 -0700 Subject: [PATCH 11/34] Update Task.js --- src/libs/actions/Task.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index fbe111ed5f45..4c63bd4ed744 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -179,7 +179,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass } } ); - console.log('What is in optimisticData', optimisticData); successData.push( { @@ -191,6 +190,14 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass }, isOptimisticReport: false, }, + }, + // Once we successfully create the chat, we'll get the accountID back from the server, this removes the optimistic personal details + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: { + [assigneeAccountID]: null, + } } ); From 16dab5dfe54a9c22dc0991522103af174fee0735 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 17 Jul 2023 15:26:00 -0700 Subject: [PATCH 12/34] move storing personal details when you select a user --- src/libs/actions/Task.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 4c63bd4ed744..a894429f45f9 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -24,6 +24,12 @@ Onyx.connect({ }, }); +let allPersonalDetails; +Onyx.connect({ + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + callback: (val) => (allPersonalDetails = val), +}); + /** * Clears out the task info from the store */ @@ -144,14 +150,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass if (assigneeChatReport) { // You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions if (assigneeChatReport.isOptimisticReport) { - // 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 = { - accountID: assigneeAccountID, - avatar: UserUtils.getDefaultAvatarURL(assigneeAccountID), - displayName: assignee, - login: assignee, - }; - optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); optimisticData.push( { @@ -170,13 +168,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, value: {[optimisticChatCreatedReportAction.reportActionID]: optimisticChatCreatedReportAction}, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [assigneeAccountID]: optimisticPersonalDetailsListAction, - } } ); @@ -593,6 +584,7 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre if (!newAssigneeAccountID) { newAssigneeAccountID = UserUtils.generateAccountID(); } + if (!isCurrentUser) { let chatReport = ReportUtils.getChatByParticipants([newAssigneeAccountID]); if (!chatReport) { @@ -604,8 +596,18 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre // 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 chatReport.isHidden = true; - Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport); + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport); + + // 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 = { + accountID: newAssigneeAccountID, + avatar: UserUtils.getDefaultAvatar(newAssigneeAccountID), + displayName: assignee, + login: assignee, + }; + Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[newAssigneeAccountID]: optimisticPersonalDetailsListAction}); } + setAssigneeChatReport(chatReport); // If there is no share destination set, automatically set it to the assignee chat report From b8bc106148687e250f1c04191d0ae646a7f85ed6 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 17 Jul 2023 15:36:00 -0700 Subject: [PATCH 13/34] fix the prop issue --- src/libs/actions/Task.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index a894429f45f9..3a925ec88167 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -150,6 +150,14 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass if (assigneeChatReport) { // You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions if (assigneeChatReport.isOptimisticReport) { + const optimisticPersonalDetailsListAction = { + accountID: assigneeAccountID, + avatar: lodashGet(allPersonalDetails, [assigneeAccountID, 'avatar'], UserUtils.getDefaultAvatarURL(assigneeAccountID)), + displayName: lodashGet(allPersonalDetails, [assigneeAccountID, 'displayName'], assignee), + login: assignee, + }; + + console.log('optimistic personal details list action', optimisticPersonalDetailsListAction); optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); optimisticData.push( { @@ -163,6 +171,13 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass isHidden: false, } }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: { + [assigneeAccountID]: optimisticPersonalDetailsListAction, + } + }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, @@ -579,6 +594,7 @@ function setAssigneeValueWithParentReportID(reportID) { function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurrentUser = false) { let newAssigneeAccountID = Number(assigneeAccountID); + console.log('setAssigneeValue', assignee, assigneeAccountID, shareDestination, isCurrentUser); // Generate optimistic accountID if this is a brand new user account that hasn't been created yet if (!newAssigneeAccountID) { @@ -598,14 +614,16 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre chatReport.isHidden = true; Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport); + /** // 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 = { accountID: newAssigneeAccountID, - avatar: UserUtils.getDefaultAvatar(newAssigneeAccountID), + avatar: lodashGet(allPersonalDetails, [newAssigneeAccountID, 'avatar'], UserUtils.getDefaultAvatar(newAssigneeAccountID)), displayName: assignee, login: assignee, }; - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[newAssigneeAccountID]: optimisticPersonalDetailsListAction}); + Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, {[newAssigneeAccountID]: optimisticPersonalDetailsListAction}); + */ } setAssigneeChatReport(chatReport); From 6a057fe6133bfc7aa61c9aa421e8c401645cf199 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 17 Jul 2023 15:38:52 -0700 Subject: [PATCH 14/34] move back into assignee selector --- src/libs/actions/Task.js | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 3a925ec88167..12115b6f90d8 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -150,14 +150,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass if (assigneeChatReport) { // You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions if (assigneeChatReport.isOptimisticReport) { - const optimisticPersonalDetailsListAction = { - accountID: assigneeAccountID, - avatar: lodashGet(allPersonalDetails, [assigneeAccountID, 'avatar'], UserUtils.getDefaultAvatarURL(assigneeAccountID)), - displayName: lodashGet(allPersonalDetails, [assigneeAccountID, 'displayName'], assignee), - login: assignee, - }; - - console.log('optimistic personal details list action', optimisticPersonalDetailsListAction); optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); optimisticData.push( { @@ -171,13 +163,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass isHidden: false, } }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [assigneeAccountID]: optimisticPersonalDetailsListAction, - } - }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, @@ -614,16 +599,18 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre chatReport.isHidden = true; Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport); - /** + // 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 = { accountID: newAssigneeAccountID, - avatar: lodashGet(allPersonalDetails, [newAssigneeAccountID, 'avatar'], UserUtils.getDefaultAvatar(newAssigneeAccountID)), - displayName: assignee, + avatar: lodashGet(allPersonalDetails, [newAssigneeAccountID, 'avatar'], UserUtils.getDefaultAvatarURL(newAssigneeAccountID)), + displayName: lodashGet(allPersonalDetails, [newAssigneeAccountID, 'displayName'], assignee), login: assignee, }; - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, {[newAssigneeAccountID]: optimisticPersonalDetailsListAction}); - */ + + console.log('optimisticPersonalDetailsListAction', optimisticPersonalDetailsListAction); + Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[newAssigneeAccountID]: optimisticPersonalDetailsListAction}); + } setAssigneeChatReport(chatReport); From 4f3587f3bebaab920d34a9aedf02c1f28e512161 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 17 Jul 2023 15:42:30 -0700 Subject: [PATCH 15/34] add failure data --- src/libs/actions/Task.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 12115b6f90d8..344b4a90127a 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -202,6 +202,13 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, value: {[optimisticChatCreatedReportAction.reportActionID]: {pendingAction: null}}, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: { + [assigneeAccountID]: null, + } } ); } From 1b05d7d85b61cd0100760aecdaab133fa15d2178 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 17 Jul 2023 15:57:12 -0700 Subject: [PATCH 16/34] Set the message --- src/libs/ReportUtils.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index cf8445fbfb49..8bc6209587b8 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1353,6 +1353,7 @@ function buildOptimisticAddCommentReportAction(text, file) { function buildOptimisticTaskCommentReportAction(taskReportID, taskTitle, taskAssignee, taskAssigneeAccountID, text, parentReportID) { const reportAction = buildOptimisticAddCommentReportAction(text); reportAction.reportAction.message[0].taskReportID = taskReportID; + reportAction.reportAction.message[0].text = reportAction.commentText; // These parameters are not saved on the reportAction, but are used to display the task in the UI // Added when we fetch the reportActions on a report From edaf71d8fd94fa719f9e8d669656c995163c0b1a Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 17 Jul 2023 16:17:56 -0700 Subject: [PATCH 17/34] remove logs / prettify --- src/libs/actions/Task.js | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 344b4a90127a..97a6010a551b 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -43,7 +43,7 @@ function clearOutTaskInfo() { * A task report is created, along with a CreatedReportAction for that new task report * A reportAction indicating that a task was created is added to the parent report (share destination) * If you assign the task to someone, a reportAction is created in the chat between you and the assignee to inform them of the task - * + * * So you have the following optimistic items potentially being created: * 1. The task report * 1a. The CreatedReportAction for the task report @@ -51,7 +51,7 @@ function clearOutTaskInfo() { * 3. The chat report between you and the assignee * 3a. The CreatedReportAction for the assignee chat report * 3b. The TaskReportAction on the assignee chat report - * + * * @param {String} parentReportID * @param {String} title * @param {String} description @@ -63,7 +63,7 @@ function clearOutTaskInfo() { function createTaskAndNavigate(parentReportID, title, description, assignee, assigneeAccountID = 0, assigneeChatReport = {}) { const optimisticTaskReport = ReportUtils.buildOptimisticTaskReport(currentUserEmail, currentUserAccountID, assigneeAccountID, parentReportID, title, description); const optimisticTaskCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(optimisticTaskReport.reportID); - + const assigneeChatReportID = assigneeChatReport.reportID; const taskReportID = optimisticTaskReport.reportID; let optimisticAssigneeAddComment; @@ -161,14 +161,13 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }, isHidden: false, - } + }, }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, value: {[optimisticChatCreatedReportAction.reportActionID]: optimisticChatCreatedReportAction}, - - } + }, ); successData.push( @@ -181,15 +180,15 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass }, isOptimisticReport: false, }, - }, + }, // Once we successfully create the chat, we'll get the accountID back from the server, this removes the optimistic personal details { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.PERSONAL_DETAILS_LIST, value: { [assigneeAccountID]: null, - } - } + }, + }, ); failureData.push( @@ -208,8 +207,8 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass key: ONYXKEYS.PERSONAL_DETAILS_LIST, value: { [assigneeAccountID]: null, - } - } + }, + }, ); } @@ -243,7 +242,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, value: optimisticAssigneeReport, - } + }, ); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -267,7 +266,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass assigneeChatReportID, assigneeChatReportActionID: optimisticAssigneeAddComment ? optimisticAssigneeAddComment.reportAction.reportActionID : 0, assigneeChatCreatedReportActionID: optimisticChatCreatedReportAction ? optimisticChatCreatedReportAction.reportActionID : 0, - }, {optimisticData, successData, failureData}, ); @@ -586,7 +584,6 @@ function setAssigneeValueWithParentReportID(reportID) { function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurrentUser = false) { let newAssigneeAccountID = Number(assigneeAccountID); - console.log('setAssigneeValue', assignee, assigneeAccountID, shareDestination, isCurrentUser); // Generate optimistic accountID if this is a brand new user account that hasn't been created yet if (!newAssigneeAccountID) { @@ -604,9 +601,8 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre // 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 chatReport.isHidden = true; - Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport); + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport); - // 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 = { accountID: newAssigneeAccountID, @@ -614,10 +610,7 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre displayName: lodashGet(allPersonalDetails, [newAssigneeAccountID, 'displayName'], assignee), login: assignee, }; - - console.log('optimisticPersonalDetailsListAction', optimisticPersonalDetailsListAction); Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[newAssigneeAccountID]: optimisticPersonalDetailsListAction}); - } setAssigneeChatReport(chatReport); From ce262d8dbddea0f6076ed04f2fdb34468c1944e7 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 24 Jul 2023 18:27:13 -0700 Subject: [PATCH 18/34] clean up the logic / add more comments --- src/libs/actions/Task.js | 69 ++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index f15ca32fdd8c..281f478e4900 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -62,7 +62,7 @@ function clearOutTaskInfo() { */ function createTaskAndNavigate(parentReportID, title, description, assignee, assigneeAccountID = 0, assigneeChatReport = {}) { - const optimisticTaskReport = ReportUtils.buildOptimisticTaskReport(currentUserEmail, currentUserAccountID, assigneeAccountID, parentReportID, title, description); + const optimisticTaskReport = ReportUtils.buildOptimisticTaskReport(currentUserAccountID, assigneeAccountID, parentReportID, title, description); const assigneeChatReportID = assigneeChatReport.reportID; const taskReportID = optimisticTaskReport.reportID; @@ -84,8 +84,10 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass lastReadTime: currentTime, }; + // We're only setting onyx data for the task report here because it's possible for the parent report to not exist yet (if you're assigning a task to someone you haven't chatted with before) + // So we don't want to set the parent report data until we've successfully created that chat report + // FOR TASK REPORT const optimisticData = [ - // Task Report { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticTaskReport.reportID}`, @@ -97,26 +99,14 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass isOptimisticReport: true, }, }, - // Task Report Actions { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTaskReport.reportID}`, value: {[optimisticTaskCreatedAction.reportActionID]: optimisticTaskCreatedAction}, }, - // Parent Report - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, - value: optimisticParentReport, - }, - // Parent Report Actions - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, - value: {[optimisticAddCommentReport.reportAction.reportActionID]: optimisticAddCommentReport.reportAction}, - }, ]; + // FOR TASK REPORT const successData = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -133,13 +123,9 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTaskReport.reportID}`, value: {[optimisticTaskCreatedAction.reportActionID]: {pendingAction: null}}, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, - value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, - }, ]; + // FOR TASK REPORT const failureData = [ { onyxMethod: Onyx.METHOD.SET, @@ -151,11 +137,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTaskReport.reportID}`, value: {[optimisticTaskCreatedAction.reportActionID]: {pendingAction: null}}, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, - value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, - }, ]; if (assigneeChatReport) { @@ -167,7 +148,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, value: { - ...assigneeChatReport, pendingFields: { createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }, @@ -192,7 +172,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass isOptimisticReport: false, }, }, - // Once we successfully create the chat, we'll get the accountID back from the server, this removes the optimistic personal details + // Once we successfully create the chat, we'll get the accountID back from the server so this removes the optimistic personal details { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.PERSONAL_DETAILS_LIST, @@ -213,6 +193,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, value: {[optimisticChatCreatedReportAction.reportActionID]: {pendingAction: null}}, }, + // If we failed, we want to remove the optimistic personal details as it was likely due to an invalid login { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.PERSONAL_DETAILS_LIST, @@ -234,7 +215,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass parentReportID, ); - // We update the lastMessage text of the assignee chat report optimistically as well const lastAssigneeCommentText = ReportUtils.formatReportLastMessageText(optimisticAssigneeAddComment.reportAction.message[0].text); const optimisticAssigneeReport = { lastVisibleActionCreated: currentTime, @@ -261,6 +241,39 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: {pendingAction: null}}, }); } + + // Now that we've created the optimistic chat report and chat reportActions, we can update the parent report data + // FOR PARENT REPORT (SHARE DESTINATION) + optimisticData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, + value: optimisticParentReport, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, + value: {[optimisticAddCommentReport.reportAction.reportActionID]: optimisticAddCommentReport.reportAction}, + }, + ); + + // FOR PARENT REPORT (SHARE DESTINATION) + successData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, + value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, + } + ); + + // FOR PARENT REPORT (SHARE DESTINATION) + failureData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, + value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, + }, + ); } API.write( From 186221c5f5c26c8fa37ea8ecbe62dc8185f09fcd Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 24 Jul 2023 18:27:32 -0700 Subject: [PATCH 19/34] remove setting the text --- src/libs/ReportUtils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 4cfc2fb1d284..3a480c79e6a7 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1452,7 +1452,6 @@ const getOptimisticDataForParentReportAction = (reportID, lastVisibleActionCreat function buildOptimisticTaskCommentReportAction(taskReportID, taskTitle, taskAssignee, taskAssigneeAccountID, text, parentReportID) { const reportAction = buildOptimisticAddCommentReportAction(text); reportAction.reportAction.message[0].taskReportID = taskReportID; - reportAction.reportAction.message[0].text = reportAction.commentText; // These parameters are not saved on the reportAction, but are used to display the task in the UI // Added when we fetch the reportActions on a report From 059737e6d261e551a1e1bfea36e83b24ae25f146 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 24 Jul 2023 18:33:22 -0700 Subject: [PATCH 20/34] Fix issue with new created --- src/libs/actions/Task.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 281f478e4900..efbb425379aa 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -141,7 +141,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass if (assigneeChatReport) { // You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions - if (assigneeChatReport.isOptimisticReport) { + if (assigneeChatReport.isOptimisticReport && lodashGet(assigneeChatReport, 'pendingFields.createChat') !== CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); optimisticData.push( { From 4cb65bc7fe29c099ecda28f67c7e12ba70c34681 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 24 Jul 2023 18:37:06 -0700 Subject: [PATCH 21/34] remove clearing the optimistic personal details --- src/libs/actions/Task.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index efbb425379aa..20a7658138fb 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -172,14 +172,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass isOptimisticReport: false, }, }, - // Once we successfully create the chat, we'll get the accountID back from the server so this removes the optimistic personal details - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [assigneeAccountID]: null, - }, - }, ); failureData.push( From 9f9dd5ad4c5aa9df97a55929983f1a5ab88c7d30 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 24 Jul 2023 18:38:53 -0700 Subject: [PATCH 22/34] prettier --- src/libs/actions/Task.js | 48 ++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 20a7658138fb..86f760f7654f 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -161,18 +161,16 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass }, ); - successData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, - value: { - pendingFields: { - createChat: null, - }, - isOptimisticReport: false, + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, + value: { + pendingFields: { + createChat: null, }, + isOptimisticReport: false, }, - ); + }); failureData.push( { @@ -250,22 +248,18 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass ); // FOR PARENT REPORT (SHARE DESTINATION) - successData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, - value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, - } - ); + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, + value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, + }); // FOR PARENT REPORT (SHARE DESTINATION) - failureData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, - value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, - }, - ); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, + value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, + }); } API.write( @@ -595,10 +589,10 @@ function setShareDestinationValue(shareDestination) { } /* Sets the assigneeChatReport details for the task -* @param {Object} chatReport -*/ + * @param {Object} chatReport + */ function setAssigneeChatReport(chatReport) { - Onyx.merge(ONYXKEYS.TASK, {assigneeChatReport: chatReport}); + Onyx.merge(ONYXKEYS.TASK, {assigneeChatReport: chatReport}); } /** From ff582713f3beaeabda1069e7202b83eb64bf40f2 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Tue, 25 Jul 2023 12:56:57 -0700 Subject: [PATCH 23/34] Fix is hidden --- src/libs/actions/Task.js | 5 ++--- src/pages/home/sidebar/SidebarLinksData.js | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index f481de50393e..6956efbdce92 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -4,7 +4,6 @@ import _ from 'underscore'; import ONYXKEYS from '../../ONYXKEYS'; import * as API from '../API'; import * as ReportUtils from '../ReportUtils'; -import * as Report from './Report'; import Navigation from '../Navigation/Navigation'; import ROUTES from '../../ROUTES'; import CONST from '../../CONST'; @@ -58,9 +57,8 @@ function clearOutTaskInfo() { * @param {String} description * @param {String} assignee * @param {Number} assigneeAccountID - * + * @param {Object} assigneeChatReport - The chat report between you and the assignee */ - function createTaskAndNavigate(parentReportID, title, description, assignee, assigneeAccountID = 0, assigneeChatReport = {}) { const optimisticTaskReport = ReportUtils.buildOptimisticTaskReport(currentUserAccountID, assigneeAccountID, parentReportID, title, description); @@ -141,6 +139,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass if (assigneeChatReport) { // You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions + // Only add the assignee chat report to onyx if we haven't already set it optimistically if (assigneeChatReport.isOptimisticReport && lodashGet(assigneeChatReport, 'pendingFields.createChat') !== CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); optimisticData.push( diff --git a/src/pages/home/sidebar/SidebarLinksData.js b/src/pages/home/sidebar/SidebarLinksData.js index a051e7e33ca7..1a3eeb450583 100644 --- a/src/pages/home/sidebar/SidebarLinksData.js +++ b/src/pages/home/sidebar/SidebarLinksData.js @@ -116,6 +116,7 @@ const chatReportSelector = (report) => participantAccountIDs: report.participantAccountIDs, hasDraft: report.hasDraft, isPinned: report.isPinned, + isHidden: report.isHidden, errorFields: { addWorkspaceRoom: report.errorFields && report.errorFields.addWorkspaceRoom, }, From 32d92104eea33e19a9cc203b07db87b9900c7968 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Tue, 25 Jul 2023 15:56:29 -0700 Subject: [PATCH 24/34] dry up the logic and use in EditTask --- src/libs/ReportUtils.js | 127 +++++++++++ src/libs/actions/Task.js | 225 +++++-------------- src/pages/tasks/TaskAssigneeSelectorModal.js | 2 +- 3 files changed, 181 insertions(+), 173 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 481b61b2296d..24d8e942c30a 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2661,6 +2661,132 @@ function shouldDisableRename(report, policy) { return !_.keys(loginList).includes(policy.owner) && policy.role !== CONST.POLICY.ROLE.ADMIN; } +/** + * Returns the onyx data needed for the task assignee chat + * @param {Number} accountID + * @param {String} assignee - Assignee email + * @param {Number} assigneeAccountID + * @param {String} taskReportID + * @param {String} assigneeChatReportID + * @param {String} parentReportID + * @param {String} title + * @param {Object} assigneeChatReport + * @returns {Object} + */ +function getTaskAssigneeChatOnyxData(accountID, assignee, assigneeAccountID, taskReportID, assigneeChatReportID, parentReportID, title, assigneeChatReport) { + // Set if we need to add a comment to the assignee chat notifying them that they have been assigned a task + let optimisticAssigneeAddComment; + // Set if this is a new chat that needs to be created for the assignee + let optimisticChatCreatedReportAction; + const currentTime = DateUtils.getDBTime(); + const optimisticData = []; + const successData = []; + const failureData = []; + + // You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions + // Only add the assignee chat report to onyx if we haven't already set it optimistically + if (assigneeChatReport.isOptimisticReport && lodashGet(assigneeChatReport, 'pendingFields.createChat') !== CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { + optimisticChatCreatedReportAction = buildOptimisticCreatedReportAction(assigneeChatReportID); + optimisticData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, + value: { + pendingFields: { + createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + isHidden: false, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, + value: {[optimisticChatCreatedReportAction.reportActionID]: optimisticChatCreatedReportAction}, + }, + ); + + successData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, + value: { + pendingFields: { + createChat: null, + }, + isOptimisticReport: false, + }, + }, + ); + + failureData.push( + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, + value: null, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, + value: {[optimisticChatCreatedReportAction.reportActionID]: {pendingAction: null}}, + }, + // If we failed, we want to remove the optimistic personal details as it was likely due to an invalid login + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: { + [assigneeAccountID]: null, + }, + }, + ); + } + + // If you're choosing to share the task in the same DM as the assignee then we don't need to create another reportAction indicating that you've been assigned + if (assigneeChatReportID !== parentReportID) { + optimisticAssigneeAddComment = buildOptimisticTaskCommentReportAction( + taskReportID, + title, + assignee, + assigneeAccountID, + `Assigned a task to you: ${title}`, + parentReportID, + ); + + const lastAssigneeCommentText = formatReportLastMessageText(optimisticAssigneeAddComment.reportAction.message[0].text); + const optimisticAssigneeReport = { + lastVisibleActionCreated: currentTime, + lastMessageText: lastAssigneeCommentText, + lastActorAccountID: accountID, + lastReadTime: currentTime, + }; + + optimisticData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, + value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: optimisticAssigneeAddComment.reportAction}, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, + value: optimisticAssigneeReport, + }, + ); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, + value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: {pendingAction: null}}, + }); + } + + return { + optimisticData, + successData, + failureData, + optimisticAssigneeAddComment, + optimisticChatCreatedReportAction + }; +} + export { getReportParticipantsTitle, isReportMessageAttachment, @@ -2773,4 +2899,5 @@ export { shouldDisableSettings, shouldDisableRename, hasSingleParticipant, + getTaskAssigneeChatOnyxData, }; diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 6956efbdce92..f1edf0bb8df9 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -59,13 +59,12 @@ function clearOutTaskInfo() { * @param {Number} assigneeAccountID * @param {Object} assigneeChatReport - The chat report between you and the assignee */ -function createTaskAndNavigate(parentReportID, title, description, assignee, assigneeAccountID = 0, assigneeChatReport = {}) { +function createTaskAndNavigate(parentReportID, title, description, assignee, assigneeAccountID = 0, assigneeChatReport = null) { const optimisticTaskReport = ReportUtils.buildOptimisticTaskReport(currentUserAccountID, assigneeAccountID, parentReportID, title, description); - const assigneeChatReportID = assigneeChatReport.reportID; + const assigneeChatReportID = assigneeChatReport ? assigneeChatReport.reportID : 0; const taskReportID = optimisticTaskReport.reportID; - let optimisticAssigneeAddComment; - let optimisticChatCreatedReportAction; + let assigneeChatReportOnyxData; // Parent ReportAction indicating that a task has been created const optimisticTaskCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(currentUserEmail); @@ -74,7 +73,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass const currentTime = DateUtils.getDBTime(); const lastCommentText = ReportUtils.formatReportLastMessageText(optimisticAddCommentReport.reportAction.message[0].text); - const optimisticParentReport = { lastVisibleActionCreated: currentTime, lastMessageText: lastCommentText, @@ -138,128 +136,41 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass ]; if (assigneeChatReport) { - // You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions - // Only add the assignee chat report to onyx if we haven't already set it optimistically - if (assigneeChatReport.isOptimisticReport && lodashGet(assigneeChatReport, 'pendingFields.createChat') !== CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { - optimisticChatCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(assigneeChatReportID); - optimisticData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, - value: { - pendingFields: { - createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - isHidden: false, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, - value: {[optimisticChatCreatedReportAction.reportActionID]: optimisticChatCreatedReportAction}, - }, - ); - - successData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, - value: { - pendingFields: { - createChat: null, - }, - isOptimisticReport: false, - }, - }); - - failureData.push( - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, - value: null, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, - value: {[optimisticChatCreatedReportAction.reportActionID]: {pendingAction: null}}, - }, - // If we failed, we want to remove the optimistic personal details as it was likely due to an invalid login - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [assigneeAccountID]: null, - }, - }, - ); - } - - // If you're choosing to share the task in the same DM as the assignee then we don't need to create another reportAction indicating that you've been assigned - if (assigneeChatReportID !== parentReportID) { - optimisticAssigneeAddComment = ReportUtils.buildOptimisticTaskCommentReportAction( - taskReportID, - title, - assignee, - assigneeAccountID, - `Assigned a task to you: ${title}`, - parentReportID, - ); - - const lastAssigneeCommentText = ReportUtils.formatReportLastMessageText(optimisticAssigneeAddComment.reportAction.message[0].text); - const optimisticAssigneeReport = { - lastVisibleActionCreated: currentTime, - lastMessageText: lastAssigneeCommentText, - lastActorAccountID: currentUserAccountID, - lastReadTime: currentTime, - }; - - optimisticData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, - value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: optimisticAssigneeAddComment.reportAction}, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, - value: optimisticAssigneeReport, - }, - ); - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, - value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: {pendingAction: null}}, - }); - } - - // Now that we've created the optimistic chat report and chat reportActions, we can update the parent report data - // FOR PARENT REPORT (SHARE DESTINATION) - optimisticData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, - value: optimisticParentReport, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, - value: {[optimisticAddCommentReport.reportAction.reportActionID]: optimisticAddCommentReport.reportAction}, - }, - ); + assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData(currentUserAccountID, assignee, assigneeAccountID, taskReportID, assigneeChatReportID, parentReportID, title, assigneeChatReport); + optimisticData.push(...assigneeChatReportOnyxData.optimisticData); + successData.push(...assigneeChatReportOnyxData.successData); + failureData.push(...assigneeChatReportOnyxData.failureData); + } - // FOR PARENT REPORT (SHARE DESTINATION) - successData.push({ + // Now that we've created the optimistic chat report and chat reportActions, we can update the parent report data + // FOR PARENT REPORT (SHARE DESTINATION) + optimisticData.push( + { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, - value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, - }); - - // FOR PARENT REPORT (SHARE DESTINATION) - failureData.push({ + key: `${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, + value: optimisticParentReport, + }, + { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, - value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, - }); - } + value: {[optimisticAddCommentReport.reportAction.reportActionID]: optimisticAddCommentReport.reportAction}, + }, + ); + + // FOR PARENT REPORT (SHARE DESTINATION) + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, + value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, + }); + + // FOR PARENT REPORT (SHARE DESTINATION) + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, + value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, + }); + API.write( 'CreateTask', @@ -273,8 +184,8 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass assignee, assigneeAccountID, assigneeChatReportID, - assigneeChatReportActionID: optimisticAssigneeAddComment ? optimisticAssigneeAddComment.reportAction.reportActionID : 0, - assigneeChatCreatedReportActionID: optimisticChatCreatedReportAction ? optimisticChatCreatedReportAction.reportActionID : 0, + assigneeChatReportActionID: assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticAssigneeAddComment ? assigneeChatReportOnyxData.optimisticAssigneeAddComment.reportAction.reportActionID : 0, + assigneeChatCreatedReportActionID: assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticChatCreatedReportAction ? assigneeChatReportOnyxData.optimisticChatCreatedReportAction.reportActionID : 0, }, {optimisticData, successData, failureData}, ); @@ -434,8 +345,9 @@ function reopenTask(taskReportID, taskTitle) { * @param {String} editedTask.description * @param {String} editedTask.assignee * @param {Number} editedTask.assigneeAccountID + * @param {Object} assigneeChatReport */ -function editTaskAndNavigate(report, ownerAccountID, {title, description, assignee = '', assigneeAccountID = 0}) { +function editTaskAndNavigate(report, ownerAccountID, {title, description, assignee = '', assigneeAccountID = 0}, assigneeChatReport = null) { // Create the EditedReportAction on the task const editTaskReportAction = ReportUtils.buildOptimisticEditedTaskReportAction(currentUserEmail); @@ -445,22 +357,8 @@ function editTaskAndNavigate(report, ownerAccountID, {title, description, assign // Description can be unset, so we default to an empty string if so const reportDescription = (!_.isUndefined(description) ? description : lodashGet(report, 'description', '')).trim(); - // If we make a change to the assignee, we want to add a comment to the assignee's chat - let optimisticAssigneeAddComment; - let assigneeChatReportID; - if (assigneeAccountID && assigneeAccountID !== report.managerID && assigneeAccountID !== ownerAccountID) { - assigneeChatReportID = ReportUtils.getChatByParticipants([assigneeAccountID]).reportID; - - if (assigneeChatReportID !== report.parentReportID.toString()) { - optimisticAssigneeAddComment = ReportUtils.buildOptimisticTaskCommentReportAction( - report.reportID, - reportName, - assignee, - assigneeAccountID, - `Assigned a task to you: ${reportName}`, - ); - } - } + let assigneeChatReportOnyxData; + const assigneeChatReportID = assigneeChatReport ? assigneeChatReport.reportID : 0; const optimisticData = [ { @@ -493,36 +391,15 @@ function editTaskAndNavigate(report, ownerAccountID, {title, description, assign }, ]; - if (optimisticAssigneeAddComment) { - const currentTime = DateUtils.getDBTime(); - const lastAssigneeCommentText = ReportUtils.formatReportLastMessageText(optimisticAssigneeAddComment.reportAction.message[0].text); - - const optimisticAssigneeReport = { - lastVisibleActionCreated: currentTime, - lastMessageText: lastAssigneeCommentText, - lastActorAccountID: ownerAccountID, - lastReadTime: currentTime, - }; - - optimisticData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, - value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: optimisticAssigneeAddComment.reportAction}, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, - value: optimisticAssigneeReport, - }, - ); - - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, - value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: {pendingAction: null}}, - }); + // If we make a change to the assignee, we want to add a comment to the assignee's chat + // Check if the assignee actually changed + if (assigneeAccountID && assigneeAccountID !== report.managerID && assigneeAccountID !== ownerAccountID && assigneeChatReport) { + assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData(currentUserAccountID, assignee, assigneeAccountID, report.reportID, assigneeChatReportID, report.parentReportID, reportName, assigneeChatReport); + optimisticData.push(...assigneeChatReportOnyxData.optimisticData); + successData.push(...assigneeChatReportOnyxData.successData); + failureData.push(...assigneeChatReportOnyxData.failureData); } + API.write( 'EditTask', @@ -533,7 +410,10 @@ function editTaskAndNavigate(report, ownerAccountID, {title, description, assign assignee: assignee || report.managerEmail, assigneeAccountID: assigneeAccountID || report.managerID, editedTaskReportActionID: editTaskReportAction.reportActionID, - assigneeChatReportActionID: optimisticAssigneeAddComment ? optimisticAssigneeAddComment.reportAction.reportActionID : 0, + assigneeChatReportID, + assigneeChatReportActionID: assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticAssigneeAddComment ? assigneeChatReportOnyxData.optimisticAssigneeAddComment.reportAction.reportActionID : 0, + assigneeChatCreatedReportActionID: assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticChatCreatedReportAction ? assigneeChatReportOnyxData.optimisticChatCreatedReportAction.reportActionID : 0, + }, {optimisticData, successData, failureData}, ); @@ -633,6 +513,7 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[newAssigneeAccountID]: optimisticPersonalDetailsListAction}); } + console.log('Setting assignee chat report'); setAssigneeChatReport(chatReport); // If there is no share destination set, automatically set it to the assignee chat report diff --git a/src/pages/tasks/TaskAssigneeSelectorModal.js b/src/pages/tasks/TaskAssigneeSelectorModal.js index 6cad4cc12869..5e4b76cf12f0 100644 --- a/src/pages/tasks/TaskAssigneeSelectorModal.js +++ b/src/pages/tasks/TaskAssigneeSelectorModal.js @@ -173,7 +173,7 @@ function TaskAssigneeSelectorModal(props) { Task.editTaskAndNavigate(props.task.report, props.session.accountID, { assignee: option.login, assigneeAccountID: option.accountID, - }); + }, props.task.assigneeChatReport); } }; From a7ef027d7c69cc9a0387a582ca6f1f6b2264e689 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Tue, 25 Jul 2023 16:06:59 -0700 Subject: [PATCH 25/34] Update Task.js --- src/libs/actions/Task.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index f1edf0bb8df9..237dd8d86ab0 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -513,7 +513,6 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[newAssigneeAccountID]: optimisticPersonalDetailsListAction}); } - console.log('Setting assignee chat report'); setAssigneeChatReport(chatReport); // If there is no share destination set, automatically set it to the assignee chat report From 5bf328c5df0cbda2014bea3cf2f763e4223131f0 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Wed, 26 Jul 2023 17:13:54 -0700 Subject: [PATCH 26/34] return chatReport for EditTask --- src/libs/actions/Task.js | 8 +++++++- src/pages/tasks/TaskAssigneeSelectorModal.js | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 237dd8d86ab0..595ed9d04034 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -484,6 +484,7 @@ function setAssigneeChatReport(chatReport) { function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurrentUser = false) { let newAssigneeAccountID = Number(assigneeAccountID); + let chatReport; // Generate optimistic accountID if this is a brand new user account that hasn't been created yet if (!newAssigneeAccountID) { @@ -491,7 +492,7 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre } if (!isCurrentUser) { - let chatReport = ReportUtils.getChatByParticipants([newAssigneeAccountID]); + chatReport = ReportUtils.getChatByParticipants([newAssigneeAccountID]); if (!chatReport) { chatReport = ReportUtils.buildOptimisticChatReport([newAssigneeAccountID]); chatReport.isOptimisticReport = true; @@ -524,6 +525,11 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre // This is only needed for creation of a new task and so it should only be stored locally Onyx.merge(ONYXKEYS.TASK, {assignee, assigneeAccountID: newAssigneeAccountID}); + + // When we're editing the assignee, we immediately call EditTaskAndNavigate. Since setting the assignee is async, + // the chatReport is not yet set when EditTaskAndNavigate is called. So we return the chatReport here so that + // EditTaskAndNavigate can use it. + return chatReport; } /** diff --git a/src/pages/tasks/TaskAssigneeSelectorModal.js b/src/pages/tasks/TaskAssigneeSelectorModal.js index 5e4b76cf12f0..fd9f70c2c75c 100644 --- a/src/pages/tasks/TaskAssigneeSelectorModal.js +++ b/src/pages/tasks/TaskAssigneeSelectorModal.js @@ -167,13 +167,13 @@ function TaskAssigneeSelectorModal(props) { if (props.route.params.reportID && props.task.report.reportID === props.route.params.reportID) { // There was an issue where sometimes a new assignee didn't have a DM thread // This would cause the app to crash, so we need to make sure we have a DM thread - Task.setAssigneeValue(option.login, option.accountID, props.task.shareDestination, OptionsListUtils.isCurrentUser(option)); + const assigneeChatReport = Task.setAssigneeValue(option.login, option.accountID, props.task.shareDestination, OptionsListUtils.isCurrentUser(option)); // Pass through the selected assignee Task.editTaskAndNavigate(props.task.report, props.session.accountID, { assignee: option.login, assigneeAccountID: option.accountID, - }, props.task.assigneeChatReport); + }, assigneeChatReport); } }; From 7776ddc9d47464d9f452097f4526a2d95b8659d9 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Wed, 26 Jul 2023 17:18:49 -0700 Subject: [PATCH 27/34] Prettier --- src/libs/ReportUtils.js | 41 ++++++++------------ src/libs/actions/Task.js | 41 +++++++++++++++----- src/pages/tasks/TaskAssigneeSelectorModal.js | 13 +++++-- 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 39873becd24c..45ef994af1e4 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2741,13 +2741,13 @@ function shouldDisableRename(report, policy) { /** * Returns the onyx data needed for the task assignee chat - * @param {Number} accountID + * @param {Number} accountID * @param {String} assignee - Assignee email * @param {Number} assigneeAccountID - * @param {String} taskReportID - * @param {String} assigneeChatReportID - * @param {String} parentReportID - * @param {String} title + * @param {String} taskReportID + * @param {String} assigneeChatReportID + * @param {String} parentReportID + * @param {String} title * @param {Object} assigneeChatReport * @returns {Object} */ @@ -2765,7 +2765,7 @@ function getTaskAssigneeChatOnyxData(accountID, assignee, assigneeAccountID, tas // Only add the assignee chat report to onyx if we haven't already set it optimistically if (assigneeChatReport.isOptimisticReport && lodashGet(assigneeChatReport, 'pendingFields.createChat') !== CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { optimisticChatCreatedReportAction = buildOptimisticCreatedReportAction(assigneeChatReportID); - optimisticData.push( + optimisticData.push( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, @@ -2783,18 +2783,16 @@ function getTaskAssigneeChatOnyxData(accountID, assignee, assigneeAccountID, tas }, ); - successData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, - value: { - pendingFields: { - createChat: null, - }, - isOptimisticReport: false, + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, + value: { + pendingFields: { + createChat: null, }, + isOptimisticReport: false, }, - ); + }); failureData.push( { @@ -2820,14 +2818,7 @@ function getTaskAssigneeChatOnyxData(accountID, assignee, assigneeAccountID, tas // If you're choosing to share the task in the same DM as the assignee then we don't need to create another reportAction indicating that you've been assigned if (assigneeChatReportID !== parentReportID) { - optimisticAssigneeAddComment = buildOptimisticTaskCommentReportAction( - taskReportID, - title, - assignee, - assigneeAccountID, - `Assigned a task to you: ${title}`, - parentReportID, - ); + optimisticAssigneeAddComment = buildOptimisticTaskCommentReportAction(taskReportID, title, assignee, assigneeAccountID, `Assigned a task to you: ${title}`, parentReportID); const lastAssigneeCommentText = formatReportLastMessageText(optimisticAssigneeAddComment.reportAction.message[0].text); const optimisticAssigneeReport = { @@ -2861,7 +2852,7 @@ function getTaskAssigneeChatOnyxData(accountID, assignee, assigneeAccountID, tas successData, failureData, optimisticAssigneeAddComment, - optimisticChatCreatedReportAction + optimisticChatCreatedReportAction, }; } diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 595ed9d04034..887fada0bc0e 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -136,7 +136,16 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass ]; if (assigneeChatReport) { - assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData(currentUserAccountID, assignee, assigneeAccountID, taskReportID, assigneeChatReportID, parentReportID, title, assigneeChatReport); + assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData( + currentUserAccountID, + assignee, + assigneeAccountID, + taskReportID, + assigneeChatReportID, + parentReportID, + title, + assigneeChatReport, + ); optimisticData.push(...assigneeChatReportOnyxData.optimisticData); successData.push(...assigneeChatReportOnyxData.successData); failureData.push(...assigneeChatReportOnyxData.failureData); @@ -171,7 +180,6 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass value: {[optimisticAddCommentReport.reportAction.reportActionID]: {pendingAction: null}}, }); - API.write( 'CreateTask', { @@ -184,8 +192,12 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass assignee, assigneeAccountID, assigneeChatReportID, - assigneeChatReportActionID: assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticAssigneeAddComment ? assigneeChatReportOnyxData.optimisticAssigneeAddComment.reportAction.reportActionID : 0, - assigneeChatCreatedReportActionID: assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticChatCreatedReportAction ? assigneeChatReportOnyxData.optimisticChatCreatedReportAction.reportActionID : 0, + assigneeChatReportActionID: + assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticAssigneeAddComment + ? assigneeChatReportOnyxData.optimisticAssigneeAddComment.reportAction.reportActionID + : 0, + assigneeChatCreatedReportActionID: + assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticChatCreatedReportAction ? assigneeChatReportOnyxData.optimisticChatCreatedReportAction.reportActionID : 0, }, {optimisticData, successData, failureData}, ); @@ -394,12 +406,20 @@ function editTaskAndNavigate(report, ownerAccountID, {title, description, assign // If we make a change to the assignee, we want to add a comment to the assignee's chat // Check if the assignee actually changed if (assigneeAccountID && assigneeAccountID !== report.managerID && assigneeAccountID !== ownerAccountID && assigneeChatReport) { - assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData(currentUserAccountID, assignee, assigneeAccountID, report.reportID, assigneeChatReportID, report.parentReportID, reportName, assigneeChatReport); + assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData( + currentUserAccountID, + assignee, + assigneeAccountID, + report.reportID, + assigneeChatReportID, + report.parentReportID, + reportName, + assigneeChatReport, + ); optimisticData.push(...assigneeChatReportOnyxData.optimisticData); successData.push(...assigneeChatReportOnyxData.successData); failureData.push(...assigneeChatReportOnyxData.failureData); } - API.write( 'EditTask', @@ -411,9 +431,12 @@ function editTaskAndNavigate(report, ownerAccountID, {title, description, assign assigneeAccountID: assigneeAccountID || report.managerID, editedTaskReportActionID: editTaskReportAction.reportActionID, assigneeChatReportID, - assigneeChatReportActionID: assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticAssigneeAddComment ? assigneeChatReportOnyxData.optimisticAssigneeAddComment.reportAction.reportActionID : 0, - assigneeChatCreatedReportActionID: assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticChatCreatedReportAction ? assigneeChatReportOnyxData.optimisticChatCreatedReportAction.reportActionID : 0, - + assigneeChatReportActionID: + assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticAssigneeAddComment + ? assigneeChatReportOnyxData.optimisticAssigneeAddComment.reportAction.reportActionID + : 0, + assigneeChatCreatedReportActionID: + assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticChatCreatedReportAction ? assigneeChatReportOnyxData.optimisticChatCreatedReportAction.reportActionID : 0, }, {optimisticData, successData, failureData}, ); diff --git a/src/pages/tasks/TaskAssigneeSelectorModal.js b/src/pages/tasks/TaskAssigneeSelectorModal.js index fd9f70c2c75c..1c502ad58960 100644 --- a/src/pages/tasks/TaskAssigneeSelectorModal.js +++ b/src/pages/tasks/TaskAssigneeSelectorModal.js @@ -170,10 +170,15 @@ function TaskAssigneeSelectorModal(props) { const assigneeChatReport = Task.setAssigneeValue(option.login, option.accountID, props.task.shareDestination, OptionsListUtils.isCurrentUser(option)); // Pass through the selected assignee - Task.editTaskAndNavigate(props.task.report, props.session.accountID, { - assignee: option.login, - assigneeAccountID: option.accountID, - }, assigneeChatReport); + Task.editTaskAndNavigate( + props.task.report, + props.session.accountID, + { + assignee: option.login, + assigneeAccountID: option.accountID, + }, + assigneeChatReport, + ); } }; From 3451c38e9f2d6aca3884b4a714a2cc49fa975a69 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 31 Jul 2023 10:50:43 -0700 Subject: [PATCH 28/34] JSDoc review comments --- src/libs/ReportUtils.js | 6 +++--- src/libs/actions/Task.js | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 8b57165a14f0..c724100439d4 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2777,7 +2777,7 @@ function shouldDisableRename(report, policy) { /** * Returns the onyx data needed for the task assignee chat * @param {Number} accountID - * @param {String} assignee - Assignee email + * @param {String} assigneeEmail * @param {Number} assigneeAccountID * @param {String} taskReportID * @param {String} assigneeChatReportID @@ -2786,7 +2786,7 @@ function shouldDisableRename(report, policy) { * @param {Object} assigneeChatReport * @returns {Object} */ -function getTaskAssigneeChatOnyxData(accountID, assignee, assigneeAccountID, taskReportID, assigneeChatReportID, parentReportID, title, assigneeChatReport) { +function getTaskAssigneeChatOnyxData(accountID, assigneeEmail, assigneeAccountID, taskReportID, assigneeChatReportID, parentReportID, title, assigneeChatReport) { // Set if we need to add a comment to the assignee chat notifying them that they have been assigned a task let optimisticAssigneeAddComment; // Set if this is a new chat that needs to be created for the assignee @@ -2853,7 +2853,7 @@ function getTaskAssigneeChatOnyxData(accountID, assignee, assigneeAccountID, tas // If you're choosing to share the task in the same DM as the assignee then we don't need to create another reportAction indicating that you've been assigned if (assigneeChatReportID !== parentReportID) { - optimisticAssigneeAddComment = buildOptimisticTaskCommentReportAction(taskReportID, title, assignee, assigneeAccountID, `Assigned a task to you: ${title}`, parentReportID); + optimisticAssigneeAddComment = buildOptimisticTaskCommentReportAction(taskReportID, title, assigneeEmail, assigneeAccountID, `Assigned a task to you: ${title}`, parentReportID); const lastAssigneeCommentText = formatReportLastMessageText(optimisticAssigneeAddComment.reportAction.message[0].text); const optimisticAssigneeReport = { diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 887fada0bc0e..a09a13b3f002 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -55,11 +55,11 @@ function clearOutTaskInfo() { * @param {String} parentReportID * @param {String} title * @param {String} description - * @param {String} assignee + * @param {String} assigneeEmail * @param {Number} assigneeAccountID * @param {Object} assigneeChatReport - The chat report between you and the assignee */ -function createTaskAndNavigate(parentReportID, title, description, assignee, assigneeAccountID = 0, assigneeChatReport = null) { +function createTaskAndNavigate(parentReportID, title, description, assigneeEmail, assigneeAccountID = 0, assigneeChatReport = null) { const optimisticTaskReport = ReportUtils.buildOptimisticTaskReport(currentUserAccountID, assigneeAccountID, parentReportID, title, description); const assigneeChatReportID = assigneeChatReport ? assigneeChatReport.reportID : 0; @@ -68,7 +68,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass // Parent ReportAction indicating that a task has been created const optimisticTaskCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(currentUserEmail); - const optimisticAddCommentReport = ReportUtils.buildOptimisticTaskCommentReportAction(taskReportID, title, assignee, assigneeAccountID, `Created a task: ${title}`, parentReportID); + const optimisticAddCommentReport = ReportUtils.buildOptimisticTaskCommentReportAction(taskReportID, title, assigneeEmail, assigneeAccountID, `Created a task: ${title}`, parentReportID); optimisticTaskReport.parentReportActionID = optimisticAddCommentReport.reportAction.reportActionID; const currentTime = DateUtils.getDBTime(); @@ -138,7 +138,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass if (assigneeChatReport) { assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData( currentUserAccountID, - assignee, + assigneeEmail, assigneeAccountID, taskReportID, assigneeChatReportID, @@ -189,7 +189,7 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass createdTaskReportActionID: optimisticTaskCreatedAction.reportActionID, title: optimisticTaskReport.reportName, description: optimisticTaskReport.description, - assignee, + assignee: assigneeEmail, assigneeAccountID, assigneeChatReportID, assigneeChatReportActionID: @@ -357,7 +357,7 @@ function reopenTask(taskReportID, taskTitle) { * @param {String} editedTask.description * @param {String} editedTask.assignee * @param {Number} editedTask.assigneeAccountID - * @param {Object} assigneeChatReport + * @param {Object} assigneeChatReport - The chat report between you and the assignee */ function editTaskAndNavigate(report, ownerAccountID, {title, description, assignee = '', assigneeAccountID = 0}, assigneeChatReport = null) { // Create the EditedReportAction on the task From 6e5b6e7cbb61f7f5ac7c32cbf3c207e88372f061 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 31 Jul 2023 15:26:29 -0700 Subject: [PATCH 29/34] Use getChatByParticipantsByLoginList --- src/libs/actions/Task.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index a09a13b3f002..ac1a11d19cb0 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -499,23 +499,18 @@ function setAssigneeChatReport(chatReport) { * 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 * It also sets the shareDestination as that chat report if a share destination isn't already set - * @param {string} assignee + * @param {string} assigneeEmail * @param {Number} assigneeAccountID * @param {string} shareDestination * @param {boolean} isCurrentUser */ -function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurrentUser = false) { +function setAssigneeValue(assigneeEmail, assigneeAccountID, shareDestination, isCurrentUser = false) { let newAssigneeAccountID = Number(assigneeAccountID); let chatReport; - // Generate optimistic accountID if this is a brand new user account that hasn't been created yet - if (!newAssigneeAccountID) { - newAssigneeAccountID = UserUtils.generateAccountID(assignee); - } - if (!isCurrentUser) { - chatReport = ReportUtils.getChatByParticipants([newAssigneeAccountID]); + chatReport = ReportUtils.getChatByParticipantsByLoginList([assigneeEmail]); if (!chatReport) { chatReport = ReportUtils.buildOptimisticChatReport([newAssigneeAccountID]); chatReport.isOptimisticReport = true; @@ -531,8 +526,8 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre const optimisticPersonalDetailsListAction = { accountID: newAssigneeAccountID, avatar: lodashGet(allPersonalDetails, [newAssigneeAccountID, 'avatar'], UserUtils.getDefaultAvatarURL(newAssigneeAccountID)), - displayName: lodashGet(allPersonalDetails, [newAssigneeAccountID, 'displayName'], assignee), - login: assignee, + displayName: lodashGet(allPersonalDetails, [newAssigneeAccountID, 'displayName'], assigneeEmail), + login: assigneeEmail, }; Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[newAssigneeAccountID]: optimisticPersonalDetailsListAction}); } @@ -547,7 +542,7 @@ function setAssigneeValue(assignee, assigneeAccountID, shareDestination, isCurre } // This is only needed for creation of a new task and so it should only be stored locally - Onyx.merge(ONYXKEYS.TASK, {assignee, assigneeAccountID: newAssigneeAccountID}); + Onyx.merge(ONYXKEYS.TASK, {assignee: assigneeEmail, assigneeAccountID: newAssigneeAccountID}); // When we're editing the assignee, we immediately call EditTaskAndNavigate. Since setting the assignee is async, // the chatReport is not yet set when EditTaskAndNavigate is called. So we return the chatReport here so that From b50ef1e81b7d3a2f46b3549c6bc34e02c2fc0d84 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Mon, 31 Jul 2023 15:55:18 -0700 Subject: [PATCH 30/34] clean up --- src/libs/actions/Task.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index ac1a11d19cb0..30fa4fde753c 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -506,13 +506,12 @@ function setAssigneeChatReport(chatReport) { */ function setAssigneeValue(assigneeEmail, assigneeAccountID, shareDestination, isCurrentUser = false) { - let newAssigneeAccountID = Number(assigneeAccountID); let chatReport; if (!isCurrentUser) { chatReport = ReportUtils.getChatByParticipantsByLoginList([assigneeEmail]); if (!chatReport) { - chatReport = ReportUtils.buildOptimisticChatReport([newAssigneeAccountID]); + chatReport = ReportUtils.buildOptimisticChatReport([assigneeAccountID]); chatReport.isOptimisticReport = true; // When assigning a task to a new user, by default we share the task in their DM @@ -524,12 +523,12 @@ function setAssigneeValue(assigneeEmail, assigneeAccountID, shareDestination, is // 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 = { - accountID: newAssigneeAccountID, - avatar: lodashGet(allPersonalDetails, [newAssigneeAccountID, 'avatar'], UserUtils.getDefaultAvatarURL(newAssigneeAccountID)), - displayName: lodashGet(allPersonalDetails, [newAssigneeAccountID, 'displayName'], assigneeEmail), + accountID: assigneeAccountID, + avatar: lodashGet(allPersonalDetails, [assigneeAccountID, 'avatar'], UserUtils.getDefaultAvatarURL(assigneeAccountID)), + displayName: lodashGet(allPersonalDetails, [assigneeAccountID, 'displayName'], assigneeEmail), login: assigneeEmail, }; - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[newAssigneeAccountID]: optimisticPersonalDetailsListAction}); + Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[assigneeAccountID]: optimisticPersonalDetailsListAction}); } setAssigneeChatReport(chatReport); @@ -542,7 +541,7 @@ function setAssigneeValue(assigneeEmail, assigneeAccountID, shareDestination, is } // This is only needed for creation of a new task and so it should only be stored locally - Onyx.merge(ONYXKEYS.TASK, {assignee: assigneeEmail, assigneeAccountID: newAssigneeAccountID}); + Onyx.merge(ONYXKEYS.TASK, {assignee: assigneeEmail, assigneeAccountID}); // When we're editing the assignee, we immediately call EditTaskAndNavigate. Since setting the assignee is async, // the chatReport is not yet set when EditTaskAndNavigate is called. So we return the chatReport here so that From 33e59daf8b84dd3fcc658c4229e9d20ef7a778ac Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Tue, 1 Aug 2023 15:22:45 -0700 Subject: [PATCH 31/34] remove editedTask --- src/libs/actions/Task.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index f5f1899027f8..e2ae82624c64 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -353,10 +353,6 @@ function reopenTask(taskReportID, taskTitle) { * @param {object} report * @param {Number} ownerAccountID * @param {Object} editedTask - * @param {String} editedTask.title - * @param {String} editedTask.description - * @param {String} editedTask.assignee - * @param {Number} editedTask.assigneeAccountID * @param {Object} assigneeChatReport - The chat report between you and the assignee */ function editTaskAndNavigate(report, ownerAccountID, {title, description, assignee = '', assigneeAccountID = 0}, assigneeChatReport = null) { From 002fa5dff7f7313af9856302da31289cb9dd6e70 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Tue, 1 Aug 2023 16:50:58 -0700 Subject: [PATCH 32/34] fallback to assigneeAccountID --- src/libs/actions/Task.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index e2ae82624c64..b5af61496d7f 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -505,7 +505,7 @@ function setAssigneeValue(assigneeEmail, assigneeAccountID, shareDestination, is let chatReport; if (!isCurrentUser) { - chatReport = ReportUtils.getChatByParticipantsByLoginList([assigneeEmail]); + chatReport = ReportUtils.getChatByParticipantsByLoginList([assigneeEmail]) || ReportUtils.getChatByParticipants([assigneeAccountID]); if (!chatReport) { chatReport = ReportUtils.buildOptimisticChatReport([assigneeAccountID]); chatReport.isOptimisticReport = true; From 92499de01b0433f6c3d4d21333da3244e74cfc1f Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Wed, 2 Aug 2023 13:02:08 -0700 Subject: [PATCH 33/34] Start using EditTaskAssignee --- src/libs/actions/Task.js | 78 ++++++++++++++++++++ src/pages/tasks/TaskAssigneeSelectorModal.js | 10 +-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 35c9f375af8d..291de4457342 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -440,6 +440,83 @@ function editTaskAndNavigate(report, ownerAccountID, {title, description, assign Navigation.dismissModal(report.reportID); } +function editTaskAssigneeAndNavigate(report, ownerAccountID, assigneeEmail, assigneeAccountID = 0, assigneeChatReport = null) { + // Create the EditedReportAction on the task + const editTaskReportAction = ReportUtils.buildOptimisticEditedTaskReportAction(currentUserEmail); + const reportName = report.reportName.trim(); + + let assigneeChatReportOnyxData; + const assigneeChatReportID = assigneeChatReport ? assigneeChatReport.reportID : 0; + + const optimisticData = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, + value: {[editTaskReportAction.reportActionID]: editTaskReportAction}, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, + value: { + reportName, + managerID: assigneeAccountID || report.managerID, + managerEmail: assigneeEmail || report.managerEmail, + }, + }, + ]; + const successData = []; + const failureData = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, + value: {[editTaskReportAction.reportActionID]: {pendingAction: null}}, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, + value: {assignee: report.managerEmail, assigneeAccountID: report.managerID}, + }, + ]; + + // If we make a change to the assignee, we want to add a comment to the assignee's chat + // Check if the assignee actually changed + if (assigneeAccountID && assigneeAccountID !== report.managerID && assigneeAccountID !== ownerAccountID && assigneeChatReport) { + assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData( + currentUserAccountID, + assigneeEmail, + assigneeAccountID, + report.reportID, + assigneeChatReportID, + report.parentReportID, + reportName, + assigneeChatReport, + ); + optimisticData.push(...assigneeChatReportOnyxData.optimisticData); + successData.push(...assigneeChatReportOnyxData.successData); + failureData.push(...assigneeChatReportOnyxData.failureData); + } + + API.write( + 'EditTaskAssignee', + { + taskReportID: report.reportID, + assignee: assigneeEmail || report.managerEmail, + assigneeAccountID: assigneeAccountID || report.managerID, + editedTaskReportActionID: editTaskReportAction.reportActionID, + assigneeChatReportID, + assigneeChatReportActionID: + assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticAssigneeAddComment + ? assigneeChatReportOnyxData.optimisticAssigneeAddComment.reportAction.reportActionID + : 0, + assigneeChatCreatedReportActionID: + assigneeChatReportOnyxData && assigneeChatReportOnyxData.optimisticChatCreatedReportAction ? assigneeChatReportOnyxData.optimisticChatCreatedReportAction.reportActionID : 0, + }, + {optimisticData, successData, failureData}, + ); + + Navigation.dismissModal(report.reportID); +} + /** * Sets the report info for the task being viewed * @@ -739,6 +816,7 @@ function canModifyTask(taskReport, sessionAccountID) { export { createTaskAndNavigate, editTaskAndNavigate, + editTaskAssigneeAndNavigate, setTitleValue, setDescriptionValue, setTaskReport, diff --git a/src/pages/tasks/TaskAssigneeSelectorModal.js b/src/pages/tasks/TaskAssigneeSelectorModal.js index 1c502ad58960..e6aba1f74c83 100644 --- a/src/pages/tasks/TaskAssigneeSelectorModal.js +++ b/src/pages/tasks/TaskAssigneeSelectorModal.js @@ -170,15 +170,13 @@ function TaskAssigneeSelectorModal(props) { const assigneeChatReport = Task.setAssigneeValue(option.login, option.accountID, props.task.shareDestination, OptionsListUtils.isCurrentUser(option)); // Pass through the selected assignee - Task.editTaskAndNavigate( + Task.editTaskAssigneeAndNavigate( props.task.report, props.session.accountID, - { - assignee: option.login, - assigneeAccountID: option.accountID, - }, + option.login, + option.accountID, assigneeChatReport, - ); + ) } }; From a884a43ab1f9af128175ef05fabc9e580d7a8a69 Mon Sep 17 00:00:00 2001 From: Jack Nam Date: Wed, 9 Aug 2023 10:58:42 -0700 Subject: [PATCH 34/34] Update TaskAssigneeSelectorModal.js --- src/pages/tasks/TaskAssigneeSelectorModal.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/pages/tasks/TaskAssigneeSelectorModal.js b/src/pages/tasks/TaskAssigneeSelectorModal.js index 3b39d56bae1f..245d00f3d0f7 100644 --- a/src/pages/tasks/TaskAssigneeSelectorModal.js +++ b/src/pages/tasks/TaskAssigneeSelectorModal.js @@ -175,13 +175,7 @@ function TaskAssigneeSelectorModal(props) { const assigneeChatReport = Task.setAssigneeValue(option.login, option.accountID, props.route.params.reportID, OptionsListUtils.isCurrentUser(option)); // Pass through the selected assignee - Task.editTaskAssigneeAndNavigate( - props.task.report, - props.session.accountID, - option.login, - option.accountID, - assigneeChatReport, - ) + Task.editTaskAssigneeAndNavigate(props.task.report, props.session.accountID, option.login, option.accountID, assigneeChatReport); } };