Skip to content

Commit 4f69c19

Browse files
authored
Merge pull request Expensify#87527 from Krishna2323/krishna2323/issue/66425-part-5
Part 5 - Thread delegateEmail through buildOptimisticTaskReportAction, completeTask, reopenTask, deleteTask
2 parents bae5db4 + cdb744d commit 4f69c19

11 files changed

Lines changed: 202 additions & 28 deletions

File tree

src/components/ReportActionItem/TaskPreview.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {delegateEmailSelector} from '@selectors/Account';
12
import React from 'react';
23
import {View} from 'react-native';
34
import type {StyleProp, ViewStyle} from 'react-native';
@@ -15,6 +16,7 @@ import type {WithCurrentUserPersonalDetailsProps} from '@components/withCurrentU
1516
import useHasOutstandingChildTask from '@hooks/useHasOutstandingChildTask';
1617
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
1718
import useLocalize from '@hooks/useLocalize';
19+
import useOnyx from '@hooks/useOnyx';
1820
import useParentReport from '@hooks/useParentReport';
1921
import useParentReportAction from '@hooks/useParentReportAction';
2022
import useReportIsArchived from '@hooks/useReportIsArchived';
@@ -31,6 +33,7 @@ import Parser from '@libs/Parser';
3133
import {isCanceledTaskReport, isOpenTaskReport, isReportManager} from '@libs/ReportUtils';
3234
import type {ContextMenuAnchor} from '@pages/inbox/report/ContextMenu/ReportActionContextMenu';
3335
import CONST from '@src/CONST';
36+
import ONYXKEYS from '@src/ONYXKEYS';
3437
import ROUTES from '@src/ROUTES';
3538
import type {Report, ReportAction} from '@src/types/onyx';
3639
import {isEmptyObject} from '@src/types/utils/EmptyObject';
@@ -114,6 +117,7 @@ function TaskPreview({
114117
const parentReport = useParentReport(taskContextReport?.reportID);
115118
const isParentReportArchived = useReportIsArchived(parentReport?.reportID);
116119
const hasOutstandingChildTask = useHasOutstandingChildTask(taskContextReport);
120+
const [delegateEmail] = useOnyx(ONYXKEYS.ACCOUNT, {selector: delegateEmailSelector});
117121
const isTaskActionable = canActionTask(taskContextReport, parentReportAction, currentUserPersonalDetails.accountID, parentReport, isParentReportArchived);
118122
const hasAssignee = taskAssigneeAccountID > 0;
119123
const personalDetails = usePersonalDetails();
@@ -163,9 +167,9 @@ function TaskPreview({
163167
disabled={!isTaskActionable}
164168
onPress={callFunctionIfActionIsAllowed(() => {
165169
if (isTaskCompleted) {
166-
reopenTask(taskContextReport, parentReport, currentUserPersonalDetails.accountID, taskReportID);
170+
reopenTask(taskContextReport, parentReport, currentUserPersonalDetails.accountID, delegateEmail, taskReportID);
167171
} else {
168-
completeTask(taskContextReport, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction, taskReportID);
172+
completeTask(taskContextReport, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction, delegateEmail, taskReportID);
169173
}
170174
})}
171175
accessibilityLabel={translate('task.task')}

src/components/ReportActionItem/TaskView.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {delegateEmailSelector} from '@selectors/Account';
12
import React, {useEffect, useMemo} from 'react';
23
import {View} from 'react-native';
34
import type {OnyxEntry} from 'react-native-onyx';
@@ -57,6 +58,7 @@ function TaskView({report, parentReport, action}: TaskViewProps) {
5758
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
5859
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
5960
const [betas] = useOnyx(ONYXKEYS.BETAS);
61+
const [delegateEmail] = useOnyx(ONYXKEYS.ACCOUNT, {selector: delegateEmailSelector});
6062

6163
useEffect(() => {
6264
setTaskReport(report);
@@ -151,9 +153,9 @@ function TaskView({report, parentReport, action}: TaskViewProps) {
151153
return;
152154
}
153155
if (isCompleted) {
154-
reopenTask(report, parentReport, currentUserPersonalDetails.accountID);
156+
reopenTask(report, parentReport, currentUserPersonalDetails.accountID, delegateEmail);
155157
} else {
156-
completeTask(report, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction);
158+
completeTask(report, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction, delegateEmail);
157159
}
158160
})}
159161
isChecked={isCompleted}

src/components/Search/SearchList/ListItem/TaskListItemRow.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {delegateEmailSelector} from '@selectors/Account';
12
import React from 'react';
23
import type {StyleProp, ViewStyle} from 'react-native';
34
import {View} from 'react-native';
@@ -10,6 +11,7 @@ import TextWithTooltip from '@components/TextWithTooltip';
1011
import useHasOutstandingChildTask from '@hooks/useHasOutstandingChildTask';
1112
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
1213
import useLocalize from '@hooks/useLocalize';
14+
import useOnyx from '@hooks/useOnyx';
1315
import useParentReport from '@hooks/useParentReport';
1416
import useParentReportAction from '@hooks/useParentReportAction';
1517
import useReportIsArchived from '@hooks/useReportIsArchived';
@@ -21,6 +23,7 @@ import {callFunctionIfActionIsAllowed} from '@libs/actions/Session';
2123
import {canActionTask, completeTask} from '@libs/actions/Task';
2224
import variables from '@styles/variables';
2325
import CONST from '@src/CONST';
26+
import ONYXKEYS from '@src/ONYXKEYS';
2427
import type {Report} from '@src/types/onyx';
2528
import AvatarWithTextCell from './AvatarWithTextCell';
2629
import DateCell from './DateCell';
@@ -79,6 +82,7 @@ function ActionCell({taskItem, isLargeScreenWidth}: TaskCellProps) {
7982
const isParentReportArchived = useReportIsArchived(parentReport?.reportID);
8083
const hasOutstandingChildTask = useHasOutstandingChildTask(taskItem.report);
8184
const parentReportAction = useParentReportAction(taskItem.report);
85+
const [delegateEmail] = useOnyx(ONYXKEYS.ACCOUNT, {selector: delegateEmailSelector});
8286
const isTaskActionable = canActionTask(taskItem.report, parentReportAction, session?.accountID, parentReport, isParentReportArchived);
8387
const isTaskCompleted = taskItem.statusNum === CONST.REPORT.STATUS_NUM.APPROVED && taskItem.stateNum === CONST.REPORT.STATE_NUM.APPROVED;
8488

@@ -108,7 +112,7 @@ function ActionCell({taskItem, isLargeScreenWidth}: TaskCellProps) {
108112
style={[styles.w100]}
109113
isDisabled={!isTaskActionable}
110114
onPress={callFunctionIfActionIsAllowed(() => {
111-
completeTask(taskItem as Report, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction, taskItem.reportID);
115+
completeTask(taskItem as Report, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction, delegateEmail, taskItem.reportID);
112116
})}
113117
/>
114118
);

src/components/TaskHeaderActionButton.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import {delegateEmailSelector} from '@selectors/Account';
12
import React from 'react';
23
import {View} from 'react-native';
34
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
45
import useHasOutstandingChildTask from '@hooks/useHasOutstandingChildTask';
56
import useLocalize from '@hooks/useLocalize';
7+
import useOnyx from '@hooks/useOnyx';
68
import useParentReport from '@hooks/useParentReport';
79
import useParentReportAction from '@hooks/useParentReportAction';
810
import useReportIsArchived from '@hooks/useReportIsArchived';
@@ -12,6 +14,7 @@ import {isActiveTaskEditRoute} from '@libs/TaskUtils';
1214
import {callFunctionIfActionIsAllowed} from '@userActions/Session';
1315
import {canActionTask, completeTask, reopenTask} from '@userActions/Task';
1416
import CONST from '@src/CONST';
17+
import ONYXKEYS from '@src/ONYXKEYS';
1518
import type * as OnyxTypes from '@src/types/onyx';
1619
import Button from './Button';
1720

@@ -28,6 +31,7 @@ function TaskHeaderActionButton({report}: TaskHeaderActionButtonProps) {
2831
const isParentReportArchived = useReportIsArchived(parentReport?.reportID);
2932
const hasOutstandingChildTask = useHasOutstandingChildTask(report);
3033
const parentReportAction = useParentReportAction(report);
34+
const [delegateEmail] = useOnyx(ONYXKEYS.ACCOUNT, {selector: delegateEmailSelector});
3135
const isTaskActionable = canActionTask(report, parentReportAction, currentUserPersonalDetails?.accountID, parentReport, isParentReportArchived);
3236

3337
if (!canWriteInReport(report)) {
@@ -46,9 +50,9 @@ function TaskHeaderActionButton({report}: TaskHeaderActionButtonProps) {
4650
return;
4751
}
4852
if (isCompletedTaskReport(report)) {
49-
reopenTask(report, parentReport, currentUserPersonalDetails.accountID);
53+
reopenTask(report, parentReport, currentUserPersonalDetails.accountID, delegateEmail);
5054
} else {
51-
completeTask(report, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction);
55+
completeTask(report, parentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction, delegateEmail);
5256
}
5357
})}
5458
style={styles.flex1}

src/libs/ReportUtils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7805,6 +7805,7 @@ function updateReportPreview(
78057805
function buildOptimisticTaskReportAction(
78067806
taskReportID: string,
78077807
actionName: typeof CONST.REPORT.ACTIONS.TYPE.TASK_COMPLETED | typeof CONST.REPORT.ACTIONS.TYPE.TASK_REOPENED | typeof CONST.REPORT.ACTIONS.TYPE.TASK_CANCELLED,
7808+
delegateEmailParam: string | undefined,
78087809
message = '',
78097810
actorAccountID = deprecatedCurrentUserAccountID,
78107811
createdOffset = 0,
@@ -7816,7 +7817,9 @@ function buildOptimisticTaskReportAction(
78167817
html: message,
78177818
whisperedTo: [],
78187819
};
7819-
const delegateAccountDetails = getPersonalDetailByEmail(delegateEmail);
7820+
// Falls back to module-level delegateEmail (from Onyx.connect) for callers not yet migrated; will be removed in https://github.com/Expensify/App/issues/66417
7821+
const effectiveDelegateEmail = delegateEmailParam ?? delegateEmail;
7822+
const delegateAccountDetails = effectiveDelegateEmail ? getPersonalDetailByEmail(effectiveDelegateEmail) : undefined;
78207823

78217824
return {
78227825
actionName,
@@ -11940,7 +11943,8 @@ function prepareOnboardingOnyxData({
1194011943
}
1194111944

1194211945
const completedTaskReportAction = isTaskAutoCompleted
11943-
? buildOptimisticTaskReportAction(currentTask.reportID, CONST.REPORT.ACTIONS.TYPE.TASK_COMPLETED, 'marked as complete', actorAccountID, 2)
11946+
? // Will be refactored in next PR; full restructure tracked in https://github.com/Expensify/App/issues/66417
11947+
buildOptimisticTaskReportAction(currentTask.reportID, CONST.REPORT.ACTIONS.TYPE.TASK_COMPLETED, undefined, 'marked as complete', actorAccountID, 2)
1194411948
: null;
1194511949
if (task.type === CONST.ONBOARDING_TASK_TYPE.CREATE_WORKSPACE) {
1194611950
createWorkspaceTaskReportID = currentTask.reportID;

src/libs/actions/Policy/Policy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2923,7 +2923,8 @@ function buildPolicyData(options: BuildPolicyDataOptions): OnyxData<BuildPolicyD
29232923
optimisticData: optimisticCreateWorkspaceTaskData,
29242924
successData: successCreateWorkspaceTaskData,
29252925
failureData: failureCreateWorkspaceTaskData,
2926-
} = buildTaskData(createWorkspaceTaskReport, introSelected.createWorkspace, false, false, undefined);
2926+
// Will be refactored in next PR; full restructure tracked in https://github.com/Expensify/App/issues/66417
2927+
} = buildTaskData(createWorkspaceTaskReport, introSelected.createWorkspace, false, false, undefined, undefined);
29272928
optimisticData.push(...optimisticCreateWorkspaceTaskData);
29282929
successData.push(...successCreateWorkspaceTaskData);
29292930
failureData.push(...failureCreateWorkspaceTaskData);

src/libs/actions/Task.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,15 @@ function buildTaskData(
386386
hasOutstandingChildTaskInParentReport: boolean,
387387
hasOutstandingChildTask: boolean,
388388
parentReportAction: OnyxEntry<ReportAction> | undefined,
389+
delegateEmail: string | undefined,
389390
): {
390391
optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>>;
391392
failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>>;
392393
successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>>;
393394
parameters: CompleteTaskParams;
394395
} {
395396
const message = `marked as complete`;
396-
const completedTaskReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASK_COMPLETED, message);
397+
const completedTaskReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASK_COMPLETED, delegateEmail, message);
397398
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [
398399
{
399400
onyxMethod: Onyx.METHOD.MERGE,
@@ -504,6 +505,7 @@ function completeTask(
504505
hasOutstandingChildTaskInParentReport: boolean,
505506
hasOutstandingChildTask: boolean,
506507
parentReportAction: OnyxEntry<ReportAction> | undefined,
508+
delegateEmail: string | undefined,
507509
reportIDFromAction?: string,
508510
): OnyxData<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS> {
509511
const taskReportID = taskReport?.reportID ?? reportIDFromAction;
@@ -518,6 +520,7 @@ function completeTask(
518520
hasOutstandingChildTaskInParentReport,
519521
hasOutstandingChildTask,
520522
parentReportAction,
523+
delegateEmail,
521524
);
522525

523526
playSound(SOUNDS.SUCCESS);
@@ -528,13 +531,19 @@ function completeTask(
528531
/**
529532
* Reopen a closed task
530533
*/
531-
function reopenTask(taskReport: OnyxEntry<OnyxTypes.Report>, parentReport: OnyxEntry<OnyxTypes.Report>, currentUserAccountID: number, reportIDFromAction?: string) {
534+
function reopenTask(
535+
taskReport: OnyxEntry<OnyxTypes.Report>,
536+
parentReport: OnyxEntry<OnyxTypes.Report>,
537+
currentUserAccountID: number,
538+
delegateEmail: string | undefined,
539+
reportIDFromAction?: string,
540+
) {
532541
const taskReportID = taskReport?.reportID ?? reportIDFromAction;
533542
if (!taskReportID) {
534543
return;
535544
}
536545
const message = `marked as incomplete`;
537-
const reopenedTaskReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASK_REOPENED, message);
546+
const reopenedTaskReportAction = ReportUtils.buildOptimisticTaskReportAction(taskReportID, CONST.REPORT.ACTIONS.TYPE.TASK_REOPENED, delegateEmail, message);
538547
const hasOutstandingChildTask = taskReport?.managerID === currentUserAccountID ? true : parentReport?.hasOutstandingChildTask;
539548

540549
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [
@@ -1173,13 +1182,14 @@ function deleteTask(
11731182
hasOutstandingChildTask: boolean,
11741183
parentReportAction: OnyxEntry<ReportAction>,
11751184
conciergeReportID: string | undefined,
1185+
delegateEmail: string | undefined,
11761186
ancestors: ReportUtils.Ancestor[] = [],
11771187
) {
11781188
if (!report) {
11791189
return;
11801190
}
11811191
const message = `deleted task: ${report.reportName}`;
1182-
const optimisticCancelReportAction = ReportUtils.buildOptimisticTaskReportAction(report.reportID, CONST.REPORT.ACTIONS.TYPE.TASK_CANCELLED, message);
1192+
const optimisticCancelReportAction = ReportUtils.buildOptimisticTaskReportAction(report.reportID, CONST.REPORT.ACTIONS.TYPE.TASK_CANCELLED, delegateEmail, message);
11831193
const optimisticReportActionID = optimisticCancelReportAction.reportActionID;
11841194
const canUserPerformWriteAction = ReportUtils.canUserPerformWriteAction(report, isReportArchived);
11851195

@@ -1437,7 +1447,8 @@ function getFinishOnboardingTaskOnyxData(
14371447
if (taskReport && canActionTask(taskReport, parentReportAction, currentUserAccountID, taskParentReport, isParentReportArchived)) {
14381448
if (taskReport) {
14391449
if (taskReport.stateNum !== CONST.REPORT.STATE_NUM.APPROVED || taskReport.statusNum !== CONST.REPORT.STATUS_NUM.APPROVED) {
1440-
return completeTask(taskReport, taskParentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction);
1450+
// Will be refactored in next PR; full restructure tracked in https://github.com/Expensify/App/issues/66417
1451+
return completeTask(taskReport, taskParentReport?.hasOutstandingChildTask ?? false, hasOutstandingChildTask, parentReportAction, undefined);
14411452
}
14421453
}
14431454
}

src/libs/actions/Workflow.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ function createApprovalWorkflow({approvalWorkflow, policy, addExpenseApprovalsTa
9292
addExpenseApprovalsTaskReport &&
9393
(addExpenseApprovalsTaskReport.stateNum !== CONST.REPORT.STATE_NUM.APPROVED || addExpenseApprovalsTaskReport.statusNum !== CONST.REPORT.STATUS_NUM.APPROVED)
9494
) {
95-
completeTask(addExpenseApprovalsTaskReport, false, false, undefined);
95+
// Will be refactored in next PR; full restructure tracked in https://github.com/Expensify/App/issues/66417
96+
completeTask(addExpenseApprovalsTaskReport, false, false, undefined, undefined);
9697
}
9798
}
9899

src/pages/ReportDetailsPage.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {StackActions} from '@react-navigation/native';
2+
import {delegateEmailSelector} from '@selectors/Account';
23
import {validTransactionDraftIDsSelector} from '@selectors/TransactionDraft';
34
import React, {useCallback, useEffect, useMemo} from 'react';
45
import {InteractionManager, View} from 'react-native';
@@ -197,6 +198,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
197198
const [betas] = useOnyx(ONYXKEYS.BETAS);
198199
const [draftTransactionIDs] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {selector: validTransactionDraftIDsSelector});
199200
const [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
201+
const [delegateEmail] = useOnyx(ONYXKEYS.ACCOUNT, {selector: delegateEmailSelector});
200202
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
201203
const {showConfirmModal} = useConfirmModal();
202204
const isPolicyAdmin = useMemo(() => isPolicyAdminUtil(policy), [policy]);
@@ -547,7 +549,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
547549
isAnonymousAction: false,
548550
action: callFunctionIfActionIsAllowed(() => {
549551
Navigation.goBack(backTo);
550-
reopenTask(report, parentReport, currentUserPersonalDetails?.accountID);
552+
reopenTask(report, parentReport, currentUserPersonalDetails?.accountID, delegateEmail);
551553
}),
552554
});
553555
}
@@ -648,6 +650,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
648650
amountOwed,
649651
ownerBillingGracePeriodEnd,
650652
iouTransaction,
653+
delegateEmail,
651654
]);
652655

653656
const displayNamesWithTooltips = useMemo(() => {
@@ -896,7 +899,17 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
896899

897900
const deleteTransaction = useCallback(() => {
898901
if (caseID === CASES.DEFAULT) {
899-
deleteTask(report, parentReport, isReportArchived, currentUserPersonalDetails.accountID, hasOutstandingChildTask, parentReportAction, conciergeReportID, ancestors);
902+
deleteTask(
903+
report,
904+
parentReport,
905+
isReportArchived,
906+
currentUserPersonalDetails.accountID,
907+
hasOutstandingChildTask,
908+
parentReportAction,
909+
conciergeReportID,
910+
delegateEmail,
911+
ancestors,
912+
);
900913
return;
901914
}
902915

@@ -949,6 +962,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
949962
deleteTransactions,
950963
removeTransaction,
951964
conciergeReportID,
965+
delegateEmail,
952966
]);
953967

954968
// Where to navigate back to after deleting the transaction and its report.

0 commit comments

Comments
 (0)