diff --git a/projects/admin/src/app/api/local-field-api.service.ts b/projects/admin/src/app/api/local-field-api.service.ts index 8feeddb49..142580c00 100644 --- a/projects/admin/src/app/api/local-field-api.service.ts +++ b/projects/admin/src/app/api/local-field-api.service.ts @@ -16,7 +16,8 @@ */ import { Injectable } from '@angular/core'; -import { RecordService, RecordUiService } from '@rero/ng-core'; +import { Record, RecordService, RecordUiService } from '@rero/ng-core'; +import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; @Injectable({ @@ -40,24 +41,18 @@ export class LocalFieldApiService { * @param organisationPid - string, pid of organisation * @return Observable */ - getByResourceTypeAndResourcePidAndOrganisationId( - resourceType: string, - resourcePid: string, - organisationPid: string - ) { - const query = [ - `parent.type:${resourceType}`, - `parent.pid:${resourcePid}`, - `organisation.pid:${organisationPid}`, - ].join(' AND '); + getByResourceTypeAndResourcePidAndOrganisationId(resourceType: string, resourcePid: string, organisationPid: string): Observable { + const query = `parent.type:${resourceType} AND parent.pid:${resourcePid} AND organisation.pid:${organisationPid}`; - return this._recordService.getRecords('local_fields', query, 1, 1).pipe( - map((result: any) => { - return this._recordService.totalHits(result.hits.total) === 0 - ? {} - : result.hits.hits[0]; - }) - ); + return this._recordService + .getRecords('local_fields', query, 1, 1) + .pipe( + map((result: Record) => { + return this._recordService.totalHits(result.hits.total) === 0 + ? {} + : result.hits.hits[0]; + }) + ); } /** diff --git a/projects/admin/src/app/record/detail-view/local-field/local-field.component.html b/projects/admin/src/app/record/detail-view/local-field/local-field.component.html index 1f0b0d0d2..292972335 100644 --- a/projects/admin/src/app/record/detail-view/local-field/local-field.component.html +++ b/projects/admin/src/app/record/detail-view/local-field/local-field.component.html @@ -1,6 +1,7 @@ - - - -
- - {{ 'local_' + field.key }} - - - {{ field.value | join:'; ' }} - -
-
-
+ +
+
+ +
{{ 'local_' + field.name }}
+
{{ field.value | join:'; ' }}
+
+
+ +
- +
-
+
; + + /** Pid of the LocalField record */ + localFieldRecordPid: string; + /** local fields */ + localFields: Array<{name: string, value: Array}> = []; + /** isLoading */ + isLoading = true; + /** return all available permissions for current user */ + permissions: IPermissions = PERMISSIONS; + /** recordPermissions */ + recordPermissions: any; + /** Available resources type for local fields */ - resourceTypes = { + private _resourceTypes = { documents: 'doc', holdings: 'hold', items: 'item' }; - /** recordPermissions */ - recordPermissions: any; - /** return all available permissions for current user */ - permissions: IPermissions = PERMISSIONS; - /** all component subscription */ private _subscriptions = new Subscription(); @@ -65,17 +70,31 @@ export class LocalFieldComponent implements OnInit, OnDestroy { /** OnInit hook */ ngOnInit() { - this.record$ = this._localFieldApiService.getByResourceTypeAndResourcePidAndOrganisationId( - this._translateType(this.resourceType), - this.resourcePid, - this._userService.user.currentOrganisation - ); - this.record$.subscribe((record) => { - this._subscriptions.add(this._recordPermissionService - .getPermission('local_fields', record.metadata.pid) - .subscribe((permissions) => this.recordPermissions = permissions) - ); - }); + this._localFieldApiService + .getByResourceTypeAndResourcePidAndOrganisationId( + this._translateType(this.resourceType), + this.resourcePid, + this._userService.user.currentOrganisation + ) + .subscribe((record: any) => { + if (record?.metadata) { + this.localFieldRecordPid = record.metadata.pid; + if (record.metadata?.fields) { + const fields = record.metadata.fields; + // Sort local fields using numeric part of the field name. + const sortKeys = Object.keys(fields).sort((k1, k2) => parseInt(k1.replace(/\D/g, '')) - parseInt(k2.replace(/\D/g, ''))); + for (const key of sortKeys) { + this.localFields.push({name: key, value: fields[key]}); + } + } + // Permission loading + this._subscriptions.add(this._recordPermissionService + .getPermission('local_fields', record.metadata.pid) + .subscribe((permissions) => this.recordPermissions = permissions) + ); + } + this.isLoading = false; + }); } /** OnDestroy hook */ @@ -83,26 +102,28 @@ export class LocalFieldComponent implements OnInit, OnDestroy { this._subscriptions.unsubscribe() } - /** - * Delete - */ - delete(resourcePid: string) { - this._localFieldApiService.delete(resourcePid).subscribe((success: any) => { - if (success) { - this.record$ = of({}); - } - }); + // PUBLIC FUNCTIONS ========================================================= + /** Delete the complete LocalField resource. */ + delete(): void { + this._localFieldApiService + .delete(this.localFieldRecordPid) + .subscribe((success: any) => { + if (success) { + this.localFields = []; + } + }); } + // PRIVATE FUNCTIONS ======================================================== /** * Translate resource type to symbol - * @param typeOfResource - string: type of resource - * @return string: resource symbol + * @param resourceType - string, name of resource + * @return string, resource symbol */ - private _translateType(typeOfResource: string) { - if (typeOfResource in this.resourceTypes) { - return this.resourceTypes[typeOfResource]; + private _translateType(resourceType: string): string { + if (resourceType in this._resourceTypes) { + return this._resourceTypes[resourceType]; } - throw new Error(`Local fields: missing resource type ${typeOfResource}.`); + throw new Error(`Local fields: missing resource type ${resourceType}.`); } }