import { Component, Input, OnInit } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { BitstreamDataService } from '../../../../core/data/bitstream-data.service';
import { Bitstream } from '../../../../core/shared/bitstream.model';
import { Item } from '../../../../core/shared/item.model';
import { filter, take } from 'rxjs/operators';
import { RemoteData } from '../../../../core/data/remote-data';
import { hasValue } from '../../../../shared/empty.util';
import { PaginatedList } from '../../../../core/data/paginated-list';
import { NotificationsService } from '../../../../shared/notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core';
/**
* This component renders the file section of the item
* inside a 'ds-metadata-field-wrapper' component.
*/
@Component({
selector: 'ds-item-page-file-section',
templateUrl: './file-section.component.html'
})
export class FileSectionComponent implements OnInit {
@Input() item: Item;
label = 'item.page.files';
separator = '
';
bitstreams$: BehaviorSubject;
currentPage: number;
isLoading: boolean;
isLastPage: boolean;
pageSize = 5;
constructor(
protected bitstreamDataService: BitstreamDataService,
protected notificationsService: NotificationsService,
protected translateService: TranslateService
) {
}
ngOnInit(): void {
this.getNextPage();
}
/**
* This method will retrieve the next page of Bitstreams from the external BitstreamDataService call.
* It'll retrieve the currentPage from the class variables and it'll add the next page of bitstreams with the
* already existing one.
* If the currentPage variable is undefined, we'll set it to 1 and retrieve the first page of Bitstreams
*/
getNextPage(): void {
this.isLoading = true;
if (this.currentPage === undefined) {
this.currentPage = 1;
this.bitstreams$ = new BehaviorSubject([]);
} else {
this.currentPage++;
}
this.bitstreamDataService.findAllByItemAndBundleName(this.item, 'ORIGINAL', {
currentPage: this.currentPage,
elementsPerPage: this.pageSize
}).pipe(
filter((bitstreamsRD: RemoteData>) => hasValue(bitstreamsRD) && (hasValue(bitstreamsRD.error) || hasValue(bitstreamsRD.payload))),
take(1),
).subscribe((bitstreamsRD: RemoteData>) => {
if (bitstreamsRD.error) {
this.notificationsService.error(this.translateService.get('file-section.error.header'), `${bitstreamsRD.error.statusCode} ${bitstreamsRD.error.message}`);
} else if (hasValue(bitstreamsRD.payload)) {
const current: Bitstream[] = this.bitstreams$.getValue();
this.bitstreams$.next([...current, ...bitstreamsRD.payload.page]);
this.isLoading = false;
this.isLastPage = this.currentPage === bitstreamsRD.payload.totalPages;
}
});
}
}