From 769166430ba2c769bdcd3331167b601d084ccf4e Mon Sep 17 00:00:00 2001 From: Bertrand Zuchuat Date: Thu, 29 Jun 2023 10:42:59 +0200 Subject: [PATCH] general: managing multiple resources in a route * Adds 2 new parameters to the ActionStatus interface. * Updates the precedence of canUpdate and canDelete functions. * Updates add button to reflect new routerLink parameter. * Adds a parameter to the deleteRecord function and modify the event. Co-Authored-by: Bertrand Zuchuat --- .../ng-core/src/lib/record/action-status.ts | 5 ++++- .../ng-core/src/lib/record/record-ui.service.ts | 17 ++++++++++------- .../record/search/record-search.component.html | 8 ++++---- .../search/record-search.component.spec.ts | 4 ++-- .../record/search/record-search.component.ts | 7 ++++--- .../result/record-search-result.component.html | 15 ++++++++------- .../record-search-result.component.spec.ts | 4 ++-- .../result/record-search-result.component.ts | 15 +++++++++------ 8 files changed, 43 insertions(+), 32 deletions(-) diff --git a/projects/rero/ng-core/src/lib/record/action-status.ts b/projects/rero/ng-core/src/lib/record/action-status.ts index ef540220..059dcfae 100644 --- a/projects/rero/ng-core/src/lib/record/action-status.ts +++ b/projects/rero/ng-core/src/lib/record/action-status.ts @@ -1,6 +1,7 @@ /* * RERO angular core - * Copyright (C) 2020 RERO + * Copyright (C) 2019-2023 RERO + * Copyright (C) 2019-2023 UCLouvain * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -22,4 +23,6 @@ export interface ActionStatus { can: boolean; message: string; url?: string; + routerLink?: string[]; + type?: string | undefined | null; } diff --git a/projects/rero/ng-core/src/lib/record/record-ui.service.ts b/projects/rero/ng-core/src/lib/record/record-ui.service.ts index 371d8531..d6936c51 100644 --- a/projects/rero/ng-core/src/lib/record/record-ui.service.ts +++ b/projects/rero/ng-core/src/lib/record/record-ui.service.ts @@ -1,6 +1,7 @@ /* * RERO angular core - * Copyright (C) 2020 RERO + * Copyright (C) 2019-2023 RERO + * Copyright (C) 2019-2023 UCLouvain * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -202,6 +203,10 @@ export class RecordUiService { */ canUpdateRecord$(record: object, type: string): Observable { const config = this.getResourceConfig(type); + // The canUpdate function takes precedence over permissions + if (config.canUpdate) { + return config.canUpdate(record).pipe(first()); + } if (config.permissions) { const permissions = config.permissions(record); return permissions.pipe( @@ -214,9 +219,6 @@ export class RecordUiService { }) ); } - return (config.canUpdate) - ? config.canUpdate(record).pipe(first()) - : of({ can: true, message: '' }); } /** @@ -227,6 +229,10 @@ export class RecordUiService { */ canDeleteRecord$(record: object, type: string): Observable { const config = this.getResourceConfig(type); + // The canDelete function takes precedence over permissions + if (config.canDelete) { + return config.canDelete(record).pipe(first()); + } if (config.permissions) { const permissions = config.permissions(record); return permissions.pipe( @@ -239,9 +245,6 @@ export class RecordUiService { }) ); } - return (config.canDelete) - ? config.canDelete(record).pipe(first()) - : of({ can: true, message: '' }); } /** diff --git a/projects/rero/ng-core/src/lib/record/search/record-search.component.html b/projects/rero/ng-core/src/lib/record/search/record-search.component.html index f42ea12b..2f502203 100644 --- a/projects/rero/ng-core/src/lib/record/search/record-search.component.html +++ b/projects/rero/ng-core/src/lib/record/search/record-search.component.html @@ -1,7 +1,7 @@ @@ -32,8 +33,8 @@ class="btn btn-sm btn-outline-primary ml-2" [title]="'Edit' | translate" [name]="'Edit' | translate" - (click)="editRecord(record.id)" - routerLink="edit/{{ record.id }}"> + (click)="editRecord(record.id, updateStatus?.routerLink)" + [routerLink]="updateStatus.routerLink || ['edit', record.id]"> @@ -44,7 +45,7 @@ class="btn btn-sm btn-outline-danger" [title]="'Delete' | translate" [name]="'Delete' | translate" - (click)="deleteRecord(record.id)"> + (click)="deleteRecord(record.id, deleteStatus?.type)"> diff --git a/projects/rero/ng-core/src/lib/record/search/result/record-search-result.component.spec.ts b/projects/rero/ng-core/src/lib/record/search/result/record-search-result.component.spec.ts index 9967b297..37797a2d 100644 --- a/projects/rero/ng-core/src/lib/record/search/result/record-search-result.component.spec.ts +++ b/projects/rero/ng-core/src/lib/record/search/result/record-search-result.component.spec.ts @@ -79,8 +79,8 @@ describe('RecordSearchResultComponent', () => { }); it('should delete record', () => { - component.deletedRecord.subscribe((pid: string) => { - expect(pid).toBe('1'); + component.deletedRecord.subscribe((result: { pid: string, type: string | undefined | null }) => { + expect(result.pid).toBe('1'); }); component.deleteRecord('1'); }); diff --git a/projects/rero/ng-core/src/lib/record/search/result/record-search-result.component.ts b/projects/rero/ng-core/src/lib/record/search/result/record-search-result.component.ts index 5c1d6c25..2ecfc989 100644 --- a/projects/rero/ng-core/src/lib/record/search/result/record-search-result.component.ts +++ b/projects/rero/ng-core/src/lib/record/search/result/record-search-result.component.ts @@ -1,6 +1,7 @@ /* * RERO angular core - * Copyright (C) 2020 RERO + * Copyright (C) 2019-2023 RERO + * Copyright (C) 2019-2023 UCLouvain * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -113,7 +114,7 @@ export class RecordSearchResultComponent implements OnInit { /** * Event emitted when a record is deleted */ - @Output() deletedRecord = new EventEmitter(); + @Output() deletedRecord = new EventEmitter<{pid: string, type?: string | undefined | null }>(); /** * Directive for displaying search results @@ -190,17 +191,19 @@ export class RecordSearchResultComponent implements OnInit { /** * Delete a record * @param pid - string, pid to delete + * @param type - string, resource type */ - deleteRecord(pid: string) { - return this.deletedRecord.emit(pid); + deleteRecord(pid: string, type?: string) { + return this.deletedRecord.emit({ pid, type }); } /** * Edit a record * @param pid - string: the pid to edit */ - editRecord(pid: string) { - this._router.navigate(['/', 'records', this.type, 'edit', pid]); + editRecord(pid: string, url?: string[]) { + const params = url ?? ['/', 'records', this.type, 'edit', pid]; + this._router.navigate(params); } /**