Skip to content

Commit

Permalink
Merge pull request Expensify#51149 from zirgulis/zirgulis/dedupe_upda…
Browse files Browse the repository at this point in the history
…te_comment_requests

Dedupe multiple UpdateComment requests
  • Loading branch information
mountiny authored Oct 23, 2024
2 parents 66acbb9 + 6f7932c commit 2712919
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type {OnyxData} from '@src/types/onyx/Request';
import {setShouldForceOffline} from './Network';
import * as PersistedRequests from './PersistedRequests';
import * as Policy from './Policy/Policy';
import resolveDuplicationConflictAction from './RequestConflictUtils';
import {resolveDuplicationConflictAction} from './RequestConflictUtils';
import * as Session from './Session';
import Timing from './Timing';

Expand Down
11 changes: 9 additions & 2 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject';
import * as CachedPDFPaths from './CachedPDFPaths';
import * as Modal from './Modal';
import navigateFromNotification from './navigateFromNotification';
import resolveDuplicationConflictAction from './RequestConflictUtils';
import {createUpdateCommentMatcher, resolveDuplicationConflictAction} from './RequestConflictUtils';
import * as Session from './Session';
import * as Welcome from './Welcome';
import * as OnboardingFlow from './Welcome/OnboardingFlow';
Expand Down Expand Up @@ -1695,7 +1695,14 @@ function editReportComment(reportID: string, originalReportAction: OnyxEntry<Rep
reportActionID,
};

API.write(WRITE_COMMANDS.UPDATE_COMMENT, parameters, {optimisticData, successData, failureData});
API.write(
WRITE_COMMANDS.UPDATE_COMMENT,
parameters,
{optimisticData, successData, failureData},
{
checkAndFixConflictingRequest: (persistedRequests) => resolveDuplicationConflictAction(persistedRequests, createUpdateCommentMatcher(reportActionID)),
},
);
}

/** Deletes the draft for a comment report action. */
Expand Down
9 changes: 8 additions & 1 deletion src/libs/actions/RequestConflictUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import {WRITE_COMMANDS} from '@libs/API/types';
import type OnyxRequest from '@src/types/onyx/Request';
import type {ConflictActionData} from '@src/types/onyx/Request';

function createUpdateCommentMatcher(reportActionID: string) {
return function (request: OnyxRequest) {
return request.command === WRITE_COMMANDS.UPDATE_COMMENT && request.data?.reportActionID === reportActionID;
};
}

type RequestMatcher = (request: OnyxRequest) => boolean;

/**
Expand Down Expand Up @@ -28,4 +35,4 @@ function resolveDuplicationConflictAction(persistedRequests: OnyxRequest[], requ
};
}

export default resolveDuplicationConflictAction;
export {resolveDuplicationConflictAction, createUpdateCommentMatcher};
28 changes: 28 additions & 0 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,4 +808,32 @@ describe('actions/Report', () => {

TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.OPEN_REPORT, 4);
});

it('it should only send the last sequential UpdateComment request to BE', async () => {
global.fetch = TestHelper.getGlobalFetchMock();
const reportID = '123';

await Onyx.set(ONYXKEYS.NETWORK, {isOffline: true});

const action: OnyxEntry<OnyxTypes.ReportAction> = {
reportID,
reportActionID: '722',
actionName: 'ADDCOMMENT',
created: '2024-10-21 10:37:59.881',
};

Report.editReportComment(reportID, action, 'value1');
Report.editReportComment(reportID, action, 'value2');
Report.editReportComment(reportID, action, 'value3');

const requests = PersistedRequests?.getAll();

expect(requests.length).toBe(1);
expect(requests?.at(0)?.command).toBe(WRITE_COMMANDS.UPDATE_COMMENT);
expect(requests?.at(0)?.data?.reportComment).toBe('value3');

await Onyx.set(ONYXKEYS.NETWORK, {isOffline: false});

TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.UPDATE_COMMENT, 1);
});
});
2 changes: 1 addition & 1 deletion tests/unit/RequestConflictUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import resolveDuplicationConflictAction from '@libs/actions/RequestConflictUtils';
import {resolveDuplicationConflictAction} from '@libs/actions/RequestConflictUtils';
import type {WriteCommand} from '@libs/API/types';

describe('RequestConflictUtils', () => {
Expand Down

0 comments on commit 2712919

Please sign in to comment.