90873: Add default bundle suggestions on upload bundle

This commit is contained in:
Alexandre Vryghem
2022-04-19 15:55:44 +02:00
parent 99aee55efa
commit beae47ef19
4 changed files with 51 additions and 27 deletions

View File

@@ -1,4 +1,4 @@
<div class="container" *ngVar="(bundlesRD$ | async)?.payload?.page as bundles">
<div class="container">
<ng-container *ngIf="bundles">
<div class="row">
<div class="col-12 mb-4">

View File

@@ -21,9 +21,8 @@ import { createPaginatedList } from '../../../shared/testing/utils.test';
import { RouterStub } from '../../../shared/testing/router.stub';
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub';
import { AuthServiceStub } from '../../../shared/testing/auth-service.stub';
import { getTestScheduler } from 'jasmine-marbles';
describe('UploadBistreamComponent', () => {
describe('UploadBitstreamComponent', () => {
let comp: UploadBitstreamComponent;
let fixture: ComponentFixture<UploadBitstreamComponent>;
@@ -94,22 +93,6 @@ describe('UploadBistreamComponent', () => {
removeByHrefSubstring: {}
});
describe('on init', () => {
beforeEach(waitForAsync(() => {
createUploadBitstreamTestingModule({
bundle: bundle.id
});
}));
beforeEach(() => {
loadFixtureAndComp();
});
it('should initialize the bundles', () => {
expect(comp.bundlesRD$).toBeDefined();
getTestScheduler().expectObservable(comp.bundlesRD$).toBe('(a|)', {a: createSuccessfulRemoteDataObject([bundle])});
});
});
describe('when a file is uploaded', () => {
beforeEach(waitForAsync(() => {
createUploadBitstreamTestingModule({});

View File

@@ -1,8 +1,8 @@
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { Observable, Subscription, of as observableOf } from 'rxjs';
import { RemoteData } from '../../../core/data/remote-data';
import { Item } from '../../../core/shared/item.model';
import { map, take } from 'rxjs/operators';
import { map, take, switchMap } from 'rxjs/operators';
import { ActivatedRoute, Router } from '@angular/router';
import { UploaderOptions } from '../../../shared/uploader/uploader-options.model';
import { hasValue, isEmpty, isNotEmpty } from '../../../shared/empty.util';
@@ -13,11 +13,13 @@ import { TranslateService } from '@ngx-translate/core';
import { PaginatedList } from '../../../core/data/paginated-list.model';
import { Bundle } from '../../../core/shared/bundle.model';
import { BundleDataService } from '../../../core/data/bundle-data.service';
import { getFirstSucceededRemoteDataPayload } from '../../../core/shared/operators';
import { getFirstSucceededRemoteDataPayload, getFirstCompletedRemoteData } from '../../../core/shared/operators';
import { UploaderComponent } from '../../../shared/uploader/uploader.component';
import { RequestService } from '../../../core/data/request.service';
import { getBitstreamModuleRoute } from '../../../app-routing-paths';
import { getEntityEditRoute } from '../../item-page-routing-paths';
import { environment } from '../../../../environments/environment';
import { StandardBundleConfig } from '../../../../config/standard-bundle-config.interface';
@Component({
selector: 'ds-upload-bitstream',
@@ -49,9 +51,9 @@ export class UploadBitstreamComponent implements OnInit, OnDestroy {
itemRD$: Observable<RemoteData<Item>>;
/**
* The item's bundles
* The item's bundles and default the default bundles that should be suggested (defined in environment)
*/
bundlesRD$: Observable<RemoteData<PaginatedList<Bundle>>>;
bundles: Bundle[] = [];
/**
* The ID of the currently selected bundle to upload a bitstream to
@@ -99,7 +101,6 @@ export class UploadBitstreamComponent implements OnInit, OnDestroy {
/**
* Initialize component properties:
* itemRD$ Fetched from the current route data (populated by BitstreamPageResolver)
* bundlesRD$ List of bundles on the item
* selectedBundleId Starts off by checking if the route's queryParams contain a "bundle" parameter. If none is found,
* the ID of the first bundle in the list is selected.
* Calls setUploadUrl after setting the selected bundle
@@ -108,7 +109,39 @@ export class UploadBitstreamComponent implements OnInit, OnDestroy {
this.itemId = this.route.snapshot.params.id;
this.entityType = this.route.snapshot.params['entity-type'];
this.itemRD$ = this.route.data.pipe(map((data) => data.dso));
this.bundlesRD$ = this.itemService.getBundles(this.itemId);
const bundlesRD$ = this.itemService.getBundles(this.itemId).pipe(
getFirstCompletedRemoteData(),
switchMap((remoteData: RemoteData<PaginatedList<Bundle>>) => {
if (remoteData.hasSucceeded) {
if (remoteData.payload.page) {
this.bundles = remoteData.payload.page;
} else if (environment.standardBundles) {
this.bundles = environment.standardBundles.map((defaultBundle: StandardBundleConfig) => Object.assign(new Bundle(), {
_name: defaultBundle.bundle,
type: 'bundle'
})
);
}
if (remoteData.payload.page && environment.standardBundles) {
for (const defaultBundle of environment.standardBundles) {
let check = true;
remoteData.payload.page.forEach((bundle: Bundle) => {
// noinspection JSDeprecatedSymbols
if (defaultBundle.bundle === bundle.name) {
check = false;
}
});
if (check) {
this.bundles.push(Object.assign(new Bundle(), {
_name: defaultBundle.bundle,
type: 'bundle'
}));
}
}
}
return observableOf(remoteData.payload.page);
}
}));
this.selectedBundleId = this.route.snapshot.queryParams.bundle;
if (isNotEmpty(this.selectedBundleId)) {
this.bundleService.findById(this.selectedBundleId).pipe(
@@ -118,6 +151,7 @@ export class UploadBitstreamComponent implements OnInit, OnDestroy {
});
this.setUploadUrl();
}
this.subs.push(bundlesRD$.subscribe());
}
/**
@@ -142,6 +176,13 @@ export class UploadBitstreamComponent implements OnInit, OnDestroy {
*/
bundleNameChange() {
this.selectedBundleId = undefined;
for (const bundle of this.bundles) {
// noinspection JSDeprecatedSymbols
if (this.selectedBundleName === bundle.name) {
this.selectedBundleId = bundle.id;
break;
}
}
}
/**

View File

@@ -1643,7 +1643,7 @@
"item.bitstreams.upload.bundle": "Bundle",
"item.bitstreams.upload.bundle.placeholder": "Select a bundle",
"item.bitstreams.upload.bundle.placeholder": "Select a bundle or input new bundle name",
"item.bitstreams.upload.bundle.new": "Create bundle",