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
25 changes: 25 additions & 0 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3673,6 +3673,30 @@ function setGroupDraft(newGroupDraft: Partial<NewGroupChatDraft>) {
Onyx.merge(ONYXKEYS.NEW_GROUP_CHAT_DRAFT, newGroupDraft);
}

function setTaskDataForNewAssingee(assigneeLogin: string, accountID: 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.

Move the function to src/libs/actions/Task.ts

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 setTaskDataForNewAssingee(assigneeLogin: string, accountID: number | undefined = undefined) {
function setNewOptimisticAssignee(assigneeLogin: string, assigneeAccountID: number) {

Copy link
Contributor

Choose a reason for hiding this comment

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

Or setOptimisticReportAndPersonalDetailsForNewAssignee

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 What do you think about setOptimisticDataForNewAssignee?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not use data because it does not tell us anything and we would have to read the function to understand what data means

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I think setNewOptimisticAssignee is fine.

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 assigneeAccountID = accountID ?? UserUtils.generateAccountID(assigneeLogin);
const report: OnyxEntry<Report> | undefined = ReportUtils.buildOptimisticChatReport([assigneeAccountID]);
nkdengineer marked this conversation as resolved.
Show resolved Hide resolved
report.isOptimisticReport = true;
nkdengineer marked this conversation as resolved.
Show resolved Hide resolved

// 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;
}
nkdengineer marked this conversation as resolved.
Show resolved Hide resolved
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);

const optimisticPersonalDetailsListAction = {
nkdengineer marked this conversation as resolved.
Show resolved Hide resolved
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};
}

export {
searchInServer,
addComment,
Expand Down Expand Up @@ -3753,5 +3777,6 @@ export {
leaveGroupChat,
removeFromGroupChat,
updateGroupChatMemberRoles,
setTaskDataForNewAssingee,
clearAvatarErrors,
};
22 changes: 1 addition & 21 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import playSound, {SOUNDS} from '@libs/Sound';
import * as UserUtils from '@libs/UserUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -670,26 +669,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 = Report.setTaskDataForNewAssingee(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
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 = Report.setTaskDataForNewAssingee(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