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
26 changes: 26 additions & 0 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Onyx from 'react-native-onyx';
import type {PartialDeep, ValueOf} from 'type-fest';
import type {Emoji} from '@assets/emojis/types';
import type {FileObject} from '@components/AttachmentModal';
import {FallbackAvatar} from '@components/Icon/Expensicons';
import * as ActiveClientManager from '@libs/ActiveClientManager';
import * as API from '@libs/API';
import type {
Expand Down Expand Up @@ -3714,6 +3715,30 @@ function setGroupDraft(newGroupDraft: Partial<NewGroupChatDraft>) {
Onyx.merge(ONYXKEYS.NEW_GROUP_CHAT_DRAFT, newGroupDraft);
}

function buildOptimisticTaskDataForNewAssingee(assigneeLogin: string) {
nkdengineer marked this conversation as resolved.
Show resolved Hide resolved
const assigneeAccountID = 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: FallbackAvatar,
displayName: assigneeLogin,
login: assigneeLogin,
};
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[assigneeAccountID]: optimisticPersonalDetailsListAction});
return {assignee: optimisticPersonalDetailsListAction, assigneeReport: report};
nkdengineer marked this conversation as resolved.
Show resolved Hide resolved
}

export {
searchInServer,
addComment,
Expand Down Expand Up @@ -3797,4 +3822,5 @@ export {
removeFromGroupChat,
updateGroupChatMemberRoles,
clearAddRoomMemberError,
buildOptimisticTaskDataForNewAssingee,
};
8 changes: 7 additions & 1 deletion src/pages/home/report/ReportFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,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.buildOptimisticTaskDataForNewAssingee(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