add flush operation, fix lint, refactor

This commit is contained in:
FrancescoMolinaro
2023-12-21 16:08:13 +01:00
parent 128dd2f566
commit 6d16bf3037
4 changed files with 73 additions and 27 deletions

View File

@@ -1,5 +1,6 @@
import { Store } from '@ngrx/store';
import {
FlushPatchOperationAction,
NewPatchAddOperationAction,
NewPatchMoveOperationAction,
NewPatchRemoveOperationAction,
@@ -99,6 +100,20 @@ export class JsonPatchOperationsBuilder {
path.path));
}
/**
* Dispatches a new FlushPatchOperationAction
*
* @param path
* a JsonPatchOperationPathObject representing path
*/
flushOperation(path: JsonPatchOperationPathObject) {
this.store.dispatch(
new FlushPatchOperationAction(
path.rootElement,
path.subRootElement,
path.path));
}
protected prepareValue(value: any, plain: boolean, first: boolean) {
let operationValue: any = null;
if (hasValue(value)) {

View File

@@ -12,7 +12,7 @@ import {
CommitPatchOperationsAction,
StartTransactionPatchOperationsAction,
RollbacktPatchOperationsAction,
DeletePendingJsonPatchOperationsAction
DeletePendingJsonPatchOperationsAction, FlushPatchOperationAction
} from './json-patch-operations.actions';
import { JsonPatchOperationModel, JsonPatchOperationType } from './json-patch.model';
@@ -71,7 +71,7 @@ export function jsonPatchOperationsReducer(state = initialState, action: PatchOp
}
case JsonPatchOperationsActionTypes.FLUSH_JSON_PATCH_OPERATIONS: {
return flushOperation(state, action as FlushPatchOperationsAction);
return flushOperations(state, action as FlushPatchOperationsAction);
}
case JsonPatchOperationsActionTypes.NEW_JSON_PATCH_ADD_OPERATION: {
@@ -106,6 +106,10 @@ export function jsonPatchOperationsReducer(state = initialState, action: PatchOp
return deletePendingOperations(state, action as DeletePendingJsonPatchOperationsAction);
}
case JsonPatchOperationsActionTypes.FLUSH_JSON_PATCH_OPERATION: {
return flushOperation(state, action as FlushPatchOperationAction);
}
default: {
return state;
}
@@ -197,6 +201,39 @@ function deletePendingOperations(state: JsonPatchOperationsState, action: Delete
return null;
}
/**
* Flush one operation from JsonPatchOperationsState.
*
* @param state
* the current state
* @param action
* an FlushPatchOperationsAction
* @return JsonPatchOperationsState
* the new state.
*/
function flushOperation(state: JsonPatchOperationsState, action: FlushPatchOperationAction): JsonPatchOperationsState {
const payload = action.payload;
if (state[payload.resourceType] && state[payload.resourceType].children) {
const body = state[payload.resourceType].children[payload.resourceId].body;
const operation = body.filter(operations => operations.operation.path === payload.path)[0];
const operationIndex = body.indexOf(operation);
const newBody = [...body];
newBody.splice(operationIndex, 1);
return Object.assign({}, state, {
[action.payload.resourceType]: Object.assign({}, {
children: {
[action.payload.resourceId]: {
body: newBody,
}
},
})
});
} else {
return state;
}
}
/**
* Add new JSON patch operation list.
*
@@ -273,7 +310,7 @@ function hasValidBody(state: JsonPatchOperationsState, resourceType: any, resour
* @return SubmissionObjectState
* the new state, with the section new validity status.
*/
function flushOperation(state: JsonPatchOperationsState, action: FlushPatchOperationsAction): JsonPatchOperationsState {
function flushOperations(state: JsonPatchOperationsState, action: FlushPatchOperationsAction): JsonPatchOperationsState {
if (hasValue(state[ action.payload.resourceType ])) {
let newChildren;
if (isNotUndefined(action.payload.resourceId)) {

View File

@@ -70,9 +70,12 @@
</div>
</div>
</div>
<button type="button" [class.mt-2]="serviceIndex > 0" class="btn btn-secondary ml-2" role="button"
<button *ngIf="ldnServiceByPattern[ldnPattern.pattern].services.length > 1"
type="button" [class.mt-2]="serviceIndex > 0"
class="btn btn-secondary ml-2"
role="button"
title="{{'form.remove' | translate}}"
attr.aria-label="{{'form.remove' | translate}}"
[attr.aria-label]="'form.remove' | translate"
(click)="removeService(ldnPattern, serviceIndex)"
>
<span><i class="fas fa-trash" aria-hidden="true"></i></span>

View File

@@ -8,7 +8,7 @@ import { JsonPatchOperationsBuilder } from '../../../core/json-patch/builder/jso
import { SectionsService } from '../sections.service';
import { SectionDataObject } from '../models/section-data.model';
import { hasNoValue, hasValue, isEmpty, isNotEmpty } from '../../../shared/empty.util';
import { hasValue, isEmpty, isNotEmpty } from '../../../shared/empty.util';
import { getFirstCompletedRemoteData, getPaginatedListPayload, getRemoteDataPayload } from '../../../core/shared/operators';
import { LdnServicesService } from '../../../admin/admin-ldn-services/ldn-services-data/ldn-services-data.service';
@@ -20,7 +20,7 @@ import { CoarNotifyConfigDataService } from './coar-notify-config-data.service';
import { filter, map, take, tap } from 'rxjs/operators';
import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap';
import { SubmissionSectionError } from '../../objects/submission-section-error.model';
import { LdnPattern } from "./submission-coar-notify.config";
import { LdnPattern } from './submission-coar-notify.config';
/**
* This component represents a section that contains the submission section-coar-notify form.
@@ -103,17 +103,7 @@ export class SubmissionSectionCoarNotifyComponent extends SectionModelComponent
getFirstCompletedRemoteData()
).subscribe((data) => {
if (data.hasSucceeded) {
// remove mock data after impl
this.patterns = [ {
"pattern":"request-review",
"multipleRequest":true
},{
"pattern":"request-endorsement",
"multipleRequest":true
},{
"pattern":"request-ingest",
"multipleRequest":false
} ];
this.patterns = data.payload.page[0].patterns;
this.initSelectedServicesByPattern();
}
}));
@@ -123,6 +113,7 @@ export class SubmissionSectionCoarNotifyComponent extends SectionModelComponent
* Handles the change event of a select element.
* @param pattern - The pattern of the select element.
* @param index - The index of the select element.
* @param selectedService - The selected LDN service.
*/
onChange(pattern: string, index: number, selectedService: LdnService | null) {
// do nothing if the selected value is the same as the previous one
@@ -145,10 +136,11 @@ export class SubmissionSectionCoarNotifyComponent extends SectionModelComponent
const hasPrevValueStored = hasValue(this.previousServices[pattern].services[index]) && this.previousServices[pattern].services[index].id !== selectedService?.id;
if (hasPrevValueStored) {
// replace the path
// when there is a previous value stored and it is different from the new one
this.operationsBuilder.replace(this.pathCombiner.getPath([pattern, index.toString()]), selectedService?.id, true);
} else {
this.operationsBuilder.flushOperation(this.pathCombiner.getPath([pattern, '-']));
}
if (!hasPrevValueStored || (selectedService?.id && hasPrevValueStored)) {
// add the path when there is no previous value stored
this.operationsBuilder.add(this.pathCombiner.getPath([pattern, '-']), [selectedService.id], false, true);
}
@@ -171,12 +163,11 @@ export class SubmissionSectionCoarNotifyComponent extends SectionModelComponent
this.subs.push(
this.filterServices(ldnPattern.pattern)
.subscribe((services: LdnService[]) => {
const selectedServices = services.filter((service) => {
this.ldnServiceByPattern[ldnPattern.pattern].services = services.filter((service) => {
const selection = (this.sectionData.data[ldnPattern.pattern] as LdnService[]).find((s: LdnService) => s.id === service.id);
this.addService(ldnPattern, selection);
return this.sectionData.data[ldnPattern.pattern].includes(service.id);
});
this.ldnServiceByPattern[ldnPattern.pattern].services = selectedServices;
})
);
} else {
@@ -191,7 +182,7 @@ export class SubmissionSectionCoarNotifyComponent extends SectionModelComponent
/**
* Adds a new service to the selected services for the given pattern.
* @param pattern - The pattern to add the new service to.
* @param ldnPattern - The pattern to add the new service to.
* @param newService - The new service to add.
*/
addService(ldnPattern: LdnPattern, newService: LdnService) {
@@ -216,7 +207,7 @@ export class SubmissionSectionCoarNotifyComponent extends SectionModelComponent
// Remove the service at the specified index from the array
this.ldnServiceByPattern[ldnPattern.pattern].services.splice(serviceIndex, 1);
this.previousServices[ldnPattern.pattern]?.services.splice(serviceIndex, 1);
this.operationsBuilder.replace(this.pathCombiner.getPath([ldnPattern.pattern, serviceIndex.toString()]), null, true);
this.operationsBuilder.flushOperation(this.pathCombiner.getPath([ldnPattern.pattern, '-']));
this.sectionService.dispatchRemoveSectionErrors(this.submissionId, this.sectionData.id);
}
if (!this.ldnServiceByPattern[ldnPattern.pattern].services.length) {