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..c6ea7dce 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,7 @@ export class RecordUiService { }) ); } - return (config.canUpdate) - ? config.canUpdate(record).pipe(first()) - : of({ can: true, message: '' }); + return of({ can: true, message: '' }); } /** @@ -227,6 +230,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 +246,7 @@ export class RecordUiService { }) ); } - return (config.canDelete) - ? config.canDelete(record).pipe(first()) - : of({ can: true, message: '' }); + return of({ can: true, message: '' }); } /** @@ -252,6 +257,10 @@ export class RecordUiService { */ canReadRecord$(record: object, type: string): Observable { const config = this.getResourceConfig(type); + // The canRead function takes precedence over permissions + if (config.canRead) { + return config.canRead(record).pipe(first()); + } if (config.permissions) { const permissions = config.permissions(record); return permissions.pipe( @@ -264,9 +273,7 @@ export class RecordUiService { }) ); } - return (config.canRead) - ? config.canRead(record).pipe(first()) - : of({ can: true, message: '' }); + return of({ can: true, message: '' }); } /** @@ -277,14 +284,16 @@ export class RecordUiService { */ canUseRecord$(record: object, type: string): Observable { const config = this.getResourceConfig(type); + // The canUse function takes precedence over permissions + if (config.canUse) { + return config.canUse(record).pipe(first()); + } if (config.permissions) { const permissions = config.permissions(record); if ('canUse' in permissions) { return permissions.canUse; } } - return (config.canUse) - ? config.canUse(record).pipe(first()) - : of({ can: false, message: '' }); + return of({ can: false, message: '' }); } } diff --git a/projects/rero/ng-core/src/lib/record/search/record-search-page.component.ts b/projects/rero/ng-core/src/lib/record/search/record-search-page.component.ts index 13134c03..2d3937c2 100644 --- a/projects/rero/ng-core/src/lib/record/search/record-search-page.component.ts +++ b/projects/rero/ng-core/src/lib/record/search/record-search-page.component.ts @@ -246,9 +246,9 @@ export class RecordSearchPageComponent implements OnInit, OnDestroy { return; } - const defaulSortValue = this.q ? 'defaultQuery' : 'defaultNoQuery'; + const defaultSortValue = this.q ? 'defaultQuery' : 'defaultNoQuery'; config.sortOptions.forEach((option: SortOption) => { - if (option[defaulSortValue] === true) { + if (option[defaultSortValue] === true) { this.sort = option.value; } }); 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 aa3bf933..7b92ebd3 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); } /**