From 9870988d3cbe3f25d93e57fc5baecf751f0e4677 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Tue, 27 Feb 2024 11:20:36 -0700 Subject: [PATCH 1/3] Refactor and simplify delete task --- src/libs/actions/Task.ts | 59 ++++++++++++++++++++++++++---------- src/pages/home/HeaderView.js | 2 +- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 74e6a6c78eebf..4118411a18a02 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -5,6 +5,7 @@ import * as Expensicons from '@components/Icon/Expensicons'; import * as API from '@libs/API'; import type {CancelTaskParams, CompleteTaskParams, CreateTaskParams, EditTaskAssigneeParams, EditTaskParams, ReopenTaskParams} from '@libs/API/parameters'; import {WRITE_COMMANDS} from '@libs/API/types'; +import * as CollectionUtils from '@libs/CollectionUtils'; import DateUtils from '@libs/DateUtils'; import * as ErrorUtils from '@libs/ErrorUtils'; import * as LocalePhoneNumber from '@libs/LocalePhoneNumber'; @@ -20,6 +21,8 @@ import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type * as OnyxTypes from '@src/types/onyx'; import type {Icon} from '@src/types/onyx/OnyxCommon'; +import type {ReportActions} from '@src/types/onyx/ReportAction'; +import type ReportAction from '@src/types/onyx/ReportAction'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import * as Report from './Report'; @@ -55,6 +58,19 @@ Onyx.connect({ callback: (value) => (allPersonalDetails = value), }); +const allReportActions: OnyxCollection = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + callback: (actions, key) => { + if (!key || !actions) { + return; + } + + const reportID = CollectionUtils.extractCollectionItemID(key); + allReportActions[reportID] = actions; + }, +}); + /** * Clears out the task info from the store */ @@ -703,19 +719,30 @@ function getShareDestination(reportID: string, reports: OnyxCollection): ReportAction | Record { + if (!report?.parentReportID || !report.parentReportActionID) { + return {}; + } + return allReportActions?.[report.parentReportID]?.[report.parentReportActionID] ?? {}; +} + /** * Cancels a task by setting the report state to SUBMITTED and status to CLOSED */ -function deleteTask(taskReportID: string, taskTitle: string, originalStateNum: number, originalStatusNum: number) { - const message = `deleted task: ${taskTitle}`; - const optimisticCancelReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASKCANCELLED, message); +function deleteTask(report: OnyxEntry) { + const message = `deleted task: ${report?.reportName}`; + const optimisticCancelReportAction = ReportUtils.buildOptimisticTaskReportAction(report?.reportID ?? '', CONST.REPORT.ACTIONS.TYPE.TASKCANCELLED, message); const optimisticReportActionID = optimisticCancelReportAction.reportActionID; - const taskReport = ReportUtils.getReport(taskReportID); - const parentReportAction = ReportActionsUtils.getParentReportAction(taskReport); - const parentReport = ReportUtils.getParentReport(taskReport); + const parentReportAction = getParentReportAction(report); + const parentReport = ReportUtils.getParentReport(report); // 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 shouldDeleteTaskReport = !ReportActionsUtils.doesReportHaveVisibleActions(report?.reportID ?? ''); const optimisticReportAction: Partial = { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, previousMessage: parentReportAction.message, @@ -739,7 +766,7 @@ function deleteTask(taskReportID: string, taskTitle: string, originalStateNum: n const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${report?.reportID}`, value: { lastVisibleActionCreated: optimisticCancelReportAction.created, lastMessageText: message, @@ -757,7 +784,7 @@ function deleteTask(taskReportID: string, taskTitle: string, originalStateNum: n }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.reportID}`, value: { [optimisticReportActionID]: optimisticCancelReportAction as OnyxTypes.ReportAction, }, @@ -785,7 +812,7 @@ function deleteTask(taskReportID: string, taskTitle: string, originalStateNum: n const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.reportID}`, value: { [optimisticReportActionID]: { pendingAction: null, @@ -806,15 +833,15 @@ function deleteTask(taskReportID: string, taskTitle: string, originalStateNum: n const failureData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${report?.reportID}`, value: { - stateNum: originalStateNum, - statusNum: originalStatusNum, + stateNum: report?.stateNum ?? '', + statusNum: report?.statusNum ?? '', } as OnyxTypes.Report, }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${taskReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.reportID}`, value: { [optimisticReportActionID]: null, }, @@ -832,7 +859,7 @@ function deleteTask(taskReportID: string, taskTitle: string, originalStateNum: n const parameters: CancelTaskParams = { cancelledTaskReportActionID: optimisticReportActionID, - taskReportID, + taskReportID: report?.reportID, }; API.write(WRITE_COMMANDS.CANCEL_TASK, parameters, {optimisticData, successData, failureData}); @@ -862,7 +889,7 @@ function getTaskAssigneeAccountID(taskReport: OnyxEntry): numb return taskReport.managerID; } - const reportAction = ReportActionsUtils.getParentReportAction(taskReport); + const reportAction = getParentReportAction(taskReport); return reportAction.childManagerAccountID; } diff --git a/src/pages/home/HeaderView.js b/src/pages/home/HeaderView.js index faa70bb0633a2..4d6e396b7c3f8 100644 --- a/src/pages/home/HeaderView.js +++ b/src/pages/home/HeaderView.js @@ -371,7 +371,7 @@ function HeaderView(props) { isVisible={isDeleteTaskConfirmModalVisible} onConfirm={() => { setIsDeleteTaskConfirmModalVisible(false); - Session.checkIfActionIsAllowed(Task.deleteTask(props.reportID, props.report.reportName, props.report.stateNum, props.report.statusNum)); + Session.checkIfActionIsAllowed(Task.deleteTask(props.report)); }} onCancel={() => setIsDeleteTaskConfirmModalVisible(false)} title={translate('task.deleteTask')} From b47c44a91e64e7b9fe59449d41fd93af11fb9ff8 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Wed, 28 Feb 2024 15:26:34 -0700 Subject: [PATCH 2/3] Improve comments --- src/libs/actions/Task.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 4118411a18a02..e062b1ec9dc64 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -721,10 +721,9 @@ function getShareDestination(reportID: string, reports: OnyxCollection): ReportAction | Record { + // If the report is not a thread report, then it won't have a parent and an empty object can be returned. if (!report?.parentReportID || !report.parentReportActionID) { return {}; } From dc3aef27b9b90e9831dcf07f49042e300276754b Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 29 Feb 2024 12:09:40 -0700 Subject: [PATCH 3/3] Remove oiptional chaining --- src/libs/actions/Task.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index e062b1ec9dc64..06af31402a82e 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -734,14 +734,17 @@ function getParentReportAction(report: OnyxEntry): ReportActio * Cancels a task by setting the report state to SUBMITTED and status to CLOSED */ function deleteTask(report: OnyxEntry) { - const message = `deleted task: ${report?.reportName}`; - const optimisticCancelReportAction = ReportUtils.buildOptimisticTaskReportAction(report?.reportID ?? '', CONST.REPORT.ACTIONS.TYPE.TASKCANCELLED, message); + if (!report) { + return; + } + const message = `deleted task: ${report.reportName}`; + const optimisticCancelReportAction = ReportUtils.buildOptimisticTaskReportAction(report.reportID ?? '', CONST.REPORT.ACTIONS.TYPE.TASKCANCELLED, message); const optimisticReportActionID = optimisticCancelReportAction.reportActionID; const parentReportAction = getParentReportAction(report); const parentReport = ReportUtils.getParentReport(report); // 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(report?.reportID ?? ''); + const shouldDeleteTaskReport = !ReportActionsUtils.doesReportHaveVisibleActions(report.reportID ?? ''); const optimisticReportAction: Partial = { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, previousMessage: parentReportAction.message, @@ -765,7 +768,7 @@ function deleteTask(report: OnyxEntry) { const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${report?.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, value: { lastVisibleActionCreated: optimisticCancelReportAction.created, lastMessageText: message, @@ -783,7 +786,7 @@ function deleteTask(report: OnyxEntry) { }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, value: { [optimisticReportActionID]: optimisticCancelReportAction as OnyxTypes.ReportAction, }, @@ -811,7 +814,7 @@ function deleteTask(report: OnyxEntry) { const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, value: { [optimisticReportActionID]: { pendingAction: null, @@ -832,15 +835,15 @@ function deleteTask(report: OnyxEntry) { const failureData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${report?.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, value: { - stateNum: report?.stateNum ?? '', - statusNum: report?.statusNum ?? '', + stateNum: report.stateNum ?? '', + statusNum: report.statusNum ?? '', } as OnyxTypes.Report, }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, value: { [optimisticReportActionID]: null, }, @@ -858,7 +861,7 @@ function deleteTask(report: OnyxEntry) { const parameters: CancelTaskParams = { cancelledTaskReportActionID: optimisticReportActionID, - taskReportID: report?.reportID, + taskReportID: report.reportID, }; API.write(WRITE_COMMANDS.CANCEL_TASK, parameters, {optimisticData, successData, failureData});