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 cannot create task for new user via [] method #41171

Merged
merged 9 commits into from
May 6, 2024
Merged
1 change: 1 addition & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ type OptimisticChatReport = Pick<
| 'writeCapability'
| 'avatarUrl'
| 'invoiceReceiver'
| 'isHidden'
> & {
isOptimisticReport: true;
};
Expand Down
42 changes: 22 additions & 20 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,26 @@ function setAssigneeChatReport(chatReport: OnyxTypes.Report) {
Onyx.merge(ONYXKEYS.TASK, {assigneeChatReport: chatReport});
}

function setNewOptimisticAssignee(assigneeLogin: string, assigneeAccountID: number) {
const report: ReportUtils.OptimisticChatReport = ReportUtils.buildOptimisticChatReport([assigneeAccountID]);

// When assigning a task to a new user, by default we share the task in their DM
// However, the DM doesn't exist yet - and will be created optimistically once the task is created
// We don't want to show the new DM yet, because if you select an assignee and then change the assignee, the previous DM will still be shown
// So here, we create it optimistically to share it with the assignee, but we have to hide it until the task is created
report.isHidden = true;
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);

const optimisticPersonalDetailsListAction: OnyxTypes.PersonalDetails = {
accountID: assigneeAccountID,
avatar: allPersonalDetails?.[assigneeAccountID]?.avatar ?? UserUtils.getDefaultAvatarURL(assigneeAccountID),
displayName: allPersonalDetails?.[assigneeAccountID]?.displayName ?? assigneeLogin,
login: assigneeLogin,
};
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[assigneeAccountID]: optimisticPersonalDetailsListAction});
return {assignee: optimisticPersonalDetailsListAction, assigneeReport: report};
}

/**
* Sets the assignee value for the task and checks for an existing chat with the assignee
* If there is no existing chat, it creates an optimistic chat report
Expand All @@ -670,26 +690,7 @@ function setAssigneeValue(
}
// If chat report is still not found we need to build new optimistic chat report
if (!report) {
report = ReportUtils.buildOptimisticChatReport([assigneeAccountID]);
report.isOptimisticReport = true;

// When assigning a task to a new user, by default we share the task in their DM
// However, the DM doesn't exist yet - and will be created optimistically once the task is created
// We don't want to show the new DM yet, because if you select an assignee and then change the assignee, the previous DM will still be shown
// So here, we create it optimistically to share it with the assignee, but we have to hide it until the task is created
if (report) {
report.isHidden = true;
}
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);

// If this is an optimistic report, we likely don't have their personal details yet so we set it here optimistically as well
const optimisticPersonalDetailsListAction = {
accountID: assigneeAccountID,
avatar: allPersonalDetails?.[assigneeAccountID]?.avatar ?? UserUtils.getDefaultAvatarURL(assigneeAccountID),
displayName: allPersonalDetails?.[assigneeAccountID]?.displayName ?? assigneeEmail,
login: assigneeEmail,
};
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[assigneeAccountID]: optimisticPersonalDetailsListAction});
report = setNewOptimisticAssignee(assigneeEmail, assigneeAccountID).assigneeReport;
}

// The optimistic field may not exist in the existing report and it can be overridden by the optimistic field of previous report data when merging the assignee chat report
Expand Down Expand Up @@ -1051,6 +1052,7 @@ export {
getTaskAssigneeAccountID,
clearTaskErrors,
canModifyTask,
setNewOptimisticAssignee,
};

export type {PolicyValue, Assignee, ShareDestination};
10 changes: 9 additions & 1 deletion src/pages/home/report/ReportFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as ReportUtils from '@libs/ReportUtils';
import * as UserUtils from '@libs/UserUtils';
import variables from '@styles/variables';
import * as Report from '@userActions/Report';
import * as Task from '@userActions/Task';
Expand Down Expand Up @@ -110,10 +111,17 @@ function ReportFooter({
const mentionWithDomain = ReportUtils.addDomainToShortMention(mention ?? '') ?? mention;

let assignee: OnyxTypes.PersonalDetails | EmptyObject = {};
let assigneeChatReport;
if (mentionWithDomain) {
assignee = Object.values(allPersonalDetails).find((value) => value?.login === mentionWithDomain) ?? {};
if (!Object.keys(assignee).length) {
const assigneeAccountID = UserUtils.generateAccountID(mentionWithDomain);
const optimisticDataForNewAssignee = Task.setNewOptimisticAssignee(mentionWithDomain, assigneeAccountID);
assignee = optimisticDataForNewAssignee.assignee;
assigneeChatReport = optimisticDataForNewAssignee.assigneeReport;
}
}
Task.createTaskAndNavigate(report.reportID, title, '', assignee?.login ?? '', assignee.accountID, undefined, report.policyID);
Task.createTaskAndNavigate(report.reportID, title, '', assignee?.login ?? '', assignee.accountID, assigneeChatReport, report.policyID);
return true;
},
[allPersonalDetails, report.policyID, report.reportID],
Expand Down
Loading