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

Remember which collections we're loading

Oliver Güntherda5b9cd93976
diff --git a/frontend/src/app/core/state/capabilities/capabilities.service.ts b/frontend/src/app/core/state/capabilities/capabilities.service.ts
index 0713952b7cd..2bc96239491 100644
--- a/frontend/src/app/core/state/capabilities/capabilities.service.ts
+++ b/frontend/src/app/core/state/capabilities/capabilities.service.ts
@@ -69,7 +69,7 @@ export class CapabilitiesResourceService extends ResourceCollectionService<ICapa
*/
public require$(params:ApiV3ListParameters):Observable<ICapability[]> {
const key = collectionKey(params);
- if (this.collectionExists(key)) {
+ if (this.collectionExists(key) || this.collectionLoading(key)) {
return this.collection(key);
}
@@ -125,13 +125,9 @@ export class CapabilitiesResourceService extends ResourceCollectionService<ICapa
}
fetchCapabilities(params:ApiV3ListParameters):Observable<IHALCollection<ICapability>> {
- const collectionURL = collectionKey(params);
-
return this
- .http
- .get<IHALCollection<ICapability>>(this.capabilitiesPath + collectionURL)
+ .fetchCollection(this.http, this.capabilitiesPath, params)
.pipe(
- tap((collection) => insertCollectionIntoState(this.store, collection, collectionURL)),
catchError((error) => {
this.toastService.addError(error);
throw error;
diff --git a/frontend/src/app/core/state/collection-store.ts b/frontend/src/app/core/state/collection-store.ts
index cebd0b88fd7..b1b16e4ca6c 100644
--- a/frontend/src/app/core/state/collection-store.ts
+++ b/frontend/src/app/core/state/collection-store.ts
@@ -19,6 +19,9 @@ export interface CollectionResponse {
export interface CollectionState<T> extends EntityState<T> {
/** Loaded notification collections */
collections:Record<string, CollectionResponse>;
+
+ /** Loading collections */
+ loadingCollections:Record<string, boolean>;
}
export interface CollectionItem {
@@ -34,9 +37,10 @@ export function mapHALCollectionToIDCollection<T extends CollectionItem>(collect
/**
* Initialize the collection part of the entity store
*/
-export function createInitialCollectionState():{ collections:Record<string, CollectionResponse> } {
+export function createInitialCollectionState<T>():CollectionState<T> {
return {
collections: {},
+ loadingCollections: {},
};
}
@@ -49,6 +53,28 @@ export function collectionKey(params:ApiV3ListParameters):string {
return listParamsString(params);
}
+/**
+ * Mark a collection key as being loaded
+ *
+ * @param store An entity store for the collection
+ * @param collectionUrl The key to insert the collection at
+ * @param loading The loading state
+ */
+export function setCollectionLoading<T extends { id:ID }>(
+ store:EntityStore<CollectionState<T>>,
+ collectionUrl:string,
+ loading:boolean,
+):void {
+ store.update(({ loadingCollections }) => (
+ {
+ loadingCollections: {
+ ...loadingCollections,
+ [collectionUrl]: loading,
+ },
+ }
+ ));
+}
+
/**
* Insert a collection into the given entity store
*
@@ -129,7 +155,10 @@ export function collectionFrom<T>(elements:T[]):IHALCollection<T> {
export function extendCollectionElementsWithId<T extends { _links:IHalResourceLinks }>(
collection:IHALCollection<T>,
):IHALCollection<T&{ id:ID }> {
- const elements = collection._embedded.elements.map((element) => ({ ...element, id: idFromLink(element._links.self.href) }));
+ const elements = collection._embedded.elements.map((element) => ({
+ ...element,
+ id: idFromLink(element._links.self.href),
+ }));
return {
...collection,
diff --git a/frontend/src/app/core/state/resource-collection.service.ts b/frontend/src/app/core/state/resource-collection.service.ts
index 75d7aa9782b..d280511c4c4 100644
--- a/frontend/src/app/core/state/resource-collection.service.ts
+++ b/frontend/src/app/core/state/resource-collection.service.ts
@@ -33,21 +33,29 @@ import {
} from '@datorama/akita';
import { Observable } from 'rxjs';
import {
+ catchError,
filter,
+ finalize,
map,
switchMap,
+ tap,
} from 'rxjs/operators';
import {
collectionKey,
CollectionState,
+ insertCollectionIntoState,
+ setCollectionLoading,
} from 'core-app/core/state/collection-store';
import { omit } from 'lodash';
import isDefinedEntity from 'core-app/core/state/is-defined-entity';
import { ApiV3ListParameters } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
+import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
+import { ICapability } from 'core-app/core/state/capabilities/capability.model';
+import { HttpClient } from '@angular/common/http';
export type CollectionStore<T> = EntityStore<CollectionState<T>>;
-export abstract class ResourceCollectionService<T> {
+export abstract class ResourceCollectionService<T extends { id:ID }> {
protected store:CollectionStore<T> = this.createStore();
protected query = new QueryEntity(this.store);
@@ -106,6 +114,17 @@ export abstract class ResourceCollectionService<T> {
.collections[key];
}
+ /**
+ * Checks, if the store already has a collection given the key
+ */
+ collectionLoading(input:string|ApiV3ListParameters):boolean {
+ const key = typeof input === 'string' ? input : collectionKey(input);
+ return this
+ .query
+ .getValue()
+ .loadingCollections[key] === true;
+ }
+
/**
* Clear a collection key
* @param key Collection key to clear
@@ -135,4 +154,20 @@ export abstract class ResourceCollectionService<T> {
* @protected
*/
protected abstract createStore():CollectionStore<T>;
+
+ /**
+ * Fetch a given collection, ensuring it is being flagged as loaded
+ */
+ protected fetchCollection(http:HttpClient, basePath:string, params:ApiV3ListParameters) {
+ const key = collectionKey(params);
+
+ setCollectionLoading(this.store, key, true);
+
+ return http
+ .get<IHALCollection<T>>(basePath + key)
+ .pipe(
+ tap((collection) => insertCollectionIntoState(this.store, collection, key)),
+ finalize(() => setCollectionLoading(this.store, key, false)),
+ );
+ }
}