Skip to content

Commit

Permalink
Merge pull request Expensify#35826 from paultsimura/fix/34364-rename-…
Browse files Browse the repository at this point in the history
…room-offline

fix: Create an optimistic "Renamed" report action
  • Loading branch information
MonilBhavsar authored Feb 9, 2024
2 parents c0498e7 + 27e1715 commit 004d8e9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/libs/API/parameters/UpdatePolicyRoomNameParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type UpdatePolicyRoomNameParams = {
reportID: string;
policyRoomName: string;
renamedRoomReportActionID: string;
};

export default UpdatePolicyRoomNameParams;
58 changes: 54 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type {
} from '@src/types/onyx';
import type {Participant} from '@src/types/onyx/IOU';
import type {Errors, Icon, PendingAction} from '@src/types/onyx/OnyxCommon';
import type {ChangeLog, IOUMessage, OriginalMessageActionName, OriginalMessageCreated, PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type {ChangeLog, IOUMessage, OriginalMessageActionName, OriginalMessageCreated, OriginalMessageRenamed, PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type {Status} from '@src/types/onyx/PersonalDetails';
import type {NotificationPreference} from '@src/types/onyx/Report';
import type {Message, ReportActionBase, ReportActions} from '@src/types/onyx/ReportAction';
Expand Down Expand Up @@ -199,6 +199,9 @@ type OptimisticClosedReportAction = Pick<
type OptimisticCreatedReportAction = OriginalMessageCreated &
Pick<ReportActionBase, 'actorAccountID' | 'automatic' | 'avatar' | 'created' | 'message' | 'person' | 'reportActionID' | 'shouldShow' | 'pendingAction'>;

type OptimisticRenamedReportAction = OriginalMessageRenamed &
Pick<ReportActionBase, 'actorAccountID' | 'automatic' | 'avatar' | 'created' | 'message' | 'person' | 'reportActionID' | 'shouldShow' | 'pendingAction'>;

type OptimisticChatReport = Pick<
Report,
| 'type'
Expand Down Expand Up @@ -3334,6 +3337,10 @@ function buildOptimisticChatReport(
};
}

function getCurrentUserAvatarOrDefault(): UserUtils.AvatarSource {
return allPersonalDetails?.[currentUserAccountID ?? '']?.avatar ?? UserUtils.getDefaultAvatarURL(currentUserAccountID);
}

/**
* Returns the necessary reportAction onyx data to indicate that the chat has been created optimistically
* @param [created] - Action created time
Expand Down Expand Up @@ -3364,12 +3371,54 @@ function buildOptimisticCreatedReportAction(emailCreatingAction: string, created
},
],
automatic: false,
avatar: allPersonalDetails?.[currentUserAccountID ?? '']?.avatar ?? UserUtils.getDefaultAvatarURL(currentUserAccountID),
avatar: getCurrentUserAvatarOrDefault(),
created,
shouldShow: true,
};
}

/**
* Returns the necessary reportAction onyx data to indicate that the room has been renamed
*/
function buildOptimisticRenamedRoomReportAction(newName: string, oldName: string): OptimisticRenamedReportAction {
const now = DateUtils.getDBTime();
return {
reportActionID: NumberUtils.rand64(),
actionName: CONST.REPORT.ACTIONS.TYPE.RENAMED,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
actorAccountID: currentUserAccountID,
message: [
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
style: 'strong',
text: 'You',
},
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
style: 'normal',
text: ` renamed this report. New title is '${newName}' (previously '${oldName}').`,
},
],
person: [
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
style: 'strong',
text: allPersonalDetails?.[currentUserAccountID ?? '']?.displayName ?? currentUserEmail,
},
],
originalMessage: {
oldName,
newName,
html: `Room renamed to ${newName}`,
lastModified: now,
},
automatic: false,
avatar: getCurrentUserAvatarOrDefault(),
created: now,
shouldShow: true,
};
}

/**
* Returns the necessary reportAction onyx data to indicate that a task report has been edited
*/
Expand Down Expand Up @@ -3399,7 +3448,7 @@ function buildOptimisticEditedTaskReportAction(emailEditingTask: string): Optimi
},
],
automatic: false,
avatar: allPersonalDetails?.[currentUserAccountID ?? '']?.avatar ?? UserUtils.getDefaultAvatarURL(currentUserAccountID),
avatar: getCurrentUserAvatarOrDefault(),
created: DateUtils.getDBTime(),
shouldShow: false,
};
Expand All @@ -3415,7 +3464,7 @@ function buildOptimisticClosedReportAction(emailClosingReport: string, policyNam
actionName: CONST.REPORT.ACTIONS.TYPE.CLOSED,
actorAccountID: currentUserAccountID,
automatic: false,
avatar: allPersonalDetails?.[currentUserAccountID ?? '']?.avatar ?? UserUtils.getDefaultAvatarURL(currentUserAccountID),
avatar: getCurrentUserAvatarOrDefault(),
created: DateUtils.getDBTime(),
message: [
{
Expand Down Expand Up @@ -4863,6 +4912,7 @@ export {
buildOptimisticChatReport,
buildOptimisticClosedReportAction,
buildOptimisticCreatedReportAction,
buildOptimisticRenamedRoomReportAction,
buildOptimisticEditedTaskReportAction,
buildOptimisticIOUReport,
buildOptimisticApprovedReportAction,
Expand Down
26 changes: 25 additions & 1 deletion src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,7 @@ function navigateToConciergeChatAndDeleteReport(reportID: string) {
}

/**
* @param policyRoomReport The policy room report
* @param policyRoomName The updated name for the policy room
*/
function updatePolicyRoomNameAndNavigate(policyRoomReport: Report, policyRoomName: string) {
Expand All @@ -1838,6 +1839,8 @@ function updatePolicyRoomNameAndNavigate(policyRoomReport: Report, policyRoomNam
return;
}

const optimisticRenamedAction = ReportUtils.buildOptimisticRenamedRoomReportAction(policyRoomName, previousName ?? '');

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -1852,6 +1855,13 @@ function updatePolicyRoomNameAndNavigate(policyRoomReport: Report, policyRoomNam
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
value: {
[optimisticRenamedAction.reportActionID]: optimisticRenamedAction,
},
},
];
const successData: OnyxUpdate[] = [
{
Expand All @@ -1863,6 +1873,11 @@ function updatePolicyRoomNameAndNavigate(policyRoomReport: Report, policyRoomNam
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
value: {[optimisticRenamedAction.reportActionID]: {pendingAction: null}},
},
];
const failureData: OnyxUpdate[] = [
{
Expand All @@ -1872,9 +1887,18 @@ function updatePolicyRoomNameAndNavigate(policyRoomReport: Report, policyRoomNam
reportName: previousName,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
value: {[optimisticRenamedAction.reportActionID]: null},
},
];

const parameters: UpdatePolicyRoomNameParams = {reportID, policyRoomName};
const parameters: UpdatePolicyRoomNameParams = {
reportID,
policyRoomName,
renamedRoomReportActionID: optimisticRenamedAction.reportActionID,
};

API.write(WRITE_COMMANDS.UPDATE_POLICY_ROOM_NAME, parameters, {optimisticData, successData, failureData});
Navigation.goBack(ROUTES.REPORT_SETTINGS.getRoute(reportID));
Expand Down
1 change: 1 addition & 0 deletions src/types/onyx/OriginalMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ export type {
ChangeLog,
OriginalMessageIOU,
OriginalMessageCreated,
OriginalMessageRenamed,
OriginalMessageAddComment,
OriginalMessageChronosOOOList,
OriginalMessageSource,
Expand Down

0 comments on commit 004d8e9

Please sign in to comment.