Skip to content

Commit

Permalink
clean ups, and added DELETE_COMMENT
Browse files Browse the repository at this point in the history
  • Loading branch information
gedu committed Aug 28, 2024
1 parent 6f278c0 commit e915009
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/libs/Network/SequentialQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,14 @@ function push(newRequest: OnyxRequest) {

const {checkAndFixConflictingRequest} = newRequest;
if (checkAndFixConflictingRequest) {
const {conflictAction} = checkAndFixConflictingRequest(requests, newRequest);
const {conflictAction} = checkAndFixConflictingRequest(requests);

// Don't try to serialize conflict resolution functions
delete newRequest.checkAndFixConflictingRequest;

if (conflictAction.type === 'save') {
PersistedRequests.save(newRequest);

Check failure on line 208 in src/libs/Network/SequentialQueue.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Assignment to property of function parameter 'newRequest'
} else {
} else if (conflictAction.type === 'update') {
PersistedRequests.update(conflictAction.index, newRequest);
}
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,17 @@ function reconnectApp(updateIDFrom: OnyxEntry<number> = 0) {
}

API.write(WRITE_COMMANDS.RECONNECT_APP, params, getOnyxDataForOpenOrReconnect(), {
checkAndFixConflictingRequest: (persistedRequests, newRequest) => {
checkAndFixConflictingRequest: (persistedRequests) => {
const index = persistedRequests.findIndex((request) => request.command === WRITE_COMMANDS.RECONNECT_APP);
if (index === -1) {
return {
request: newRequest,
conflictAction: {
type: 'save',
},
};
}

return {
request: newRequest,
conflictAction: {
type: 'update',
index,
Expand Down
13 changes: 10 additions & 3 deletions src/libs/actions/PersistedRequests.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import isEqual from 'lodash/isEqual';
import Onyx from 'react-native-onyx';
import {WRITE_COMMANDS} from '@libs/API/types';
import Log from '@libs/Log';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Request} from '@src/types/onyx';

let persistedRequests: Request[] = [];
const keepLastInstance: string[] = [WRITE_COMMANDS.RECONNECT_APP];

Onyx.connect({
key: ONYXKEYS.PERSISTED_REQUESTS,
Expand Down Expand Up @@ -50,6 +48,15 @@ function remove(requestToRemove: Request) {
});
}

function bulkRemove(requestsToRemove: Request[]) {
if (requestsToRemove.length === 0) {
return;
}
const requests = persistedRequests.filter((request) => !requestsToRemove.includes(request));
persistedRequests = requests;
Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, requests);
}

function update(oldRequestIndex: number, newRequest: Request) {
const requests = [...persistedRequests];
requests.splice(oldRequestIndex, 1, newRequest);
Expand All @@ -61,4 +68,4 @@ function getAll(): Request[] {
return persistedRequests;
}

export {clear, save, getAll, remove, update, getLength};
export {clear, save, getAll, remove, update, getLength, bulkRemove};
33 changes: 32 additions & 1 deletion src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import * as ReportUtils from '@libs/ReportUtils';
import {doesReportBelongToWorkspace} from '@libs/ReportUtils';
import shouldSkipDeepLinkNavigation from '@libs/shouldSkipDeepLinkNavigation';
import Visibility from '@libs/Visibility';
import * as PersistedRequests from '@userActions/PersistedRequests';

Check warning on line 85 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected subpath import via alias '@userActions/PersistedRequests'. Use './PersistedRequests' instead
import CONFIG from '@src/CONFIG';
import type {OnboardingPurposeType} from '@src/CONST';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -1510,7 +1511,37 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) {

CachedPDFPaths.clearByKey(reportActionID);

API.write(WRITE_COMMANDS.DELETE_COMMENT, parameters, {optimisticData, successData, failureData});
let shouldRequestHappen = true;

API.write(
WRITE_COMMANDS.DELETE_COMMENT,
parameters,
{optimisticData, successData, failureData},
{
checkAndFixConflictingRequest: (persistedRequests) => {
const conflictingCommands = (
isDeletedParentAction ? [WRITE_COMMANDS.UPDATE_COMMENT] : [WRITE_COMMANDS.ADD_COMMENT, WRITE_COMMANDS.ADD_ATTACHMENT, WRITE_COMMANDS.UPDATE_COMMENT]
) as string[];

const conflictingRequests = persistedRequests.filter((request) => conflictingCommands.includes(request.command) && request.data?.reportActionID === reportActionID);
shouldRequestHappen = conflictingRequests.some((request) => request.command !== WRITE_COMMANDS.UPDATE_COMMENT);
const hasConflict = conflictingRequests.length > 0;

// Delete the conflicting requests
PersistedRequests.bulkRemove(conflictingRequests);

conflictingRequests.forEach(() => {
Onyx.update(successData);
});

const mustSave = !(!shouldRequestHappen && hasConflict);

return {
conflictAction: mustSave ? {type: 'save'} : {type: 'noAction'},
};
},
},
);

// if we are linking to the report action, and we are deleting it, and it's not a deleted parent action,
// we should navigate to its report in order to not show not found page
Expand Down
17 changes: 11 additions & 6 deletions src/types/onyx/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,23 @@ type ConflictRequestSave = {
};

/**
* An object that has the request and the action to take in case of a conflict.
* Model of a conflict request that no need to be updated or saved, in the request queue.
*/
type ConflictActionData = {
type ConflictRequestNoAction = {
/**
* The request that is conflicting with the new request.
* The action to take in case of a conflict.
*/
request: Request;
type: 'noAction';
};

/**
* An object that has the request and the action to take in case of a conflict.
*/
type ConflictActionData = {
/**
* The action to take in case of a conflict.
*/
conflictAction: ConflictRequestUpdate | ConflictRequestSave;
conflictAction: ConflictRequestUpdate | ConflictRequestSave | ConflictRequestNoAction;
};

/**
Expand All @@ -106,7 +111,7 @@ type RequestConflictResolver = {
/**
* A function that checks if a new request conflicts with any existing requests in the queue.
*/
checkAndFixConflictingRequest?: (persistedRequest: Request[], request: Request) => ConflictActionData;
checkAndFixConflictingRequest?: (persistedRequest: Request[]) => ConflictActionData;
};

/**
Expand Down

0 comments on commit e915009

Please sign in to comment.