2021-01-21 16:15:53 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient, HttpHeaders} from "@angular/common/http";
|
2021-01-21 16:15:53 +01:00
|
|
|
import {CommunityInfo} from './communityInfo';
|
2019-03-07 16:43:54 +01:00
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {map} from "rxjs/operators";
|
2021-02-19 18:50:34 +01:00
|
|
|
import {BehaviorSubject, from, Observable, Subscriber} from "rxjs";
|
2020-09-24 13:18:24 +02:00
|
|
|
import {properties} from "../../../../environments/environment";
|
2021-01-21 16:15:53 +01:00
|
|
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
2021-02-16 11:25:23 +01:00
|
|
|
import {Stakeholder} from "../../monitor/entities/stakeholder";
|
|
|
|
import {StringUtils} from "../../utils/string-utils.class";
|
2018-03-07 12:08:03 +01:00
|
|
|
|
2021-01-21 16:15:53 +01:00
|
|
|
@Injectable({providedIn: 'root'})
|
2018-03-07 12:08:03 +01:00
|
|
|
export class CommunityService {
|
2021-01-21 16:15:53 +01:00
|
|
|
|
2020-07-17 01:08:10 +02:00
|
|
|
public community: BehaviorSubject<CommunityInfo> = null;
|
|
|
|
private promise: Promise<boolean> = null;
|
2021-03-26 16:45:30 +01:00
|
|
|
private subs=[];
|
2020-07-17 01:08:10 +02:00
|
|
|
constructor(private http: HttpClient) {
|
|
|
|
this.community = new BehaviorSubject(null);
|
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
2020-11-12 16:41:03 +01:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.clearSubscriptions();
|
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
clearSubscriptions() {
|
2021-03-26 16:45:30 +01:00
|
|
|
this.subs.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscriber) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
2020-11-12 16:41:03 +01:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
public getCommunityAsObservable() {
|
|
|
|
return this.community.asObservable();
|
|
|
|
}
|
|
|
|
|
|
|
|
setCommunity(community: CommunityInfo) {
|
|
|
|
this.community.next(community);
|
|
|
|
}
|
2021-02-19 19:08:49 +01:00
|
|
|
|
|
|
|
getCommunity(communityId: string, refresh = false) {
|
2021-02-16 11:25:23 +01:00
|
|
|
if(!this.community.value || this.community.value.communityId !== communityId || refresh) {
|
2021-01-21 16:15:53 +01:00
|
|
|
this.promise = new Promise<any>((resolve, reject) => {
|
2021-03-26 16:45:30 +01:00
|
|
|
this.subs.push(this.http.get<CommunityInfo>(properties.communityAPI + communityId)
|
2021-02-16 11:25:23 +01:00
|
|
|
.pipe(map(community => this.parseCommunity(community))).subscribe(community => {
|
2021-01-21 16:15:53 +01:00
|
|
|
this.community.next(community);
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.community.next(null);
|
2021-01-22 19:09:44 +01:00
|
|
|
resolve();
|
2021-03-26 16:45:30 +01:00
|
|
|
}));
|
2021-01-21 16:15:53 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return from(this.getCommunityAsync());
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
async getCommunityAsync() {
|
|
|
|
await this.promise;
|
|
|
|
return this.community.getValue();
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:25:23 +01:00
|
|
|
private checkIsUpload(response: CommunityInfo | CommunityInfo[]): any | any[] {
|
|
|
|
if(Array.isArray(response)) {
|
|
|
|
response.forEach(value => {
|
|
|
|
value.isUpload = value.logoUrl && !StringUtils.isValidUrl(value.logoUrl);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
response.isUpload = response.logoUrl && !StringUtils.isValidUrl(response.logoUrl);
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-17 01:08:10 +02:00
|
|
|
updateCommunity(url: string, community: any) {
|
2021-01-21 16:15:53 +01:00
|
|
|
//const headers = new Headers({'Content-Type': 'application/json'});
|
|
|
|
//const options = new RequestOptions({headers: headers});
|
2021-02-16 11:25:23 +01:00
|
|
|
if(!community.logoUrl) {
|
|
|
|
community.logoUrl = '';
|
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
const options = {
|
|
|
|
headers: new HttpHeaders({
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
const body = JSON.stringify(community);
|
|
|
|
return this.http.post(url, body, options);
|
|
|
|
/*.map(res => res.json())*/
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
2021-03-01 18:27:45 +01:00
|
|
|
public parseCommunity(data: any): CommunityInfo {
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
const resData = Array.isArray(data) ? data[0] : data;
|
|
|
|
|
|
|
|
const community: CommunityInfo = new CommunityInfo();
|
|
|
|
community['title'] = resData.name;
|
|
|
|
community['shortTitle'] = resData.shortName;
|
|
|
|
community['communityId'] = resData.id;
|
|
|
|
community['queryId'] = resData.queryId;
|
|
|
|
community['logoUrl'] = resData.logoUrl;
|
|
|
|
community['description'] = resData.description;
|
|
|
|
community['date'] = resData.creationDate;
|
|
|
|
community['zenodoCommunity'] = resData.zenodoCommunity;
|
|
|
|
community['status'] = 'all';
|
|
|
|
if (resData.hasOwnProperty('status')) {
|
|
|
|
community['status'] = resData.status;
|
|
|
|
const status = ['all', 'hidden', 'manager'];
|
|
|
|
if (status.indexOf(community['status']) === -1) {
|
|
|
|
community['status'] = 'hidden';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resData.type != null) {
|
|
|
|
community['type'] = resData.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resData.managers != null) {
|
|
|
|
if (community['managers'] === undefined) {
|
|
|
|
community['managers'] = new Array<string>();
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
const managers = resData.managers;
|
|
|
|
const length = Array.isArray(managers) ? managers.length : 1;
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
const manager = Array.isArray(managers) ? managers[i] : managers;
|
|
|
|
community.managers[i] = manager;
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (resData.subjects != null) {
|
|
|
|
if (community['subjects'] === undefined) {
|
|
|
|
community['subjects'] = new Array<string>();
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
const subjects = resData.subjects;
|
|
|
|
const length = Array.isArray(subjects) ? subjects.length : 1;
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
const subject = Array.isArray(subjects) ? subjects[i] : subjects;
|
|
|
|
community.subjects[i] = subject;
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
}
|
2021-02-16 11:25:23 +01:00
|
|
|
return this.checkIsUpload(community);
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
2021-02-19 18:50:34 +01:00
|
|
|
isRIType(community: string): Observable<boolean> {
|
2021-02-19 19:08:49 +01:00
|
|
|
return this.getCommunity(community).pipe(map(community => community.type === 'ri'));
|
2021-02-19 18:50:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isCommunityType(community: string): Observable<boolean> {
|
2021-02-19 19:08:49 +01:00
|
|
|
return this.getCommunity(community).pipe(map(community => community.type === 'community'));
|
2021-02-19 18:50:34 +01:00
|
|
|
}
|
2018-03-07 12:08:03 +01:00
|
|
|
}
|