Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update parent action for assignee report as well #24110

Merged
merged 18 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/ReportActionItem/TaskPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ function TaskPreview(props) {
disabled={ReportUtils.isCanceledTaskReport(props.taskReport)}
onPress={() => {
if (isTaskCompleted) {
Task.reopenTask(props.taskReportID, taskTitle);
Task.reopenTask(props.taskReport, taskTitle);
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
techievivek marked this conversation as resolved.
Show resolved Hide resolved
} else {
Task.completeTask(props.taskReportID, taskTitle);
Task.completeTask(props.taskReport, taskTitle);
}
}}
accessibilityLabel={props.translate('task.task')}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ReportActionItem/TaskView.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function TaskView(props) {
<Text style={styles.taskTitleDescription}>{props.translate('task.title')}</Text>
<View style={[styles.flexRow, styles.alignItemsTop, styles.flex1]}>
<Checkbox
onPress={() => (isCompleted ? Task.reopenTask(props.report.reportID, taskTitle) : Task.completeTask(props.report.reportID, taskTitle))}
onPress={() => (isCompleted ? Task.reopenTask(props.report, taskTitle) : Task.completeTask(props.report, taskTitle))}
isChecked={isCompleted}
style={styles.taskMenuItemCheckbox}
containerSize={24}
Expand Down
15 changes: 15 additions & 0 deletions src/libs/ReportActionsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,20 @@ function getIOUReportIDFromReportActionPreview(reportAction) {
return lodashGet(reportAction, 'originalMessage.linkedReportID', '');
}

/**
* Find the reportAction whose childReportID is taskReportID in reportActions with reportID
*
* @param {String} taskReportID
* @param {String} reportID
* @returns {Object}
*/
function getParentReportActionForTask(taskReportID, reportID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is not exclusive to tasks. Can we rename it to something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to getParentReportActionInReport

return _.find(
allReportActions[reportID],
(reportAction) => reportAction && `${reportAction.childReportID}` === `${taskReportID}` && reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
);
}

function isCreatedTaskReportAction(reportAction) {
return reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT && _.has(reportAction.originalMessage, 'taskReportID');
}
Expand Down Expand Up @@ -593,4 +607,5 @@ export {
isWhisperAction,
isPendingRemove,
getReportAction,
getParentReportActionForTask,
};
1 change: 1 addition & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,7 @@ function buildOptimisticTaskReport(ownerAccountID, assigneeAccountID = 0, parent
description,
ownerAccountID,
managerID: assigneeAccountID,
participantAccountIDs: [assigneeAccountID],
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
type: CONST.REPORT.TYPE.TASK,
parentReportID,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
Expand Down
65 changes: 58 additions & 7 deletions src/libs/actions/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ function createTaskAndNavigate(parentReportID, title, description, assignee, ass
Navigation.dismissModal(optimisticTaskReport.reportID);
}

function completeTask(taskReportID, taskTitle) {
/**
* Complete a task
* @param {Object} taskReport task report
* @param {String} taskTitle Title of the task
*/
function completeTask(taskReport, taskTitle) {
const taskReportID = taskReport.reportID;
const message = `completed task: ${taskTitle}`;
const completedTaskReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASKCOMPLETED, message);

Expand Down Expand Up @@ -259,10 +265,32 @@ function completeTask(taskReportID, taskTitle) {
},
];

// Update optimistic data for parent report action
// Update optimistic data and failure data for parent report action
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(taskReportID, completedTaskReportAction.created, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
if (!_.isEmpty(optimisticParentReportData)) {
optimisticData.push(optimisticParentReportData);
const assigneeChatReportID = lodashGet(ReportUtils.getChatByParticipants(taskReport.participantAccountIDs), 'reportID');
if (assigneeChatReportID && `${assigneeChatReportID}` !== `${taskReport.parentReportID}`) {
const parentReportAction = ReportActionsUtils.getParentReportActionForTask(taskReportID, assigneeChatReportID);
if (parentReportAction) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`,
value: {
[parentReportAction.reportActionID]: _.values(optimisticParentReportData.value)[0],
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`,
value: {
[parentReportAction.reportActionID]: {
errors: ErrorUtils.getMicroSecondOnyxError('task.messages.error'),
},
},
});
}
}
}

API.write(
Expand All @@ -276,11 +304,12 @@ function completeTask(taskReportID, taskTitle) {
}

/**
* Reopens a closed task
* @param {string} taskReportID ReportID of the task
* @param {string} taskTitle Title of the task
* Reopen a closed task
* @param {Object} taskReport task report
* @param {String} taskTitle Title of the task
*/
function reopenTask(taskReportID, taskTitle) {
function reopenTask(taskReport, taskTitle) {
const taskReportID = taskReport.reportID;
const message = `reopened task: ${taskTitle}`;
const reopenedTaskReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASKREOPENED, message);

Expand Down Expand Up @@ -335,10 +364,32 @@ function reopenTask(taskReportID, taskTitle) {
},
];

// Update optimistic data for parent report action
// Update optimistic data and failure data for parent report action
const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(taskReportID, reopenedTaskReportAction.created, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
if (!_.isEmpty(optimisticParentReportData)) {
optimisticData.push(optimisticParentReportData);
const assigneeChatReportID = lodashGet(ReportUtils.getChatByParticipants(taskReport.participantAccountIDs), 'reportID');
if (assigneeChatReportID && `${assigneeChatReportID}` !== `${taskReport.parentReportID}`) {
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
const parentReportAction = ReportActionsUtils.getParentReportActionForTask(taskReportID, assigneeChatReportID);
if (parentReportAction) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`,
value: {
[parentReportAction.reportActionID]: _.values(optimisticParentReportData.value)[0],
},
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`,
value: {
[parentReportAction.reportActionID]: {
errors: ErrorUtils.getMicroSecondOnyxError('task.messages.error'),
},
},
});
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

API.write(
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/HeaderView.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function HeaderView(props) {
{!_.isEmpty(parentNavigationSubtitleData) && (
<ParentNavigationSubtitle
parentNavigationSubtitleData={parentNavigationSubtitleData}
parentReportID={props.report.parentReportID}
parentReportID={`${props.report.parentReportID}`}
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
pressableStyles={[styles.alignSelfStart, styles.mw100]}
/>
)}
Expand Down