Restructure the frontend code to be more readable.

- Respond with a list of FileLinks instead of void in FileLinksResourceService#updateCollectionsForWorkPackage
- Move error handling up to the caller of FileLinksResourceService#updateCollectionsForWorkPackage
- Avoid nested subscribe calls
pull/11919/head
Pavel Balashov 2 years ago
parent 54b22c3d19
commit ccd597ab9f
  1. 10
      frontend/src/app/core/state/file-links/file-links.service.ts
  2. 24
      frontend/src/app/features/work-packages/routing/wp-view-base/work-package-single-view.base.ts

@ -31,12 +31,10 @@ import { Injectable } from '@angular/core';
import { HttpHeaders } from '@angular/common/http';
import { from, Observable } from 'rxjs';
import {
catchError,
groupBy,
mergeMap,
reduce,
switchMap,
map,
tap,
} from 'rxjs/operators';
@ -55,7 +53,7 @@ import idFromLink from 'core-app/features/hal/helpers/id-from-link';
export class FileLinksResourceService extends ResourceCollectionService<IFileLink> {
@InjectField() toastService:ToastService;
updateCollectionsForWorkPackage(fileLinksSelfLink:string):Observable<void> {
updateCollectionsForWorkPackage(fileLinksSelfLink:string):Observable<IFileLink[]> {
return this.http
.get<IHALCollection<IFileLink>>(fileLinksSelfLink)
.pipe(
@ -72,14 +70,14 @@ export class FileLinksResourceService extends ResourceCollectionService<IFileLin
return acc;
}, seed));
}),
map((fileLinkCollections) => {
tap((fileLinkCollections) => {
const storageId = idFromLink(fileLinkCollections.storage);
const collectionKey = `${fileLinksSelfLink}?filters=[{"storage":{"operator":"=","values":["${storageId}"]}}]`;
const collection = { _embedded: { elements: fileLinkCollections.fileLinks } } as IHALCollection<IFileLink>;
insertCollectionIntoState(this.store, collection, collectionKey);
}),
catchError(this.toastAndThrow.bind(this)),
) as Observable<void>;
reduce((acc, group) => acc.concat(group.fileLinks), [] as IFileLink[]),
);
}
protected createStore():CollectionStore<IFileLink> {

@ -45,6 +45,9 @@ import {
import {
WorkPackageNotificationService,
} from 'core-app/features/work-packages/services/notifications/work-package-notification.service';
import {
switchMap,
} from 'rxjs/operators';
import { InjectField } from 'core-app/shared/helpers/angular/inject-field.decorator';
import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
@ -194,16 +197,19 @@ export class WorkPackageSingleViewBase extends UntilDestroyedMixin {
if (this.workPackage.$links.fileLinks) {
this.fileLinkResourceService
.updateCollectionsForWorkPackage(this.workPackage.$links.fileLinks.href as string)
.pipe(
this.untilDestroyed(),
switchMap(() => this.projectsResourceService.lookup((this.workPackage.project as unknown&{ id:string }).id)),
)
.subscribe(
() => {
this.projectsResourceService
.lookup((this.workPackage.project as unknown&{ id:string }).id)
.pipe(this.untilDestroyed())
.subscribe((project) => {
if (project._links.storages) {
this.storages.updateCollection(project._links.self.href, project._links.storages);
}
});
(project) => {
if (project._links.storages) {
this.storages.updateCollection(project._links.self.href, project._links.storages);
}
},
(error) => {
this.toastService.addError(error);
throw error;
},
);
}

Loading…
Cancel
Save