Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documents: refactoring action buttons #1090

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion projects/admin/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ import { PreviewEmailModule } from './shared/preview-email/preview-email.module'
import { CurrentLibraryPermissionValidator } from './utils/permissions';
import { CustomShortcutHelpComponent } from './widgets/custom-shortcut-help/custom-shortcut-help.component';
import { FrontpageComponent } from './widgets/frontpage/frontpage.component';
import { DocumentDetailComponent } from './record/detail-view/document-detail-view/document-detail/document-detail.component';

/** Init application factory */
export function appInitFactory(appInitializerService: AppInitializerService): () => Promise<any> {
Expand Down Expand Up @@ -344,7 +345,8 @@ export function appInitFactory(appInitializerService: AppInitializerService): ()
DocumentAdvancedSearchFormComponent,
RepeatTypeComponent,
FieldCustomInputTypeComponent,
DocumentAdvancedSearchComponent
DocumentAdvancedSearchComponent,
DocumentDetailComponent
],
imports: [
AppRoutingModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,6 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<!-- import button for external source importation such as BNF -->
<div *ngIf="record && record.metadata && !record.metadata.pid && pid && source" class="float-right ml-4 mt-2 mb-4" [permissions]="permissions.DOC_CREATE">
<a (click)="importDocument($event, record, { source: source, pid: pid })"
class="btn btn-sm btn-outline-primary"
translate>Import</a>
</div>
<!-- duplicate button -->
<div *ngIf="record && record.metadata && record.metadata.pid" class="float-right mt-2 mb-4 mr-n4 btn-duplicate" [permissions]="permissions.DOC_CREATE">
<a [routerLink]="['/records', 'documents', 'duplicate']"
[queryParams]="{type: 'documents', pid: record.metadata.pid}"
class="btn btn-sm btn-outline-primary"
translate>Duplicate</a>
</div>
<div class="clearfix"></div>
<ng-container *ngIf="record">
<!-- HEADER -->
<header class="row mt-5 mb-3">
Expand Down Expand Up @@ -254,7 +240,7 @@ <h3 id="{{ 'doc-altgr-title-' + i }}">{{ altgr_title }}</h3>
<!-- END ENTITIES RELATED -->
<!-- LOCAL FIELDS TAB -->
<tab
*ngIf="!record.metadata.harvested && record.metadata.pid && showhideLocalFieldsTab"
*ngIf="!record.metadata.harvested && record.metadata.pid && showHideLocalFieldsTab"
id="documents-local-field-tab"
tabOrder="4"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { Record, RecordService } from '@rero/ng-core';
import { DetailRecord } from '@rero/ng-core/lib/record/detail/view/detail-record';
import { DetailRecord, RecordService } from '@rero/ng-core';
import { IPermissions, PERMISSIONS, PermissionsService } from '@rero/shared';
import { cloneDeep } from 'lodash-es';
import { BsModalService } from 'ngx-bootstrap/modal';
import { Observable, of, Subscription } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { DocumentApiService } from '../../../api/document-api.service';
import { IdentifierTypes } from '../../../classes/identifiers';
import { OperationLogsService } from '../../../service/operation-logs.service';
import { DialogImportComponent } from './dialog-import/dialog-import.component';

@Component({
selector: 'admin-document-detail-view',
Expand Down Expand Up @@ -65,24 +59,6 @@ export class DocumentDetailViewComponent implements DetailRecord, OnInit, OnDest
/** Enables or disables links */
activateLink: boolean = true;

/** Mapping types for import */
private _mappingtypes = {
'bf:Ean': 'bf:Isbn',
'bf:Isbn': 'bf:Isbn',
'bf:Issn': 'bf:IssnL',
'bf:IssnL': 'bf:Issn'
};

/** Identifiers used to check for duplicates */
private _identifiersList = [
IdentifierTypes.ISBN,
IdentifierTypes.ISSN,
IdentifierTypes.DOI,
IdentifierTypes.LCCN,
IdentifierTypes.L_ISSN,
IdentifierTypes.EAN
];

/**
* Is operation log enabled
* @return boolean
Expand All @@ -91,14 +67,6 @@ export class DocumentDetailViewComponent implements DetailRecord, OnInit, OnDest
return this._operationLogsService.isLogVisible('documents');
}

/** Source for imported record. */
get source() {
if (this._activatedRouter.snapshot && this._activatedRouter.snapshot.params && this._activatedRouter.snapshot.params.type !== null) {
return this._activatedRouter.snapshot.params.type.replace('import_', '');
}
return null;
}

/** External identifier for imported record. */
get pid(): string | null {
if (this._activatedRouter.snapshot && this._activatedRouter.snapshot.params && this._activatedRouter.snapshot.params.pid !== null) {
Expand All @@ -122,7 +90,7 @@ export class DocumentDetailViewComponent implements DetailRecord, OnInit, OnDest
* Show or hide local fields tab
* @return boolean - if False, hide the local fields tab
*/
get showhideLocalFieldsTab(): boolean {
get showHideLocalFieldsTab(): boolean {
return this._permissionsService.canAccess([PERMISSIONS.LOFI_SEARCH, PERMISSIONS.LOFI_CREATE]);
}

Expand All @@ -132,8 +100,6 @@ export class DocumentDetailViewComponent implements DetailRecord, OnInit, OnDest
* @param _activatedRouter - ActivatedRoute to get url parameters.
* @param _recordService - RecordService to the MARC version for the record.
* @param _operationLogsService - OperationLogsService
* @param _router - Router
* @param _bsModalService - BsModalService
* @param _documentApiService - DocumentApiService
* @param _permissionsService - PermissionsService
*/
Expand All @@ -142,8 +108,6 @@ export class DocumentDetailViewComponent implements DetailRecord, OnInit, OnDest
private _activatedRouter: ActivatedRoute,
private _recordService: RecordService,
private _operationLogsService: OperationLogsService,
private _router: Router,
private _bsModalService: BsModalService,
private _documentApiService: DocumentApiService,
private _permissionsService: PermissionsService
) { }
Expand Down Expand Up @@ -186,73 +150,6 @@ export class DocumentDetailViewComponent implements DetailRecord, OnInit, OnDest
return ('key' in element && element.key === 'bf:Publication');
}

/**
* Import Document
* @param event - Event
* @param record - Current record
* @param data - Import source reference
*/
importDocument(event: Event, record: any, data: { source: string, pid: string }): void {
event.preventDefault();
const rec = record.metadata;
const route = ['/records', 'documents', 'new'];
const queryParams = [];
// If we have identifiers
if (rec.identifiedBy) {
// We select only a defined part of the identifier
rec.identifiedBy
.filter((identifier: any) => this._identifiersList.includes(identifier.type))
.map((identifier: any) => {
queryParams.push(this._extractAndFormatQueryParams(identifier));
if (identifier.type in this._mappingtypes) {
const cidentifier = cloneDeep(identifier);
cidentifier.type = this._mappingtypes[cidentifier.type];
queryParams.push(this._extractAndFormatQueryParams(cidentifier));
}
});
}

// If we do not have a identifier and we have a title
// TODO: Nice to have - create an backend API entrypoint to detect the duplicate resource.
// https://github.com/rero/rero-ils/issues/2900 (Generalize to all resources)
if (queryParams.length === 0 && rec.title) {
// We extract the title
const titles = rec.title.filter((rtitle: any) => rtitle.type === 'bf:Title' && '_text' in rtitle);
if (titles.length > 0) {
// We clean the text string by deleting some characters
const regex = /["\[\]]/gi;
queryParams.push(`title._text:"${titles[0]._text.replace(regex, '')}"`);
}
}

// No identifier and no title, we redirect to the add form.
if (queryParams.length === 0) {
this._router.navigate(route, { queryParams: data });
} else {
// Find documents(s) with query params
const query = queryParams.join(' OR ');
this._recordService.getRecords(
'documents', query, 1, undefined, undefined, undefined, { accept: 'application/rero+json' }
).subscribe((response: Record) => {
if (this._recordService.totalHits(response.hits.total) === 0) {
this._router.navigate(route, { queryParams: data });
} else {
const config = {
initialState: {
records: response.hits.hits
}
};
const bsModalRef = this._bsModalService.show(DialogImportComponent, config);
bsModalRef.content.confirmation$.subscribe((confirmation: boolean) => {
if (confirmation) {
this._router.navigate(route, { queryParams: data });
}
});
}
});
}
}

/**
* Format "part of" numbering for display
*
Expand Down Expand Up @@ -295,21 +192,6 @@ export class DocumentDetailViewComponent implements DetailRecord, OnInit, OnDest
}
}

/**
* Extract and format query params
* @param identifier - IdentifiedBy object
* @return string, query formatted
*/
private _extractAndFormatQueryParams(identifier: any): string {
const query = [];
query.push(`identifiedBy.type:"${identifier.type}"`);
query.push(`identifiedBy.value:"${identifier.value}"`);
if (identifier.source) {
query.push(`identifiedBy.source:"${identifier.source}"`);
}
return `(${query.join(' AND ')})`;
}

/**
* Process related resources
* @param record - Record metadata
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!--
RERO angular core
Copyright (C) 2020-2023 RERO

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
the Free Software Foundation, version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<div class="main-content">
<ng-core-detail-button
*ngIf="record"
[record]="record"
[type]="type"
[adminMode]="adminMode"
[useStatus]="useStatus"
[updateStatus]="updateStatus"
[deleteStatus]="deleteStatus"
(recordEvent)="recordEvent($event)"
(deleteMessageEvent)="showDeleteMessage($event)"
>
<ng-container beforeButton>
<ng-container *ngIf="record">
<button
*ngIf="!record.metadata.pid && source"
class="btn btn-sm btn-outline-primary ml-1"
[permissions]="permissions.DOC_CREATE"
(click)="importDocument($event, record, { source: source, pid: pid })"
translate>Import</button>
<button
*ngIf="record.metadata.pid"
class="btn btn-sm btn-outline-primary ml-1"
[permissions]="permissions.DOC_CREATE"
[routerLink]="['/records', 'documents', 'duplicate']"
[queryParams]="{type: 'documents', pid: record.metadata.pid}"
translate>Duplicate</button>
</ng-container>
</ng-container>
</ng-core-detail-button>
<ng-core-error [error]="error" *ngIf="error"></ng-core-error>
<ng-template ngCoreRecordDetail></ng-template>
<ng-core-record-files [type]="type" [pid]="record.id"
*ngIf="record && filesEnabled && updateStatus && updateStatus.can"></ng-core-record-files>
</div>
Loading
Loading