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

[#43695] refactored resource service

Eric Schubert3d9545af9b34
diff --git a/frontend/src/app/core/state/storage-files/storage-files.service.ts b/frontend/src/app/core/state/storage-files/storage-files.service.ts
index 937f24e6c6c..a1e3f9e33ca 100644
--- a/frontend/src/app/core/state/storage-files/storage-files.service.ts
+++ b/frontend/src/app/core/state/storage-files/storage-files.service.ts
@@ -28,17 +28,18 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
+import { Observable } from 'rxjs';
+import { map, tap } from 'rxjs/operators';
+
import {
CollectionStore,
ResourceCollectionService,
} from 'core-app/core/state/resource-collection.service';
-import { StorageFilesStore } from 'core-app/core/state/storage-files/storage-files.store';
-import { Observable } from 'rxjs';
import { IStorageFile } from 'core-app/core/state/storage-files/storage-file.model';
+import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
import { IHalResourceLink } from 'core-app/core/state/hal-resource';
+import { StorageFilesStore } from 'core-app/core/state/storage-files/storage-files.store';
import { insertCollectionIntoState } from 'core-app/core/state/collection-store';
-import { map } from 'rxjs/operators';
-import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
@Injectable()
export class StorageFilesResourceService extends ResourceCollectionService<IStorageFile> {
@@ -50,18 +51,19 @@ export class StorageFilesResourceService extends ResourceCollectionService<IStor
return new StorageFilesStore();
}
- fetch(link:IHalResourceLink):void {
- this.http
- .get<[IStorageFile]>(link.href)
- .pipe(
- map((fileList) => ({ _embedded: { elements: fileList } } as unknown as IHALCollection<IStorageFile>)),
- // delay(5000),
- )
- .subscribe((fileList) => insertCollectionIntoState(this.store, fileList, 'root'));
- }
+ files(link:IHalResourceLink):Observable<IStorageFile[]> {
+ if (this.collectionExists(link.href)) {
+ return this.collection(link.href);
+ }
- files():Observable<IStorageFile[]> {
- return this.collection('root');
+ return this.http
+ .get<IHALCollection<IStorageFile>>(link.href)
+ .pipe(
+ tap((collection) => {
+ insertCollectionIntoState(this.store, collection, link.href);
+ }),
+ map((collection) => collection._embedded.elements),
+ );
}
reset():void {
diff --git a/frontend/src/app/shared/components/file-links/file-picker-modal/file-picker-modal.component.ts b/frontend/src/app/shared/components/file-links/file-picker-modal/file-picker-modal.component.ts
index f42028c8fe9..3feedbf0aec 100644
--- a/frontend/src/app/shared/components/file-links/file-picker-modal/file-picker-modal.component.ts
+++ b/frontend/src/app/shared/components/file-links/file-picker-modal/file-picker-modal.component.ts
@@ -35,7 +35,7 @@ import {
OnDestroy,
OnInit,
} from '@angular/core';
-import { BehaviorSubject, Observable } from 'rxjs';
+import { BehaviorSubject } from 'rxjs';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { OpModalLocalsMap } from 'core-app/shared/components/modal/modal.types';
@@ -52,7 +52,7 @@ import { IHalResourceLink } from 'core-app/core/state/hal-resource';
export class FilePickerModalComponent extends OpModalComponent implements OnInit, OnDestroy {
public loading$ = new BehaviorSubject<boolean>(true);
- public storageFiles$:Observable<IStorageFile[]>;
+ public storageFiles$ = new BehaviorSubject<IStorageFile[]>([]);
public text = {
header: this.i18n.t('js.storages.file_links.select'),
@@ -84,17 +84,15 @@ export class FilePickerModalComponent extends OpModalComponent implements OnInit
super.ngOnInit();
const filesLink:IHalResourceLink = {
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/restrict-template-expressions
- href: `${this.locals.storageLink.href}/files`,
+ href: `${(this.locals.storageLink as IHalResourceLink).href}/files`,
title: 'Storage files',
};
- this.storageFilesResourceService.fetch(filesLink);
- this.storageFiles$ = this.storageFilesResourceService.files();
-
- this.storageFiles$.subscribe(() => {
- this.loading$.next(false);
- });
+ this.storageFilesResourceService.files(filesLink)
+ .subscribe((files) => {
+ this.storageFiles$.next(files);
+ this.loading$.next(false);
+ });
}
ngOnDestroy():void {
diff --git a/modules/storages/lib/api/v3/storages/storage_files_api.rb b/modules/storages/lib/api/v3/storages/storage_files_api.rb
index 8bfbf87d859..eeb3631cd65 100644
--- a/modules/storages/lib/api/v3/storages/storage_files_api.rb
+++ b/modules/storages/lib/api/v3/storages/storage_files_api.rb
@@ -32,36 +32,43 @@ module API
class StorageFilesAPI < ::API::OpenProjectAPI
resources :files do
get do
- [
- {
- id: 1,
- name: 'image.png',
- mimeType: 'image/png',
- lastModifiedAt: '2022-09-16T12:00Z',
- lastModifiedByName: 'Leia Organa',
- location: '/data',
- },
- {
- id: 2,
- name: 'Readme.md',
- mimeType: 'text/markdown',
- lastModifiedAt: '2022-09-16T13:00Z',
- lastModifiedByName: 'Anakin Skywalker',
- location: '/data',
- },
- {
- id: 3,
- name: 'folder',
- mimeType: 'application/x-op-directory',
- location: '/data',
- },
- {
- id: 4,
- name: 'directory',
- mimeType: 'application/x-op-directory',
- location: '/data',
- },
- ]
+ {
+ count: 4,
+ total: 4,
+ _embedded: {
+ elements:
+ [
+ {
+ id: 1,
+ name: 'image.png',
+ mimeType: 'image/png',
+ lastModifiedAt: '2022-09-16T12:00Z',
+ lastModifiedByName: 'Leia Organa',
+ location: '/data'
+ },
+ {
+ id: 2,
+ name: 'Readme.md',
+ mimeType: 'text/markdown',
+ lastModifiedAt: '2022-09-16T13:00Z',
+ lastModifiedByName: 'Anakin Skywalker',
+ location: '/data'
+ },
+ {
+ id: 3,
+ name: 'folder',
+ mimeType: 'application/x-op-directory',
+ location: '/data'
+ },
+ {
+ id: 4,
+ name: 'directory',
+ mimeType: 'application/x-op-directory',
+ location: '/data'
+ }
+ ]
+ }
+ }
end
end
end