Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Reworked StorageResourceService to use collection store pattern

Andreas Pfohl71e56351deee
diff --git a/frontend/src/app/core/state/storages/storage.model.ts b/frontend/src/app/core/state/storages/storage.model.ts
index e96e5aff8e1..2df92f5622d 100644
--- a/frontend/src/app/core/state/storages/storage.model.ts
+++ b/frontend/src/app/core/state/storages/storage.model.ts
@@ -28,6 +28,29 @@
import { ID } from '@datorama/akita';
import { IHalResourceLink, IHalResourceLinks } from 'core-app/core/state/hal-resource';
+import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
+
+export class StorageCollection implements IHALCollection<IStorage> {
+ readonly _type:'Collection';
+
+ readonly _embedded:{ elements:IStorage[] } = { elements: [] };
+
+ readonly count:number;
+
+ readonly offset:number;
+
+ readonly pageSize:number;
+
+ readonly total:number;
+
+ constructor(storages:IStorage[]) {
+ this._embedded.elements = storages;
+ this.count = storages.length;
+ this.offset = 0;
+ this.pageSize = 1;
+ this.total = storages.length;
+ }
+}
export interface IStorageHalResourceLinks extends IHalResourceLinks {
self:IHalResourceLink;
diff --git a/frontend/src/app/core/state/storages/storages.service.ts b/frontend/src/app/core/state/storages/storages.service.ts
index 7d3e9a2523b..b161b29e517 100644
--- a/frontend/src/app/core/state/storages/storages.service.ts
+++ b/frontend/src/app/core/state/storages/storages.service.ts
@@ -28,59 +28,44 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
-import { Observable } from 'rxjs';
-import { catchError, map, tap } from 'rxjs/operators';
-import { IStorage } from 'core-app/core/state/storages/storage.model';
+import { forkJoin, Observable } from 'rxjs';
+import { map, switchMap } from 'rxjs/operators';
+import { IStorage, StorageCollection } from 'core-app/core/state/storages/storage.model';
import { StoragesStore } from 'core-app/core/state/storages/storages.store';
import { StoragesQuery } from 'core-app/core/state/storages/storages.query';
-import { IHalResourceLink } from 'core-app/core/state/hal-resource';
-import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
-import { ToastService } from 'core-app/shared/components/toaster/toast.service';
-import idFromLink from 'core-app/features/hal/helpers/id-from-link';
+import { insertCollectionIntoState } from 'core-app/core/state/collection-store';
+import { HalResource } from 'core-app/features/hal/resources/hal-resource';
@Injectable()
export class StoragesResourceService {
- protected store = new StoragesStore();
+ private readonly store = new StoragesStore();
- protected readonly query = new StoragesQuery(this.store);
+ private readonly query = new StoragesQuery(this.store);
- private get storagesPath():string {
- return `${this.apiV3Service.root.path}/storages`;
- }
+ constructor(private readonly http:HttpClient) {}
- constructor(
- private readonly http:HttpClient,
- private readonly toastService:ToastService,
- private readonly apiV3Service:ApiV3Service,
- ) {}
+ collection(project:HalResource):Observable<IStorage[]> {
+ return this
+ .query
+ .select()
+ .pipe(
+ map((state) => state.collections[project.id as string]?.ids),
+ switchMap((ids) => this.query.selectMany(ids)),
+ );
+ }
- lookup(link:IHalResourceLink, require = false):Observable<IStorage> {
- const id = idFromLink(link.href);
+ lookup(id:string):Observable<IStorage|undefined> {
+ return this.query.selectEntity(id);
+ }
- if (require && !this.query.hasEntity(id)) {
- return this.http
- .get<IStorage>(`${this.storagesPath}/${id}`)
- .pipe(
- tap((storage) => {
- if (storage) {
- this.store.add(storage);
- }
- }),
- catchError((error) => {
- this.toastService.addError(error);
- throw error;
- }),
- );
- }
+ async updateCollection(project:HalResource):Promise<void> {
+ const storages = await forkJoin(
+ (project.$links as { storages:{ href:string }[] })
+ .storages
+ .map((link) => this.http.get<IStorage>(link.href)),
+ ).toPromise();
+ const storageCollection = new StorageCollection(storages);
- return this.query.selectEntity(id)
- .pipe(
- map((storage) => {
- if (!storage) {
- throw new Error('not found');
- }
- return storage;
- }),
- );
+ insertCollectionIntoState(this.store, storageCollection, project.id as string);
}
}
diff --git a/frontend/src/app/features/work-packages/components/wp-single-view-tabs/files-tab/op-files-tab.component.ts b/frontend/src/app/features/work-packages/components/wp-single-view-tabs/files-tab/op-files-tab.component.ts
index c8e719ca282..b596853c4bd 100644
--- a/frontend/src/app/features/work-packages/components/wp-single-view-tabs/files-tab/op-files-tab.component.ts
+++ b/frontend/src/app/features/work-packages/components/wp-single-view-tabs/files-tab/op-files-tab.component.ts
@@ -26,20 +26,18 @@
// See COPYRIGHT and LICENSE files for more details.
//++
-import {
- ChangeDetectionStrategy,
- Component,
- OnInit,
-} from '@angular/core';
+import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { HookService } from 'core-app/features/plugins/hook-service';
-import { forkJoin, Observable } from 'rxjs';
+import { Observable } from 'rxjs';
import { CurrentUserService } from 'core-app/core/current-user/current-user.service';
import { StoragesResourceService } from 'core-app/core/state/storages/storages.service';
-import { switchMap } from 'rxjs/operators';
+import { catchError } from 'rxjs/operators';
import { IStorage } from 'core-app/core/state/storages/storage.model';
import { ProjectsResourceService } from 'core-app/core/state/projects/projects.service';
+import { ToastService } from 'core-app/shared/components/toaster/toast.service';
+import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
import { HalResource } from 'core-app/features/hal/resources/hal-resource';
@Component({
@@ -52,10 +50,10 @@ export class WorkPackageFilesTabComponent implements OnInit {
text = {
attachments: {
- label: this.I18n.t('js.label_attachments'),
+ label: this.i18n.t('js.label_attachments'),
},
file_links: {
- label: this.I18n.t('js.label_nextcloud'),
+ label: this.i18n.t('js.label_nextcloud'),
},
};
@@ -64,34 +62,38 @@ export class WorkPackageFilesTabComponent implements OnInit {
storages$:Observable<IStorage[]>;
constructor(
- readonly I18n:I18nService,
- protected hook:HookService,
- private currentUserService:CurrentUserService,
+ private readonly i18n:I18nService,
+ private readonly hook:HookService,
+ private readonly currentUserService:CurrentUserService,
private readonly projectsResourceService:ProjectsResourceService,
private readonly storagesResourceService:StoragesResourceService,
+ private readonly apiV3:ApiV3Service,
+ private readonly toast:ToastService,
) { }
- ngOnInit():void {
+ async ngOnInit():Promise<void> {
const project = this.workPackage.$embedded.project as HalResource;
if (project.id === null) {
return;
}
+ try {
+ await this.storagesResourceService.updateCollection(project);
+ } catch (error) {
+ this.toast.addError(error);
+ return;
+ }
+
this.canViewFileLinks$ = this
.currentUserService
- .hasCapabilities$('file_links/view', project.id);
-
- this.storages$ = this.projectsResourceService.lookup(project.id, true)
- .pipe(
- switchMap((p) => {
- const storageLinks = p._links.storages;
+ .hasCapabilities$('file_links/view', project.id.toString());
- return forkJoin(
- storageLinks.map((link) => this
- .storagesResourceService
- .lookup(link, true)),
- );
- }),
- );
+ this.storages$ = this
+ .storagesResourceService
+ .collection(project)
+ .pipe(catchError((error) => {
+ this.toast.addError(error);
+ throw error;
+ }));
}
}
diff --git a/frontend/src/app/features/work-packages/components/wp-single-view-tabs/files-tab/op-files-tab.html b/frontend/src/app/features/work-packages/components/wp-single-view-tabs/files-tab/op-files-tab.html
index af1d1b7cd53..ed4d4c678ee 100644
--- a/frontend/src/app/features/work-packages/components/wp-single-view-tabs/files-tab/op-files-tab.html
+++ b/frontend/src/app/features/work-packages/components/wp-single-view-tabs/files-tab/op-files-tab.html
@@ -1,6 +1,6 @@
<div class="op-tab-content--container">
<section data-qa-selector="op-tab-content--tab-section">
- <div *ngIf="canViewFileLinks$ | async" class="op-tab-content--header">
+ <div *ngIf="(canViewFileLinks$ | async) === true" class="op-tab-content--header">
<h3 class="op-tab-content--header-text" [textContent]="text.attachments.label"></h3>
</div>