From da4be7b57f29e399de566fb59ec4a37ea9c199ed Mon Sep 17 00:00:00 2001 From: Alessandro Martelli Date: Fri, 28 May 2021 13:19:59 +0200 Subject: [PATCH] [DSC-76] Delete pending json patch operations --- .../json-patch-operations.actions.ts | 11 +++++++++- .../json-patch-operations.reducer.spec.ts | 17 ++++++++++++++- .../json-patch-operations.reducer.ts | 21 ++++++++++++++++++- .../json-patch-operations.service.spec.ts | 16 ++++++++++++++ .../json-patch-operations.service.ts | 9 +++++++- ...sion-json-patch-operations-service.stub.ts | 1 + .../edit/submission-edit.component.spec.ts | 17 +++++++++++++++ .../edit/submission-edit.component.ts | 6 +++++- 8 files changed, 93 insertions(+), 5 deletions(-) diff --git a/src/app/core/json-patch/json-patch-operations.actions.ts b/src/app/core/json-patch/json-patch-operations.actions.ts index 4c26703472..91fc6cb0da 100644 --- a/src/app/core/json-patch/json-patch-operations.actions.ts +++ b/src/app/core/json-patch/json-patch-operations.actions.ts @@ -20,6 +20,7 @@ export const JsonPatchOperationsActionTypes = { ROLLBACK_JSON_PATCH_OPERATIONS: type('dspace/core/patch/ROLLBACK_JSON_PATCH_OPERATIONS'), FLUSH_JSON_PATCH_OPERATIONS: type('dspace/core/patch/FLUSH_JSON_PATCH_OPERATIONS'), START_TRANSACTION_JSON_PATCH_OPERATIONS: type('dspace/core/patch/START_TRANSACTION_JSON_PATCH_OPERATIONS'), + DELETE_PENDING_JSON_PATCH_OPERATIONS: type('dspace/core/patch/DELETE_PENDING_JSON_PATCH_OPERATIONS'), }; /* tslint:disable:max-classes-per-file */ @@ -261,6 +262,13 @@ export class NewPatchReplaceOperationAction implements Action { } } +/** + * An ngrx action to delete all pending JSON Patch Operations. + */ +export class DeletePendingJsonPatchOperationsAction implements Action { + type = JsonPatchOperationsActionTypes.DELETE_PENDING_JSON_PATCH_OPERATIONS; +} + /* tslint:enable:max-classes-per-file */ /** @@ -276,4 +284,5 @@ export type PatchOperationsActions | NewPatchRemoveOperationAction | NewPatchReplaceOperationAction | RollbacktPatchOperationsAction - | StartTransactionPatchOperationsAction; + | StartTransactionPatchOperationsAction + | DeletePendingJsonPatchOperationsAction; diff --git a/src/app/core/json-patch/json-patch-operations.reducer.spec.ts b/src/app/core/json-patch/json-patch-operations.reducer.spec.ts index 34378b819b..1f98cf0920 100644 --- a/src/app/core/json-patch/json-patch-operations.reducer.spec.ts +++ b/src/app/core/json-patch/json-patch-operations.reducer.spec.ts @@ -1,7 +1,7 @@ import * as deepFreeze from 'deep-freeze'; import { - CommitPatchOperationsAction, + CommitPatchOperationsAction, DeletePendingJsonPatchOperationsAction, FlushPatchOperationsAction, NewPatchAddOperationAction, NewPatchRemoveOperationAction, @@ -323,4 +323,19 @@ describe('jsonPatchOperationsReducer test suite', () => { }); + describe('When DeletePendingJsonPatchOperationsAction has been dispatched', () => { + it('should set set the JsonPatchOperationsState to null ', () => { + const action = new DeletePendingJsonPatchOperationsAction(); + initState = Object.assign({}, testState, { + [testJsonPatchResourceType]: Object.assign({}, testState[testJsonPatchResourceType], { + transactionStartTime: startTimestamp, + commitPending: true + }) + }); + const newState = jsonPatchOperationsReducer(initState, action); + + expect(newState).toBeNull(); + }); + }); + }); diff --git a/src/app/core/json-patch/json-patch-operations.reducer.ts b/src/app/core/json-patch/json-patch-operations.reducer.ts index 8d630dbfa1..5e00027edb 100644 --- a/src/app/core/json-patch/json-patch-operations.reducer.ts +++ b/src/app/core/json-patch/json-patch-operations.reducer.ts @@ -11,7 +11,8 @@ import { NewPatchReplaceOperationAction, CommitPatchOperationsAction, StartTransactionPatchOperationsAction, - RollbacktPatchOperationsAction + RollbacktPatchOperationsAction, + DeletePendingJsonPatchOperationsAction } from './json-patch-operations.actions'; import { JsonPatchOperationModel, JsonPatchOperationType } from './json-patch.model'; @@ -101,6 +102,10 @@ export function jsonPatchOperationsReducer(state = initialState, action: PatchOp return startTransactionPatchOperations(state, action as StartTransactionPatchOperationsAction); } + case JsonPatchOperationsActionTypes.DELETE_PENDING_JSON_PATCH_OPERATIONS: { + return deletePendingOperations(state, action as DeletePendingJsonPatchOperationsAction); + } + default: { return state; } @@ -178,6 +183,20 @@ function rollbackOperations(state: JsonPatchOperationsState, action: RollbacktPa } } +/** + * Set the JsonPatchOperationsState to its initial value. + * + * @param state + * the current state + * @param action + * an DeletePendingJsonPatchOperationsAction + * @return JsonPatchOperationsState + * the new state. + */ +function deletePendingOperations(state: JsonPatchOperationsState, action: DeletePendingJsonPatchOperationsAction): JsonPatchOperationsState { + return null; +} + /** * Add new JSON patch operation list. * diff --git a/src/app/core/json-patch/json-patch-operations.service.spec.ts b/src/app/core/json-patch/json-patch-operations.service.spec.ts index 97aba52e56..d1b2948777 100644 --- a/src/app/core/json-patch/json-patch-operations.service.spec.ts +++ b/src/app/core/json-patch/json-patch-operations.service.spec.ts @@ -17,6 +17,7 @@ import { HALEndpointService } from '../shared/hal-endpoint.service'; import { JsonPatchOperationsEntry, JsonPatchOperationsResourceEntry } from './json-patch-operations.reducer'; import { CommitPatchOperationsAction, + DeletePendingJsonPatchOperationsAction, RollbacktPatchOperationsAction, StartTransactionPatchOperationsAction } from './json-patch-operations.actions'; @@ -288,4 +289,19 @@ describe('JsonPatchOperationsService test suite', () => { }); }); + describe('deletePendingJsonPatchOperations', () => { + beforeEach(() => { + store.dispatch.and.callFake(() => { /* */ }); + }); + + it('should dispatch a new DeletePendingJsonPatchOperationsAction', () => { + + const expectedAction = new DeletePendingJsonPatchOperationsAction(); + scheduler.schedule(() => service.deletePendingJsonPatchOperations()); + scheduler.flush(); + + expect(store.dispatch).toHaveBeenCalledWith(expectedAction); + }); + }); + }); diff --git a/src/app/core/json-patch/json-patch-operations.service.ts b/src/app/core/json-patch/json-patch-operations.service.ts index 84d946daba..c3363f4db4 100644 --- a/src/app/core/json-patch/json-patch-operations.service.ts +++ b/src/app/core/json-patch/json-patch-operations.service.ts @@ -10,7 +10,7 @@ import { CoreState } from '../core.reducers'; import { jsonPatchOperationsByResourceType } from './selectors'; import { JsonPatchOperationsResourceEntry } from './json-patch-operations.reducer'; import { - CommitPatchOperationsAction, + CommitPatchOperationsAction, DeletePendingJsonPatchOperationsAction, RollbacktPatchOperationsAction, StartTransactionPatchOperationsAction } from './json-patch-operations.actions'; @@ -105,6 +105,13 @@ export abstract class JsonPatchOperationsService { @@ -25,6 +27,7 @@ describe('SubmissionEditComponent Component', () => { let fixture: ComponentFixture; let submissionServiceStub: SubmissionServiceStub; let itemDataService: ItemDataService; + let submissionJsonPatchOperationsServiceStub: SubmissionJsonPatchOperationsServiceStub; let router: RouterStub; const submissionId = '826'; @@ -46,6 +49,7 @@ describe('SubmissionEditComponent Component', () => { providers: [ { provide: NotificationsService, useClass: NotificationsServiceStub }, { provide: SubmissionService, useClass: SubmissionServiceStub }, + { provide: SubmissionJsonPatchOperationsService, useClass: SubmissionJsonPatchOperationsServiceStub }, { provide: ItemDataService, useValue: itemDataService }, { provide: TranslateService, useValue: getMockTranslateService() }, { provide: Router, useValue: new RouterStub() }, @@ -60,6 +64,7 @@ describe('SubmissionEditComponent Component', () => { fixture = TestBed.createComponent(SubmissionEditComponent); comp = fixture.componentInstance; submissionServiceStub = TestBed.inject(SubmissionService as any); + submissionJsonPatchOperationsServiceStub = TestBed.inject(SubmissionJsonPatchOperationsService as any); router = TestBed.inject(Router as any); }); @@ -112,4 +117,16 @@ describe('SubmissionEditComponent Component', () => { expect(comp.submissionDefinition).toBeUndefined(); }); + describe('ngOnDestroy', () => { + it('should call delete pending json patch operations', fakeAsync(() => { + + submissionJsonPatchOperationsServiceStub.deletePendingJsonPatchOperations.and.callFake(() => { /* */ }); + comp.ngOnDestroy(); + + expect(submissionJsonPatchOperationsServiceStub.deletePendingJsonPatchOperations).toHaveBeenCalled(); + })); + + }); + + }); diff --git a/src/app/submission/edit/submission-edit.component.ts b/src/app/submission/edit/submission-edit.component.ts index 34fdcba104..154e73a765 100644 --- a/src/app/submission/edit/submission-edit.component.ts +++ b/src/app/submission/edit/submission-edit.component.ts @@ -17,6 +17,7 @@ import { Item } from '../../core/shared/item.model'; import { getAllSucceededRemoteData } from '../../core/shared/operators'; import { ItemDataService } from '../../core/data/item-data.service'; import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject'; +import { SubmissionJsonPatchOperationsService } from '../../core/submission/submission-json-patch-operations.service'; /** * This component allows to edit an existing workspaceitem/workflowitem. @@ -92,7 +93,8 @@ export class SubmissionEditComponent implements OnDestroy, OnInit { private router: Router, private itemDataService: ItemDataService, private submissionService: SubmissionService, - private translate: TranslateService) { + private translate: TranslateService, + private submissionJsonPatchOperationsService: SubmissionJsonPatchOperationsService) { } /** @@ -149,5 +151,7 @@ export class SubmissionEditComponent implements OnDestroy, OnInit { this.subs .filter((sub) => hasValue(sub)) .forEach((sub) => sub.unsubscribe()); + + this.submissionJsonPatchOperationsService.deletePendingJsonPatchOperations(); } }