1
0
Files
yel-dspace-angular/src/app/shared/object-collection/shared/badges/access-status-badge/access-status-badge.component.ts

63 lines
2.3 KiB
TypeScript

import { Component, Input } from '@angular/core';
import { catchError, map } from 'rxjs/operators';
import { Observable, of as observableOf } from 'rxjs';
import { AccessStatusObject } from './access-status.model';
import { hasValue } from '../../../../empty.util';
import { environment } from 'src/environments/environment';
import { AccessStatusDataService } from 'src/app/core/data/access-status-data.service';
import { DSpaceObject } from '../../../../../core/shared/dspace-object.model';
import { ITEM_TYPE } from '../../../../../core/shared/item-relationships/item-type.resource-type';
import { Item } from '../../../../../core/shared/item.model';
import { ITEM } from '../../../../../core/shared/item.resource-type';
@Component({
selector: 'ds-access-status-badge',
templateUrl: './access-status-badge.component.html'
})
/**
* Component rendering the access status of an item as a badge
*/
export class AccessStatusBadgeComponent {
@Input() object: DSpaceObject;
accessStatus$: Observable<string>;
/**
* Whether to show the access status badge or not
*/
showAccessStatus: boolean;
/**
* Initialize instance variables
*
* @param {AccessStatusDataService} accessStatusDataService
*/
constructor(private accessStatusDataService: AccessStatusDataService) { }
ngOnInit(): void {
this.showAccessStatus = environment.item.showAccessStatuses;
if (this.object.type.toString() !== ITEM.value || !this.showAccessStatus || this.object == null) {
// Do not show the badge if the feature is inactive or if the item is null.
return;
}
const item = this.object as Item;
if (item.accessStatus == null) {
// In case the access status has not been loaded, do it individually.
item.accessStatus = this.accessStatusDataService.findAccessStatusFor(item);
}
this.accessStatus$ = item.accessStatus.pipe(
map((accessStatusRD) => {
if (accessStatusRD.statusCode !== 401 && hasValue(accessStatusRD.payload)) {
return accessStatusRD.payload;
} else {
return [];
}
}),
map((accessStatus: AccessStatusObject) => hasValue(accessStatus.status) ? accessStatus.status : 'unknown'),
map((status: string) => `access-status.${status.toLowerCase()}.listelement.badge`),
catchError(() => observableOf('access-status.unknown.listelement.badge'))
);
}
}