Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import type {OriginalMessageChangeLog, PaymentMethodType} from '@src/types/onyx/
import type {Status} from '@src/types/onyx/PersonalDetails';
import type {ConnectionName} from '@src/types/onyx/Policy';
import type {NotificationPreference, Participants, PendingChatMember, Participant as ReportParticipant} from '@src/types/onyx/Report';
import type {Message, ReportActions} from '@src/types/onyx/ReportAction';
import type {Message, OldDotReportAction, ReportActions} from '@src/types/onyx/ReportAction';
import type {Comment, TransactionChanges, WaypointCollection} from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type IconAsset from '@src/types/utils/IconAsset';
Expand Down Expand Up @@ -258,6 +258,11 @@ type OptimisticCancelPaymentReportAction = Pick<
'actionName' | 'actorAccountID' | 'message' | 'originalMessage' | 'person' | 'reportActionID' | 'shouldShow' | 'created' | 'pendingAction'
>;

type OptimisticChangeFieldAction = Pick<
OldDotReportAction & ReportAction,
'actionName' | 'actorAccountID' | 'originalMessage' | 'person' | 'reportActionID' | 'created' | 'pendingAction' | 'message'
>;

type OptimisticEditedTaskReportAction = Pick<
ReportAction,
'reportActionID' | 'actionName' | 'pendingAction' | 'actorAccountID' | 'automatic' | 'avatar' | 'created' | 'shouldShow' | 'message' | 'person' | 'delegateAccountID'
Expand Down Expand Up @@ -2562,6 +2567,56 @@ function getReimbursementDeQueuedActionMessage(
return Localize.translateLocal('iou.canceledRequest', {submitterDisplayName, amount: formattedAmount});
}

/**
* Builds an optimistic REIMBURSEMENT_DEQUEUED report action with a randomly generated reportActionID.
*
*/
function buildOptimisticChangeFieldAction(reportField: PolicyReportField, previousReportField: PolicyReportField): OptimisticChangeFieldAction {
return {
actionName: CONST.REPORT.ACTIONS.TYPE.CHANGE_FIELD,
actorAccountID: currentUserAccountID,
message: [
{
type: 'TEXT',
style: 'strong',
text: 'You',
},
{
type: 'TEXT',
style: 'normal',
text: ` modified field '${reportField.name}'.`,
},
{
type: 'TEXT',
style: 'normal',
text: ` New value is '${reportField.value}'`,
},
{
type: 'TEXT',
style: 'normal',
text: ` (previously '${previousReportField.value}').`,
},
],
originalMessage: {
fieldName: reportField.name,
newType: reportField.type,
newValue: reportField.value,
oldType: previousReportField.type,
oldValue: previousReportField.value,
},
person: [
{
style: 'strong',
text: getCurrentUserDisplayNameOrEmail(),
type: 'TEXT',
},
],
reportActionID: NumberUtils.rand64(),
created: DateUtils.getDBTime(),
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
};
}

/**
* Builds an optimistic REIMBURSEMENT_DEQUEUED report action with a randomly generated reportActionID.
*
Expand Down Expand Up @@ -8636,6 +8691,7 @@ export {
hasMissingInvoiceBankAccount,
reasonForReportToBeInOptionList,
getReasonAndReportActionThatRequiresAttention,
buildOptimisticChangeFieldAction,
isPolicyRelatedReport,
hasReportErrorsOtherThanFailedReceipt,
shouldShowViolations,
Expand Down
28 changes: 28 additions & 0 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,8 @@ function updateReportField(reportID: string, reportField: PolicyReportField, pre
const fieldViolation = ReportUtils.getFieldViolation(reportViolations, reportField);
const recentlyUsedValues = allRecentlyUsedReportFields?.[fieldKey] ?? [];

const optimisticChangeFieldAction = ReportUtils.buildOptimisticChangeFieldAction(reportField, previousReportField);

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -1926,6 +1928,13 @@ function updateReportField(reportID: string, reportField: PolicyReportField, pre
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
value: {
[optimisticChangeFieldAction.reportActionID]: optimisticChangeFieldAction,
},
},
];

if (fieldViolation) {
Expand Down Expand Up @@ -1966,6 +1975,15 @@ function updateReportField(reportID: string, reportField: PolicyReportField, pre
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
value: {
[optimisticChangeFieldAction.reportActionID]: {
errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('report.genericUpdateReportFieldFailureMessage'),
},
},
},
];

if (reportField.type === 'dropdown') {
Expand All @@ -1991,11 +2009,21 @@ function updateReportField(reportID: string, reportField: PolicyReportField, pre
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
value: {
[optimisticChangeFieldAction.reportActionID]: {
pendingAction: null,
},
},
},
];

const parameters = {
reportID,
reportFields: JSON.stringify({[fieldKey]: reportField}),
reportFieldsActionIDs: JSON.stringify({[fieldKey]: optimisticChangeFieldAction.reportActionID}),
};

API.write(WRITE_COMMANDS.SET_REPORT_FIELD, parameters, {optimisticData, failureData, successData});
Expand Down