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

Add capability store and extract common helper function to insert

Oliver Günther038c7552ed2c
diff --git a/frontend/src/app/core/state/capabilities/capabilities.query.ts b/frontend/src/app/core/state/capabilities/capabilities.query.ts
new file mode 100644
index 00000000000..3383c51d578
--- /dev/null
+++ b/frontend/src/app/core/state/capabilities/capabilities.query.ts
@@ -0,0 +1,5 @@
+import { QueryEntity } from '@datorama/akita';
+import { CapabilitiesState } from 'core-app/core/state/capabilities/capabilities.store';
+
+export class CapabilitiesQuery extends QueryEntity<CapabilitiesState> {
+}
diff --git a/frontend/src/app/core/state/capabilities/capabilities.service.ts b/frontend/src/app/core/state/capabilities/capabilities.service.ts
new file mode 100644
index 00000000000..fa5468fd886
--- /dev/null
+++ b/frontend/src/app/core/state/capabilities/capabilities.service.ts
@@ -0,0 +1,59 @@
+import { Injectable } from '@angular/core';
+import {
+ catchError,
+ tap,
+} from 'rxjs/operators';
+import { Observable } from 'rxjs';
+import { ID } from '@datorama/akita';
+import { HttpClient } from '@angular/common/http';
+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 { ApiV3ListParameters } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
+import {
+ collectionKey,
+ insertCollectionIntoState,
+} from 'core-app/core/state/collection-store';
+import { ICapability } from 'core-app/core/state/capabilities/capability.model';
+import { CapabilitiesStore } from 'core-app/core/state/capabilities/capabilities.store';
+import { CapabilitiesQuery } from 'core-app/core/state/capabilities/capabilities.query';
+
+@Injectable()
+export class CapabilitiesResourceService {
+ protected store = new CapabilitiesStore();
+
+ readonly query = new CapabilitiesQuery(this.store);
+
+ private get capabilitiesPath():string {
+ return this
+ .apiV3Service
+ .capabilities
+ .path;
+ }
+
+ constructor(
+ private http:HttpClient,
+ private apiV3Service:ApiV3Service,
+ private toastService:ToastService,
+ ) {
+ }
+
+ fetchCapabilities(params:ApiV3ListParameters):Observable<IHALCollection<ICapability>> {
+ const collectionURL = collectionKey(params);
+
+ return this
+ .http
+ .get<IHALCollection<ICapability>>(this.capabilitiesPath + collectionURL)
+ .pipe(
+ tap((collection) => insertCollectionIntoState(this.store, collection, collectionURL)),
+ catchError((error) => {
+ this.toastService.addError(error);
+ throw error;
+ }),
+ );
+ }
+
+ update(id:ID, project:Partial<ICapability>):void {
+ this.store.update(id, project);
+ }
+}
diff --git a/frontend/src/app/core/state/capabilities/capabilities.store.ts b/frontend/src/app/core/state/capabilities/capabilities.store.ts
new file mode 100644
index 00000000000..88f16737fdf
--- /dev/null
+++ b/frontend/src/app/core/state/capabilities/capabilities.store.ts
@@ -0,0 +1,13 @@
+import { EntityStore, StoreConfig } from '@datorama/akita';
+import { CollectionState, createInitialCollectionState } from 'core-app/core/state/collection-store';
+import { ICapability } from 'core-app/core/state/capabilities/capability.model';
+
+export interface CapabilitiesState extends CollectionState<ICapability> {
+}
+
+@StoreConfig({ name: 'capabilities' })
+export class CapabilitiesStore extends EntityStore<CapabilitiesState> {
+ constructor() {
+ super(createInitialCollectionState());
+ }
+}
diff --git a/frontend/src/app/core/state/capabilities/capability.model.ts b/frontend/src/app/core/state/capabilities/capability.model.ts
new file mode 100644
index 00000000000..9db444fe1e7
--- /dev/null
+++ b/frontend/src/app/core/state/capabilities/capability.model.ts
@@ -0,0 +1,17 @@
+import {
+ IHalResourceLink,
+ IHalResourceLinks,
+} from 'core-app/core/state/hal-resource';
+
+export interface ICapabilityHalResourceLinks extends IHalResourceLinks {
+ self:IHalResourceLink;
+
+ action:IHalResourceLink;
+ context:IHalResourceLink;
+ principal:IHalResourceLink;
+}
+
+export interface ICapability {
+ id:string;
+ _links:ICapabilityHalResourceLinks;
+}
diff --git a/frontend/src/app/core/state/collection-store.ts b/frontend/src/app/core/state/collection-store.ts
index f0a5ac70201..2a15e9f8eb8 100644
--- a/frontend/src/app/core/state/collection-store.ts
+++ b/frontend/src/app/core/state/collection-store.ts
@@ -1,5 +1,7 @@
import {
+ applyTransaction,
EntityState,
+ EntityStore,
ID,
QueryEntity,
} from '@datorama/akita';
@@ -12,6 +14,7 @@ import { Observable } from 'rxjs';
import {
filter,
} from 'rxjs/operators';
+import { HasId } from 'core-app/core/apiv3/cache/state-cache.service';
export interface CollectionResponse {
ids:ID[];
@@ -100,3 +103,33 @@ export function selectCollectionAsEntities$<T extends CollectionItem>(service:Co
return selectEntitiesFromIDCollection(service, collection);
}
+
+/**
+ * Insert a collection into the given entity store
+ *
+ * @param store An entity store for the collection
+ * @param collection A loaded collection
+ * @param collectionUrl The key to insert the collection at
+ */
+export function insertCollectionIntoState<T extends { id:ID }>(
+ store:EntityStore<CollectionState<T>>,
+ collection:IHALCollection<T>,
+ collectionUrl:string,
+):void {
+ // Some JSON endpoints return no elements result if there are no elements
+ const ids = collection._embedded.elements?.map((el) => el.id) || [];
+
+ applyTransaction(() => {
+ store.add(collection._embedded.elements);
+ store.update(({ collections }) => (
+ {
+ collections: {
+ ...collections,
+ [collectionUrl]: {
+ ids,
+ },
+ },
+ }
+ ));
+ });
+}
diff --git a/frontend/src/app/core/state/in-app-notifications/in-app-notifications.service.ts b/frontend/src/app/core/state/in-app-notifications/in-app-notifications.service.ts
index 1598ed033d2..14983eac198 100644
--- a/frontend/src/app/core/state/in-app-notifications/in-app-notifications.service.ts
+++ b/frontend/src/app/core/state/in-app-notifications/in-app-notifications.service.ts
@@ -14,7 +14,10 @@ import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
import { HttpClient } from '@angular/common/http';
import { InAppNotificationsQuery } from 'core-app/core/state/in-app-notifications/in-app-notifications.query';
import { ApiV3ListParameters } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
-import { collectionKey } from 'core-app/core/state/collection-store';
+import {
+ collectionKey,
+ insertCollectionIntoState,
+} from 'core-app/core/state/collection-store';
import {
markNotificationsAsRead,
notificationsMarkedRead,
@@ -56,22 +59,7 @@ export class InAppNotificationsResourceService {
.http
.get<IHALCollection<InAppNotification>>(this.notificationsPath + 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),
- },
- },
- }
- ));
- });
- }),
+ tap((collection) => insertCollectionIntoState(this.store, collection, collectionURL)),
catchError((error) => {
this.toastService.addError(error);
throw error;
diff --git a/frontend/src/app/core/state/openproject-state.module.ts b/frontend/src/app/core/state/openproject-state.module.ts
index ac287f28e08..b0f45469c30 100644
--- a/frontend/src/app/core/state/openproject-state.module.ts
+++ b/frontend/src/app/core/state/openproject-state.module.ts
@@ -32,12 +32,14 @@ import {
import { InAppNotificationsResourceService } from './in-app-notifications/in-app-notifications.service';
import { ProjectsResourceService } from './projects/projects.service';
import { PrincipalsResourceService } from './principals/principals.service';
+import { CapabilitiesResourceService } from 'core-app/core/state/capabilities/capabilities.service';
@NgModule({
providers: [
InAppNotificationsResourceService,
ProjectsResourceService,
PrincipalsResourceService,
+ CapabilitiesResourceService,
],
})
export class OpenProjectStateModule {
diff --git a/frontend/src/app/core/state/principals/principals.service.ts b/frontend/src/app/core/state/principals/principals.service.ts
index 241aca40f99..de9c3b04c75 100644
--- a/frontend/src/app/core/state/principals/principals.service.ts
+++ b/frontend/src/app/core/state/principals/principals.service.ts
@@ -14,7 +14,10 @@ import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
import { HttpClient } from '@angular/common/http';
import { PrincipalsQuery } from 'core-app/core/state/principals/principals.query';
import { ApiV3ListParameters } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
-import { collectionKey } from 'core-app/core/state/collection-store';
+import {
+ collectionKey,
+ insertCollectionIntoState,
+} from 'core-app/core/state/collection-store';
import {
EffectHandler,
} from 'core-app/core/state/effects/effect-handler.decorator';
@@ -51,22 +54,7 @@ export class PrincipalsResourceService {
.http
.get<IHALCollection<IPrincipal>>(this.principalsPath + 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),
- },
- },
- }
- ));
- });
- }),
+ tap((collection) => insertCollectionIntoState(this.store, collection, collectionURL)),
catchError((error) => {
this.toastService.addError(error);
throw error;
diff --git a/frontend/src/app/core/state/projects/projects.service.ts b/frontend/src/app/core/state/projects/projects.service.ts
index 67764a05383..3d06b39c9a8 100644
--- a/frontend/src/app/core/state/projects/projects.service.ts
+++ b/frontend/src/app/core/state/projects/projects.service.ts
@@ -14,7 +14,10 @@ import { ToastService } from 'core-app/shared/components/toaster/toast.service';
import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
import { ProjectsQuery } from 'core-app/core/state/projects/projects.query';
import { ApiV3ListParameters } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
-import { collectionKey } from 'core-app/core/state/collection-store';
+import {
+ collectionKey,
+ insertCollectionIntoState,
+} from 'core-app/core/state/collection-store';
import { ProjectsStore } from './projects.store';
import { IProject } from './project.model';
@@ -45,21 +48,7 @@ export class ProjectsResourceService {
.http
.get<IHALCollection<IProject>>(this.projectsPath + collectionURL)
.pipe(
- tap((events) => {
- applyTransaction(() => {
- this.store.add(events._embedded.elements);
- this.store.update(({ collections }) => (
- {
- collections: {
- ...collections,
- [collectionURL]: {
- ids: events._embedded.elements.map((el) => el.id),
- },
- },
- }
- ));
- });
- }),
+ tap((collection) => insertCollectionIntoState(this.store, collection, collectionURL)),
catchError((error) => {
this.toastService.addError(error);
throw error;