Skip to content

Commit

Permalink
Build optimistic Edit Task actions
Browse files Browse the repository at this point in the history
  • Loading branch information
paultsimura committed Feb 19, 2024
1 parent 91ea979 commit aefde12
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,8 @@ function getLastMessageTextForReport(report: OnyxEntry<Report>, lastActorDetails
} else if (
lastActionName === CONST.REPORT.ACTIONS.TYPE.TASKCOMPLETED ||
lastActionName === CONST.REPORT.ACTIONS.TYPE.TASKREOPENED ||
lastActionName === CONST.REPORT.ACTIONS.TYPE.TASKCANCELLED
lastActionName === CONST.REPORT.ACTIONS.TYPE.TASKCANCELLED ||
lastActionName === CONST.REPORT.ACTIONS.TYPE.TASKEDITED
) {
lastMessageTextFromReport = lastReportAction?.message?.[0].text ?? '';
} else if (ReportActionUtils.isCreatedTaskReportAction(lastReportAction)) {
Expand Down
4 changes: 0 additions & 4 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,6 @@ function shouldReportActionBeVisible(reportAction: OnyxEntry<ReportAction>, key:
return false;
}

if (reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.TASKEDITED) {
return false;
}

// Filter out any unsupported reportAction types
if (!supportedActionTypes.includes(reportAction.actionName)) {
return false;
Expand Down
57 changes: 48 additions & 9 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
ReportAction,
ReportMetadata,
Session,
Task,
Transaction,
TransactionViolation,
} from '@src/types/onyx';
Expand Down Expand Up @@ -3568,25 +3569,62 @@ function buildOptimisticUnHoldReportAction(created = DateUtils.getDBTime()): Opt
};
}

/**
* Returns the necessary reportAction onyx data to indicate that a task report has been edited
*/
function buildOptimisticEditedTaskReportAction(emailEditingTask: string): OptimisticEditedTaskReportAction {
function buildOptimisticEditedTaskFieldReportAction({title, description}: Task): OptimisticEditedTaskReportAction {
// We do not modify title & description in one request, so we need to create a different optimistic action for each field modification
let field = '';
let value = '';
if (title !== undefined) {
field = 'task title';
value = title;
} else if (description !== undefined) {
field = 'description';
value = description;
}

let changelog = 'edited this task';
if (field && value) {
changelog = `updated the ${field} to ${value}`;
} else if (field) {
changelog = `removed the ${field}`;
}

return {
reportActionID: NumberUtils.rand64(),
actionName: CONST.REPORT.ACTIONS.TYPE.TASKEDITED,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
actorAccountID: currentUserAccountID,
message: [
{
type: CONST.REPORT.MESSAGE.TYPE.COMMENT,
text: changelog,
html: changelog,
},
],
person: [
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
style: 'strong',
text: emailEditingTask,
text: allPersonalDetails?.[currentUserAccountID ?? '']?.displayName ?? currentUserEmail,
},
],
automatic: false,
avatar: getCurrentUserAvatarOrDefault(),
created: DateUtils.getDBTime(),
shouldShow: false,
};
}

function buildOptimisticChangedTaskAssigneeReportAction(assigneeAccountID: number): OptimisticEditedTaskReportAction {
return {
reportActionID: NumberUtils.rand64(),
actionName: CONST.REPORT.ACTIONS.TYPE.TASKEDITED,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
actorAccountID: currentUserAccountID,
message: [
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
style: 'normal',
text: ' edited this task',
type: CONST.REPORT.MESSAGE.TYPE.COMMENT,
text: `assigned to ${getDisplayNameForParticipant(assigneeAccountID)}`,
html: `assigned to <mention-user accountID=${assigneeAccountID}></mention-user>`,
},
],
person: [
Expand Down Expand Up @@ -5077,7 +5115,8 @@ export {
buildOptimisticClosedReportAction,
buildOptimisticCreatedReportAction,
buildOptimisticRenamedRoomReportAction,
buildOptimisticEditedTaskReportAction,
buildOptimisticEditedTaskFieldReportAction,
buildOptimisticChangedTaskAssigneeReportAction,
buildOptimisticIOUReport,
buildOptimisticApprovedReportAction,
buildOptimisticMovedReportAction,
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ function reopenTask(taskReport: OnyxEntry<OnyxTypes.Report>) {

function editTask(report: OnyxTypes.Report, {title, description}: OnyxTypes.Task) {
// Create the EditedReportAction on the task
const editTaskReportAction = ReportUtils.buildOptimisticEditedTaskReportAction(currentUserEmail);
const editTaskReportAction = ReportUtils.buildOptimisticEditedTaskFieldReportAction({title, description});

// Sometimes title or description is undefined, so we need to check for that, and we provide it to multiple functions
const reportName = (title ?? report?.reportName).trim();
Expand Down Expand Up @@ -453,7 +453,7 @@ function editTask(report: OnyxTypes.Report, {title, description}: OnyxTypes.Task

function editTaskAssignee(report: OnyxTypes.Report, ownerAccountID: number, assigneeEmail: string, assigneeAccountID = 0, assigneeChatReport: OnyxEntry<OnyxTypes.Report> = null) {
// Create the EditedReportAction on the task
const editTaskReportAction = ReportUtils.buildOptimisticEditedTaskReportAction(currentUserEmail);
const editTaskReportAction = ReportUtils.buildOptimisticChangedTaskAssigneeReportAction(assigneeAccountID);
const reportName = report.reportName?.trim();

let assigneeChatReportOnyxData;
Expand Down

0 comments on commit aefde12

Please sign in to comment.