[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
This commit is contained in:
k.triantafyllou 2021-03-29 07:39:48 +00:00
parent 1f643b347c
commit 4214b08f87
1 changed files with 10 additions and 12 deletions

View File

@ -12,16 +12,12 @@ import {StringUtils} from "../../utils/string-utils.class";
@Injectable({providedIn: 'root'}) @Injectable({providedIn: 'root'})
export class CommunityService { export class CommunityService {
public community: BehaviorSubject<CommunityInfo> = null; public community: BehaviorSubject<CommunityInfo> = new BehaviorSubject(null);
public communityId: string = null;
private promise: Promise<boolean> = null; private promise: Promise<boolean> = null;
private subs=[]; private subs = [];
constructor(private http: HttpClient) {
this.community = new BehaviorSubject(null);
}
ngOnDestroy() { constructor(private http: HttpClient) {}
this.clearSubscriptions();
}
clearSubscriptions() { clearSubscriptions() {
this.subs.forEach(subscription => { this.subs.forEach(subscription => {
@ -40,7 +36,8 @@ export class CommunityService {
} }
getCommunity(communityId: string, refresh = false) { 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<any>((resolve, reject) => { this.promise = new Promise<any>((resolve, reject) => {
this.subs.push(this.http.get<CommunityInfo>(properties.communityAPI + communityId) this.subs.push(this.http.get<CommunityInfo>(properties.communityAPI + communityId)
.pipe(map(community => this.parseCommunity(community))).subscribe(community => { .pipe(map(community => this.parseCommunity(community))).subscribe(community => {
@ -58,11 +55,12 @@ export class CommunityService {
async getCommunityAsync() { async getCommunityAsync() {
await this.promise; await this.promise;
this.clearSubscriptions();
return this.community.getValue(); return this.community.getValue();
} }
private checkIsUpload(response: CommunityInfo | CommunityInfo[]): any | any[] { private checkIsUpload(response: CommunityInfo | CommunityInfo[]): any | any[] {
if(Array.isArray(response)) { if (Array.isArray(response)) {
response.forEach(value => { response.forEach(value => {
value.isUpload = value.logoUrl && !StringUtils.isValidUrl(value.logoUrl); value.isUpload = value.logoUrl && !StringUtils.isValidUrl(value.logoUrl);
}); });
@ -76,7 +74,7 @@ export class CommunityService {
updateCommunity(url: string, community: any) { updateCommunity(url: string, community: any) {
//const headers = new Headers({'Content-Type': 'application/json'}); //const headers = new Headers({'Content-Type': 'application/json'});
//const options = new RequestOptions({headers: headers}); //const options = new RequestOptions({headers: headers});
if(!community.logoUrl) { if (!community.logoUrl) {
community.logoUrl = ''; community.logoUrl = '';
} }
const options = { const options = {