[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'})
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 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<any>((resolve, reject) => {
this.subs.push(this.http.get<CommunityInfo>(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 = {