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
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
43 changes: 23 additions & 20 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,27 @@ function setAssigneeChatReport(chatReport: OnyxTypes.Report) {
Onyx.merge(ONYXKEYS.TASK, {assigneeChatReport: chatReport});
}

function setNewOptimisticAssignee(assigneeLogin: string, assigneeAccountID: number | undefined = undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function setNewOptimisticAssignee(assigneeLogin: string, assigneeAccountID: number | undefined = undefined) {
function setNewOptimisticAssignee(assigneeLogin: string, assigneeAccountID: number) {

NAB let's make the prop required and generate the account id before calling this function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@s77rt I updated.

const currentAssigneeAccountID = assigneeAccountID ?? UserUtils.generateAccountID(assigneeLogin);
const report: ReportUtils.OptimisticChatReport = ReportUtils.buildOptimisticChatReport([currentAssigneeAccountID]);

// 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: currentAssigneeAccountID,
avatar: allPersonalDetails?.[currentAssigneeAccountID]?.avatar ?? UserUtils.getDefaultAvatarURL(currentAssigneeAccountID),
displayName: allPersonalDetails?.[currentAssigneeAccountID]?.displayName ?? assigneeLogin,
login: assigneeLogin,
};
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[currentAssigneeAccountID]: 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 +691,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 +1053,7 @@ export {
getTaskAssigneeAccountID,
clearTaskErrors,
canModifyTask,
setNewOptimisticAssignee,
};

export type {PolicyValue, Assignee, ShareDestination};
8 changes: 7 additions & 1 deletion src/pages/home/report/ReportFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,16 @@ 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 optimisticDataForNewAssignee = Task.setNewOptimisticAssignee(mentionWithDomain);
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