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

Introduce view store and replace viewHalResource with plain object

Henriette Dargec6e535036682
diff --git a/frontend/src/app/core/apiv3/endpoints/views/apiv3-views-paths.ts b/frontend/src/app/core/apiv3/endpoints/views/apiv3-views-paths.ts
index d1a31082b64..627049deb05 100644
--- a/frontend/src/app/core/apiv3/endpoints/views/apiv3-views-paths.ts
+++ b/frontend/src/app/core/apiv3/endpoints/views/apiv3-views-paths.ts
@@ -29,22 +29,40 @@
import { APIv3GettableResource, APIv3ResourceCollection } from 'core-app/core/apiv3/paths/apiv3-resource';
import { APIV3Service } from 'core-app/core/apiv3/api-v3.service';
import { Observable } from 'rxjs';
-import { ViewResource } from 'core-app/features/hal/resources/views-resource';
+import { View } from 'core-app/core/state/views/view.model';
+import { InjectField } from 'core-app/shared/helpers/angular/inject-field.decorator';
+import { HttpClient } from '@angular/common/http';
+import { map } from 'rxjs/operators';
-export class APIv3ViewsPaths extends APIv3ResourceCollection<ViewResource, APIv3GettableResource<ViewResource>> {
- constructor(protected apiRoot:APIV3Service,
- protected basePath:string) {
+export class APIv3ViewsPaths extends APIv3ResourceCollection<View, APIv3GettableResource<View>> {
+ @InjectField() http:HttpClient;
+
+ constructor(
+ protected apiRoot:APIV3Service,
+ protected basePath:string,
+ ) {
super(apiRoot, basePath, 'views');
}
/**
* Create a new view resource
*
- * @param payload Payload object or view HAL resource
+ * @param TODO
*/
- public post(resource:ViewResource|unknown, type:string):Observable<ViewResource> {
+ post(resource:View|unknown, type:string):Observable<View> {
return this
- .halResourceService
- .post<ViewResource>(`${this.path}/${type}`, resource);
+ .http
+ .post(
+ `${this.path}/${type})}`,
+ { resource },
+ {
+ withCredentials: true,
+ responseType: 'json',
+ },
+ ).pipe(
+ map((view:unknown) => {
+ return view as View;
+ }),
+ );
}
}
diff --git a/frontend/src/app/core/state/views/view.model.ts b/frontend/src/app/core/state/views/view.model.ts
new file mode 100644
index 00000000000..1eb575b7b41
--- /dev/null
+++ b/frontend/src/app/core/state/views/view.model.ts
@@ -0,0 +1,14 @@
+import { ID } from '@datorama/akita';
+import {
+ HalResourceLink,
+ HalResourceLinks,
+} from 'core-app/core/state/hal-resource';
+
+export interface ViewLinks extends HalResourceLinks {
+ query:HalResourceLink
+}
+
+export interface View {
+ id:ID;
+ _links:ViewLinks;
+}
diff --git a/frontend/src/app/core/state/views/views.query.ts b/frontend/src/app/core/state/views/views.query.ts
new file mode 100644
index 00000000000..7912e955a90
--- /dev/null
+++ b/frontend/src/app/core/state/views/views.query.ts
@@ -0,0 +1,5 @@
+import { QueryEntity } from '@datorama/akita';
+import { ViewsState } from 'core-app/core/state/views/views.store';
+
+export class ViewsQuery extends QueryEntity<ViewsState> {
+}
diff --git a/frontend/src/app/core/state/views/views.service.ts b/frontend/src/app/core/state/views/views.service.ts
new file mode 100644
index 00000000000..f0e31c0c29f
--- /dev/null
+++ b/frontend/src/app/core/state/views/views.service.ts
@@ -0,0 +1,110 @@
+import { Injectable } from '@angular/core';
+import {
+ catchError,
+ tap,
+} from 'rxjs/operators';
+import { Observable } from 'rxjs';
+import {
+ applyTransaction,
+ ID,
+} from '@datorama/akita';
+import { APIV3Service } from 'core-app/core/apiv3/api-v3.service';
+import { ToastService } from 'core-app/shared/components/toaster/toast.service';
+import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
+import { HttpClient } from '@angular/common/http';
+import { Apiv3ListParameters } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
+import { collectionKey } from 'core-app/core/state/collection-store';
+import {
+ EffectHandler,
+} from 'core-app/core/state/effects/effect-handler.decorator';
+import { ActionsService } from 'core-app/core/state/actions/actions.service';
+import { ViewsStore } from 'core-app/core/state/views/views.store';
+import { ViewsQuery } from 'core-app/core/state/views/views.query';
+import { View } from 'core-app/core/state/views/view.model';
+
+@EffectHandler
+@Injectable()
+export class ViewsResourceService {
+ protected store = new ViewsStore();
+
+ readonly query = new ViewsQuery(this.store);
+
+ private get viewsPath():string {
+ return this
+ .apiV3Service
+ .views
+ .path;
+ }
+
+ constructor(
+ readonly actions$:ActionsService,
+ private http:HttpClient,
+ private apiV3Service:APIV3Service,
+ private toastService:ToastService,
+ ) {
+ }
+
+ fetchViews(params:Apiv3ListParameters):Observable<IHALCollection<View>> {
+ const collectionURL = collectionKey(params);
+
+ return this
+ .http
+ .get<IHALCollection<View>>(this.viewsPath + collectionURL)
+ .pipe(
+ tap((events) => {
+ applyTransaction(() => {
+ this.store.add(events._embedded.elements);
+ this.store.update(({ collections }) => (
+ {
+ collections: {
+ ...collections,
+ [collectionURL]: {
+ ...collections[collectionURL],
+ ids: events._embedded.elements.map((el) => el.id),
+ },
+ },
+ }
+ ));
+ });
+ }),
+ catchError((error) => {
+ this.toastService.addError(error);
+ throw error;
+ }),
+ );
+ }
+
+ update(id:ID, view:Partial<View>):void {
+ this.store.update(id, view);
+ }
+
+ modifyCollection(params:Apiv3ListParameters, callback:(collection:ID[]) => ID[]):void {
+ const key = collectionKey(params);
+ this.store.update(({ collections }) => (
+ {
+ collections: {
+ ...collections,
+ [key]: {
+ ...collections[key],
+ ids: [...callback(collections[key]?.ids || [])],
+ },
+ },
+ }
+ ));
+ }
+
+ removeFromCollection(params:Apiv3ListParameters, ids:ID[]):void {
+ const key = collectionKey(params);
+ this.store.update(({ collections }) => (
+ {
+ collections: {
+ ...collections,
+ [key]: {
+ ...collections[key],
+ ids: (collections[key]?.ids || []).filter((id) => !ids.includes(id)),
+ },
+ },
+ }
+ ));
+ }
+}
diff --git a/frontend/src/app/core/state/views/views.store.ts b/frontend/src/app/core/state/views/views.store.ts
new file mode 100644
index 00000000000..d8fcbbd34f4
--- /dev/null
+++ b/frontend/src/app/core/state/views/views.store.ts
@@ -0,0 +1,13 @@
+import { EntityStore, StoreConfig } from '@datorama/akita';
+import { CollectionState, createInitialCollectionState } from 'core-app/core/state/collection-store';
+import { View } from './view.model';
+
+export interface ViewsState extends CollectionState<View> {
+}
+
+@StoreConfig({ name: 'views' })
+export class ViewsStore extends EntityStore<ViewsState> {
+ constructor() {
+ super(createInitialCollectionState());
+ }
+}
diff --git a/frontend/src/app/features/hal/resources/views-resource.ts b/frontend/src/app/features/hal/resources/views-resource.ts
deleted file mode 100644
index 04200cc3d68..00000000000
--- a/frontend/src/app/features/hal/resources/views-resource.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-// -- copyright
-// OpenProject is an open source project management software.
-// Copyright (C) 2012-2021 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 { HalResource } from 'core-app/features/hal/resources/hal-resource';
-
-export interface ViewResourceLinks {
-}
-
-export interface ViewResourceEmbedded {
- query:HalResource;
-}
-
-export class ViewResource extends HalResource {
-}
-
-export interface ViewResource extends ViewResourceLinks, ViewResourceEmbedded {}
diff --git a/frontend/src/app/features/work-packages/components/wp-list/wp-query-view.service.ts b/frontend/src/app/features/work-packages/components/wp-list/wp-query-view.service.ts
index e7f85416893..a84bd61bb06 100644
--- a/frontend/src/app/features/work-packages/components/wp-list/wp-query-view.service.ts
+++ b/frontend/src/app/features/work-packages/components/wp-list/wp-query-view.service.ts
@@ -2,8 +2,8 @@ import { Injectable } from '@angular/core';
import { StateService } from '@uirouter/core';
import { QueryResource } from 'core-app/features/hal/resources/query-resource';
import { APIV3Service } from 'core-app/core/apiv3/api-v3.service';
-import { ViewResource } from 'core-app/features/hal/resources/views-resource';
import { Observable } from 'rxjs';
+import { View } from 'core-app/core/state/views/view.model';
@Injectable()
export class WorkPackagesQueryViewService {
@@ -12,7 +12,7 @@ export class WorkPackagesQueryViewService {
protected apiV3Service:APIV3Service,
) { }
- create(query:QueryResource):Observable<ViewResource> {
+ create(query:QueryResource):Observable<View> {
return this
.apiV3Service
.views