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

Implemented actual fetching of file links

Andreas Pfohl497cb8d3b038
diff --git a/frontend/src/app/core/state/attachments/attachments.service.ts b/frontend/src/app/core/state/attachments/attachments.service.ts
index 4db449ecb6e..644f7a8ab2f 100644
--- a/frontend/src/app/core/state/attachments/attachments.service.ts
+++ b/frontend/src/app/core/state/attachments/attachments.service.ts
@@ -44,7 +44,7 @@ import {
map,
tap,
} from 'rxjs/operators';
-import { AttachmentsStore } from 'core-app/core/state/attachments/attacments.store';
+import { AttachmentsStore } from 'core-app/core/state/attachments/attachments.store';
import { IAttachment } from 'core-app/core/state/attachments/attachment.model';
import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
import { ToastService } from 'core-app/shared/components/toaster/toast.service';
@@ -67,13 +67,15 @@ export class AttachmentsResourceService {
public query = new QueryEntity(this.store);
- constructor(private readonly I18n:I18nService,
+ constructor(
+ private readonly I18n:I18nService,
private readonly http:HttpClient,
private readonly apiV3Service:ApiV3Service,
private readonly fileUploadService:OpenProjectFileUploadService,
private readonly directFileUploadService:OpenProjectDirectFileUploadService,
private readonly configurationService:ConfigurationService,
- private readonly toastService:ToastService) { }
+ private readonly toastService:ToastService,
+ ) { }
/**
* This method ensures that a specific collection is fetched, if not available.
@@ -148,7 +150,7 @@ export class AttachmentsResourceService {
* @param files The upload files to be attached.
*/
attachFiles(resource:HalResource, files:UploadFile[]):Observable<IAttachment[]> {
- const identifier = this.getAttachmentsSelfLink(resource) || HAL_NEW_RESOURCE_ID;
+ const identifier = AttachmentsResourceService.getAttachmentsSelfLink(resource) || HAL_NEW_RESOURCE_ID;
const href = this.getUploadTarget(resource);
const isDirectUpload = !!this.getDirectUploadLink(resource);
@@ -234,7 +236,7 @@ export class AttachmentsResourceService {
*/
private getUploadTarget(resource:HalResource):string {
return this.getDirectUploadLink(resource)
- || this.getAttachmentsSelfLink(resource)
+ || AttachmentsResourceService.getAttachmentsSelfLink(resource)
|| this.apiV3Service.attachments.path;
}
@@ -252,7 +254,7 @@ export class AttachmentsResourceService {
return null;
}
- private getAttachmentsSelfLink(resource:HalResource):string|null {
+ private static getAttachmentsSelfLink(resource:HalResource):string|null {
const attachments = resource.attachments as unknown&{ href?:string };
return attachments?.href || null;
}
diff --git a/frontend/src/app/core/state/attachments/attacments.store.ts b/frontend/src/app/core/state/attachments/attachments.store.ts
similarity index 100%
rename from frontend/src/app/core/state/attachments/attacments.store.ts
rename to frontend/src/app/core/state/attachments/attachments.store.ts
diff --git a/frontend/src/app/core/state/file-links/file-links.service.ts b/frontend/src/app/core/state/file-links/file-links.service.ts
new file mode 100644
index 00000000000..592a6c1c7fb
--- /dev/null
+++ b/frontend/src/app/core/state/file-links/file-links.service.ts
@@ -0,0 +1,72 @@
+// -- copyright
+// OpenProject is an open source project management software.
+// Copyright (C) 2012-2022 the OpenProject GmbH
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License version 3.
+//
+// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+// Copyright (C) 2006-2013 Jean-Philippe Lang
+// Copyright (C) 2010-2013 the ChiliProject Team
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// See COPYRIGHT and LICENSE files for more details.
+//++
+
+import { Injectable } from '@angular/core';
+import { FileLinksStore } from 'core-app/core/state/file-links/file-links.store';
+import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
+import { IFileLink } from 'core-app/core/state/file-links/file-link.model';
+import { HttpClient } from '@angular/common/http';
+import { ToastService } from 'core-app/shared/components/toaster/toast.service';
+import {
+ catchError, map, switchMap, tap,
+} from 'rxjs/operators';
+import { insertCollectionIntoState } from 'core-app/core/state/collection-store';
+import { Observable } from 'rxjs';
+import { QueryEntity } from '@datorama/akita';
+
+@Injectable()
+export class FileLinkResourceService {
+ protected store = new FileLinksStore();
+
+ constructor(
+ private readonly http:HttpClient,
+ private readonly toastService:ToastService,
+ ) {}
+
+ fetchCurrent(fileLinksSelfLink:string):void {
+ this.http
+ .get<IHALCollection<IFileLink>>(fileLinksSelfLink)
+ .pipe(
+ tap((collection) => insertCollectionIntoState(this.store, collection, fileLinksSelfLink)),
+ catchError((error) => {
+ this.toastService.addError(error);
+ throw error;
+ }),
+ ).subscribe();
+ }
+
+ all(key:string):Observable<IFileLink[]> {
+ const query = new QueryEntity(this.store);
+ return query
+ .select()
+ .pipe(
+ map((state) => state.collections[key]?.ids),
+ switchMap((fileLinkIds) => query.selectMany(fileLinkIds)),
+ );
+ }
+}
diff --git a/frontend/src/app/core/state/file-links/file-links.store.ts b/frontend/src/app/core/state/file-links/file-links.store.ts
new file mode 100644
index 00000000000..38fb1eb8b6a
--- /dev/null
+++ b/frontend/src/app/core/state/file-links/file-links.store.ts
@@ -0,0 +1,40 @@
+// -- copyright
+// OpenProject is an open source project management software.
+// Copyright (C) 2012-2022 the OpenProject GmbH
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License version 3.
+//
+// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+// Copyright (C) 2006-2013 Jean-Philippe Lang
+// Copyright (C) 2010-2013 the ChiliProject Team
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// See COPYRIGHT and LICENSE files for more details.
+//++
+
+import { CollectionState, createInitialCollectionState } from 'core-app/core/state/collection-store';
+import { EntityStore, StoreConfig } from '@datorama/akita';
+import { IFileLink } from 'core-app/core/state/file-links/file-link.model';
+
+export interface FileLinksState extends CollectionState<IFileLink> {}
+
+@StoreConfig({ name: 'attachments' })
+export class FileLinksStore extends EntityStore<FileLinksState> {
+ constructor() {
+ super(createInitialCollectionState());
+ }
+}
diff --git a/frontend/src/app/features/work-packages/openproject-work-packages.module.ts b/frontend/src/app/features/work-packages/openproject-work-packages.module.ts
index e08c9ba5ff7..6d980cd1ee3 100644
--- a/frontend/src/app/features/work-packages/openproject-work-packages.module.ts
+++ b/frontend/src/app/features/work-packages/openproject-work-packages.module.ts
@@ -394,6 +394,7 @@ import {
import { WorkPackagesQueryViewService } from 'core-app/features/work-packages/components/wp-list/wp-query-view.service';
import isNewResource from 'core-app/features/hal/helpers/is-new-resource';
import { OpenprojectFileLinksModule } from 'core-app/shared/components/file-links/openproject-file-links.module';
+import { FileLinkResourceService } from 'core-app/core/state/file-links/file-links.service';
@NgModule({
imports: [
@@ -448,6 +449,7 @@ import { OpenprojectFileLinksModule } from 'core-app/shared/components/file-link
WorkPackagesQueryViewService,
HalEventsService,
+ FileLinkResourceService,
],
declarations: [
// Routing
diff --git a/frontend/src/app/shared/components/file-links/file-link-list/file-link-list.component.ts b/frontend/src/app/shared/components/file-links/file-link-list/file-link-list.component.ts
index 8ff7cdd8616..2447d11ef2f 100644
--- a/frontend/src/app/shared/components/file-links/file-link-list/file-link-list.component.ts
+++ b/frontend/src/app/shared/components/file-links/file-link-list/file-link-list.component.ts
@@ -29,10 +29,12 @@
import {
ChangeDetectionStrategy, Component, Input, OnInit,
} from '@angular/core';
-import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin';
import { HalResource } from 'core-app/features/hal/resources/hal-resource';
-import { Observable, of } from 'rxjs';
+import { Observable } from 'rxjs';
import { IFileLink } from 'core-app/core/state/file-links/file-link.model';
+import { FileLinkResourceService } from 'core-app/core/state/file-links/file-links.service';
+import isNewResource from 'core-app/features/hal/helpers/is-new-resource';
+import { tap } from 'rxjs/operators';
@Component({
selector: 'op-file-link-list',
@@ -40,29 +42,32 @@ import { IFileLink } from 'core-app/core/state/file-links/file-link.model';
styleUrls: ['./file-link-list.sass'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
-export class FileLinkListComponent extends UntilDestroyedMixin implements OnInit {
+export class FileLinkListComponent implements OnInit {
@Input() public resource:HalResource;
$fileLinks:Observable<IFileLink[]>;
+ constructor(private readonly fileLinkResourceService:FileLinkResourceService) {}
+
ngOnInit():void {
- this.$fileLinks = of([
- {
- id: 1,
- originData: {
- name: 'awesome.png',
- lastModifiedAt: '2022-04-01T12:00Z',
- lastModifiedByName: 'Anakin Skywalker',
- },
- },
- {
- id: 2,
- originData: {
- name: 'logo.png',
- lastModifiedAt: '2022-04-01T12:00Z',
- lastModifiedByName: 'Leia Organa',
- },
- },
- ]);
+ this.fileLinkResourceService.fetchCurrent(this.fileLinkSelfLink);
+
+ this.$fileLinks = this.fileLinkResourceService.all(this.collectionKey)
+ .pipe(
+ tap((fileLinks) => {
+ if (isNewResource(this.resource)) {
+ this.resource.fileLinks = { elements: fileLinks.map((a) => a._links?.self) };
+ }
+ }),
+ );
+ }
+
+ private get fileLinkSelfLink():string {
+ const fileLinks = this.resource.fileLinks as unknown&{ href:string };
+ return fileLinks.href;
+ }
+
+ private get collectionKey():string {
+ return isNewResource(this.resource) ? 'new' : this.fileLinkSelfLink;
}
}