diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 4847eee2c8c6..bef9e391f099 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -640,6 +640,19 @@ function isTaskAction(reportAction: OnyxEntry): boolean { ); } +/** + * When we delete certain reports, we want to check whether there are any visible actions left to display. + * If there are no visible actions left (including system messages), we can hide the report from view entirely + */ +function doesReportHaveVisibleActions(reportID: string, actionsToMerge: ReportActions = {}): boolean { + const reportActions = Object.values(OnyxUtils.fastMerge(allReportActions?.[reportID] ?? {}, actionsToMerge)); + const visibleReportActions = Object.values(reportActions ?? {}).filter((action) => shouldReportActionBeVisibleAsLastAction(action)); + + // Exclude the task system message and the created message + const visibleReportActionsWithoutTaskSystemMessage = visibleReportActions.filter((action) => !isTaskAction(action) && !isCreatedAction(action)); + return visibleReportActionsWithoutTaskSystemMessage.length > 0; +} + function getAllReportActions(reportID: string): ReportActions { return allReportActions?.[reportID] ?? {}; } @@ -811,6 +824,7 @@ export { isSentMoneyReportAction, isSplitBillAction, isTaskAction, + doesReportHaveVisibleActions, isThreadParentMessage, isTransactionThread, isWhisperAction, diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 678e1fb967dd..31cfd96c0bd3 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -713,7 +713,7 @@ function getShareDestination(reportID, reports, personalDetails) { * @param {number} originalStateNum * @param {number} originalStatusNum */ -function cancelTask(taskReportID, taskTitle, originalStateNum, originalStatusNum) { +function deleteTask(taskReportID, taskTitle, originalStateNum, originalStatusNum) { const message = `deleted task: ${taskTitle}`; const optimisticCancelReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASKCANCELLED, message); const optimisticReportActionID = optimisticCancelReportAction.reportActionID; @@ -721,6 +721,9 @@ function cancelTask(taskReportID, taskTitle, originalStateNum, originalStatusNum const parentReportAction = ReportActionsUtils.getParentReportAction(taskReport); const parentReport = ReportUtils.getParentReport(taskReport); + // If the task report is the last visible action in the parent report, we should navigate back to the parent report + const shouldDeleteTaskReport = !ReportActionsUtils.doesReportHaveVisibleActions(taskReportID); + const optimisticReportActions = { [parentReportAction.reportActionID]: { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, @@ -822,6 +825,10 @@ function cancelTask(taskReportID, taskTitle, originalStateNum, originalStatusNum ]; API.write('CancelTask', {cancelledTaskReportActionID: optimisticReportActionID, taskReportID}, {optimisticData, successData, failureData}); + + if (shouldDeleteTaskReport) { + Navigation.goBack(ROUTES.REPORT_WITH_ID.getRoute(parentReport.reportID)); + } } /** @@ -928,7 +935,7 @@ export { clearOutTaskInfoAndNavigate, getAssignee, getShareDestination, - cancelTask, + deleteTask, dismissModalAndClearOutTaskInfo, getTaskAssigneeAccountID, clearTaskErrors, diff --git a/src/pages/home/HeaderView.js b/src/pages/home/HeaderView.js index 6c5a3ac4843f..edf6b65b2f4a 100644 --- a/src/pages/home/HeaderView.js +++ b/src/pages/home/HeaderView.js @@ -136,8 +136,8 @@ function HeaderView(props) { if (props.report.stateNum !== CONST.REPORT.STATE_NUM.SUBMITTED && props.report.statusNum !== CONST.REPORT.STATUS.CLOSED && canModifyTask) { threeDotMenuItems.push({ icon: Expensicons.Trashcan, - text: translate('common.cancel'), - onSelected: Session.checkIfActionIsAllowed(() => Task.cancelTask(props.report.reportID, props.report.reportName, props.report.stateNum, props.report.statusNum)), + text: translate('common.delete'), + onSelected: Session.checkIfActionIsAllowed(() => Task.deleteTask(props.report.reportID, props.report.reportName, props.report.stateNum, props.report.statusNum)), }); } } diff --git a/src/pages/tasks/TaskAssigneeSelectorModal.js b/src/pages/tasks/TaskAssigneeSelectorModal.js index 39b585438413..a4fc61910be2 100644 --- a/src/pages/tasks/TaskAssigneeSelectorModal.js +++ b/src/pages/tasks/TaskAssigneeSelectorModal.js @@ -104,7 +104,7 @@ function TaskAssigneeSelectorModal(props) { false, {}, [], - false, + true, ); setHeaderMessage(OptionsListUtils.getHeaderMessage(recentReports?.length + personalDetails?.length !== 0 || currentUserOption, Boolean(userToInvite), searchValue));