|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { |
| 3 | + combineLatest, |
| 4 | + Observable, |
| 5 | + of as observableOf, |
| 6 | + switchMap, |
| 7 | +} from 'rxjs'; |
| 8 | + |
| 9 | +import { getDSORoute } from '../../app-routing-paths'; |
| 10 | +import { Breadcrumb } from '../../breadcrumbs/breadcrumb/breadcrumb.model'; |
| 11 | +import { hasValue } from '../../shared/empty.util'; |
| 12 | +import { SubmissionService } from '../../submission/submission.service'; |
| 13 | +import { BreadcrumbsProviderService } from '../breadcrumbs/breadcrumbsProviderService'; |
| 14 | +import { DSOBreadcrumbsService } from '../breadcrumbs/dso-breadcrumbs.service'; |
| 15 | +import { DSONameService } from '../breadcrumbs/dso-name.service'; |
| 16 | +import { CollectionDataService } from '../data/collection-data.service'; |
| 17 | +import { RemoteData } from '../data/remote-data'; |
| 18 | +import { Collection } from '../shared/collection.model'; |
| 19 | +import { |
| 20 | + getFirstCompletedRemoteData, |
| 21 | + getRemoteDataPayload, |
| 22 | +} from '../shared/operators'; |
| 23 | +import { SubmissionObject } from './models/submission-object.model'; |
| 24 | + |
| 25 | +/** |
| 26 | + * Service to calculate the parent {@link DSpaceObject} breadcrumbs for a {@link SubmissionObject} |
| 27 | + */ |
| 28 | +@Injectable({ |
| 29 | + providedIn: 'root', |
| 30 | +}) |
| 31 | +export class SubmissionParentBreadcrumbsService implements BreadcrumbsProviderService<SubmissionObject> { |
| 32 | + |
| 33 | + constructor( |
| 34 | + protected dsoNameService: DSONameService, |
| 35 | + protected dsoBreadcrumbsService: DSOBreadcrumbsService, |
| 36 | + protected submissionService: SubmissionService, |
| 37 | + protected collectionService: CollectionDataService, |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates the parent breadcrumb structure for {@link SubmissionObject}s. It also automatically recreates the |
| 43 | + * parent breadcrumb structure when you change a {@link SubmissionObject}'s by dispatching a |
| 44 | + * {@link ChangeSubmissionCollectionAction}. |
| 45 | + * |
| 46 | + * @param submissionObject The {@link SubmissionObject} for which the parent breadcrumb structure needs to be created |
| 47 | + */ |
| 48 | + getBreadcrumbs(submissionObject: SubmissionObject): Observable<Breadcrumb[]> { |
| 49 | + return combineLatest([ |
| 50 | + (submissionObject.collection as Observable<RemoteData<Collection>>).pipe( |
| 51 | + getFirstCompletedRemoteData(), |
| 52 | + getRemoteDataPayload(), |
| 53 | + ), |
| 54 | + this.submissionService.getSubmissionCollectionId(submissionObject.id), |
| 55 | + ]).pipe( |
| 56 | + switchMap(([collection, latestCollectionId]: [Collection, string]) => { |
| 57 | + if (hasValue(latestCollectionId)) { |
| 58 | + return this.collectionService.findById(latestCollectionId).pipe( |
| 59 | + getFirstCompletedRemoteData(), |
| 60 | + getRemoteDataPayload(), |
| 61 | + ); |
| 62 | + } else { |
| 63 | + return observableOf(collection); |
| 64 | + } |
| 65 | + }), |
| 66 | + switchMap((collection: Collection) => this.dsoBreadcrumbsService.getBreadcrumbs(collection, getDSORoute(collection))), |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments