From 4214b08f8795a56467a6738de83065bcdfd04cf3 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Mon, 29 Mar 2021 07:39:48 +0000 Subject: [PATCH] [Library | Trunk]: Community Service: fix getCommunity method to avoid multiple requests git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@60735 d315682c-612b-4755-9ff5-7f18f6832af3 --- connect/community/community.service.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/connect/community/community.service.ts b/connect/community/community.service.ts index d9d1bc60..314e1744 100644 --- a/connect/community/community.service.ts +++ b/connect/community/community.service.ts @@ -12,16 +12,12 @@ import {StringUtils} from "../../utils/string-utils.class"; @Injectable({providedIn: 'root'}) export class CommunityService { - public community: BehaviorSubject = null; + public community: BehaviorSubject = new BehaviorSubject(null); + public communityId: string = null; private promise: Promise = null; - private subs=[]; - constructor(private http: HttpClient) { - this.community = new BehaviorSubject(null); - } + private subs = []; - ngOnDestroy() { - this.clearSubscriptions(); - } + constructor(private http: HttpClient) {} clearSubscriptions() { this.subs.forEach(subscription => { @@ -38,9 +34,10 @@ export class CommunityService { setCommunity(community: CommunityInfo) { this.community.next(community); } - + getCommunity(communityId: string, refresh = false) { - if(!this.community.value || this.community.value.communityId !== communityId || refresh) { + if (this.communityId !== communityId || !this.community.value || refresh) { + this.communityId = communityId; this.promise = new Promise((resolve, reject) => { this.subs.push(this.http.get(properties.communityAPI + communityId) .pipe(map(community => this.parseCommunity(community))).subscribe(community => { @@ -58,11 +55,12 @@ export class CommunityService { async getCommunityAsync() { await this.promise; + this.clearSubscriptions(); return this.community.getValue(); } private checkIsUpload(response: CommunityInfo | CommunityInfo[]): any | any[] { - if(Array.isArray(response)) { + if (Array.isArray(response)) { response.forEach(value => { value.isUpload = value.logoUrl && !StringUtils.isValidUrl(value.logoUrl); }); @@ -76,7 +74,7 @@ export class CommunityService { updateCommunity(url: string, community: any) { //const headers = new Headers({'Content-Type': 'application/json'}); //const options = new RequestOptions({headers: headers}); - if(!community.logoUrl) { + if (!community.logoUrl) { community.logoUrl = ''; } const options = {