-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Remove the side-loading of data in Task action from the ReportActionUtils #37340
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
Changes from all commits
9870988
b47c44a
dc3aef2
f1dac85
d172d48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<ReportActions> = {}; | ||
| 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,32 @@ function getShareDestination(reportID: string, reports: OnyxCollection<OnyxTypes | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the parentReportAction if the given report is a thread/task. | ||
| */ | ||
| function getParentReportAction(report: OnyxEntry<OnyxTypes.Report>): ReportAction | Record<string, never> { | ||
| // 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to check for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment added (but it's kind of self-explanatory too when you look at the next line and see how those properties are used). |
||
| 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<OnyxTypes.Report>) { | ||
| 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 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<ReportUtils.OptimisticTaskReportAction> = { | ||
| pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, | ||
| previousMessage: parentReportAction.message, | ||
|
|
@@ -739,7 +768,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 +786,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 +814,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 +835,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 +861,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 +891,7 @@ function getTaskAssigneeAccountID(taskReport: OnyxEntry<OnyxTypes.Report>): numb | |
| return taskReport.managerID; | ||
| } | ||
|
|
||
| const reportAction = ReportActionsUtils.getParentReportAction(taskReport); | ||
| const reportAction = getParentReportAction(taskReport); | ||
| return reportAction.childManagerAccountID; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible and make sense to pass
Reporthere and todeleteTaskinstead ofOnyxEntry<OnyxTypes.Report>, so that we don't have to checkreport?? It looks like we only call deleteTask in one placeIt looks like before we were using
props.report.reportNameand notprops.report?.reportNameThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could revert the changes in
deleteTaskto go back to usingconst taskReport = ReportUtils.getReport(taskReportID);which returns aReportand not aOnyxEntry<OnyxTypes.Report>, but here is why I removed it.deleteTaskand then getting the full report object fromReportUtils.getReport()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah I think the change you made was an improvement, so I wasn't suggesting reverting back to the old version, just to go from
getParentReportAction(report: OnyxEntry<OnyxTypes.Report>):togetParentReportAction(report: Report):, and remove a lot of?that would be unnecessary. Is that doable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't simply cast one type to another like that, but I asked about it in Slack and I at least found a way to remove all the optional chaining by adding an early return at the beginning of the method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works. I was thinking not just updating the param type --that was to illustrate what I meant, but also the function call
Session.checkIfActionIsAllowed(Task.deleteTask(props.report));, with something likeSession.checkIfActionIsAllowed(Task.deleteTask(props.report as Report));or whatever TS magic helps with that.I like checking before and returning early tho 👍