2019-02-26 16:25:52 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient} from "@angular/common/http";
|
2018-03-14 14:29:53 +01:00
|
|
|
|
2019-02-26 16:25:52 +01:00
|
|
|
import {CommunityInfo} from '../community/communityInfo';
|
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
2020-11-12 16:41:03 +01:00
|
|
|
import {BehaviorSubject, Subscriber} from "rxjs";
|
2019-06-03 15:20:36 +02:00
|
|
|
import {map} from "rxjs/operators";
|
2018-03-14 14:29:53 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CommunitiesService {
|
|
|
|
|
2019-04-18 14:47:26 +02:00
|
|
|
public communities: BehaviorSubject<CommunityInfo[]> = null;
|
|
|
|
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor(private http: HttpClient) {
|
2020-08-11 14:16:50 +02:00
|
|
|
this.communities = new BehaviorSubject([]);
|
2019-04-18 14:47:26 +02:00
|
|
|
}
|
2020-11-12 16:41:03 +01:00
|
|
|
sub;
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.clearSubscriptions();
|
|
|
|
}
|
|
|
|
clearSubscriptions(){
|
|
|
|
if (this.sub instanceof Subscriber) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
2019-04-18 14:47:26 +02:00
|
|
|
updateCommunities(properties: EnvProperties, url: string) {
|
2020-11-12 16:41:03 +01:00
|
|
|
this.sub = this.getCommunities(properties, url).subscribe(res => {
|
2020-08-11 14:16:50 +02:00
|
|
|
this.communities.next(res);
|
|
|
|
}, error => {
|
|
|
|
this.communities.error(error);
|
2019-04-18 14:47:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getCommunitiesState() {
|
|
|
|
return this.communities.asObservable();
|
2018-03-14 14:29:53 +01:00
|
|
|
}
|
|
|
|
|
2019-02-26 16:25:52 +01:00
|
|
|
getCommunities(properties: EnvProperties, url: string) {
|
|
|
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(map(res => this.parseCommunities(res)));
|
2018-03-14 14:29:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
parseCommunities(data: any): CommunityInfo[] {
|
2019-02-26 16:25:52 +01:00
|
|
|
const communities: CommunityInfo[] = [];
|
|
|
|
|
|
|
|
const length = Array.isArray(data) ? data.length : 1;
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
const resData = Array.isArray(data) ? data[i] : data;
|
|
|
|
const result: CommunityInfo = new CommunityInfo();
|
|
|
|
result['title'] = resData.name;
|
|
|
|
result['shortTitle'] = resData.shortName;
|
|
|
|
result['communityId'] = resData.id;
|
|
|
|
result['queryId'] = resData.queryId;
|
|
|
|
result['logoUrl'] = resData.logoUrl;
|
|
|
|
result['description'] = resData.description;
|
|
|
|
result['date'] = resData.creationDate;
|
|
|
|
result['status'] = 'all';
|
|
|
|
if (resData.hasOwnProperty('status')) {
|
|
|
|
result['status'] = resData.status;
|
|
|
|
const status = ['all', 'hidden', 'manager'];
|
|
|
|
if (status.indexOf(result['status']) === -1) {
|
|
|
|
result['status'] = 'hidden';
|
2018-03-26 16:13:28 +02:00
|
|
|
}
|
2019-02-26 16:25:52 +01:00
|
|
|
}
|
|
|
|
if (resData.type != null) {
|
|
|
|
result['type'] = resData.type;
|
|
|
|
}
|
2018-03-26 16:13:28 +02:00
|
|
|
|
2019-02-26 16:25:52 +01:00
|
|
|
if (resData.managers != null) {
|
|
|
|
if (result['managers'] === undefined) {
|
|
|
|
result['managers'] = new Array<string>();
|
2018-03-14 14:29:53 +01:00
|
|
|
}
|
|
|
|
|
2019-02-26 16:25:52 +01:00
|
|
|
const managers = resData.managers;
|
|
|
|
const lengthManagers = Array.isArray(managers) ? managers.length : 1;
|
2018-03-14 14:29:53 +01:00
|
|
|
|
2019-02-26 16:25:52 +01:00
|
|
|
for (let j = 0; j < lengthManagers; j++) {
|
|
|
|
const manager = Array.isArray(managers) ? managers[j] : managers;
|
|
|
|
result.managers[j] = manager;
|
2018-05-03 13:27:42 +02:00
|
|
|
}
|
2019-02-26 16:25:52 +01:00
|
|
|
}
|
2018-03-14 14:29:53 +01:00
|
|
|
|
2019-02-26 16:25:52 +01:00
|
|
|
if (resData.subjects != null) {
|
|
|
|
if (result['subjects'] === undefined) {
|
|
|
|
result['subjects'] = new Array<string>();
|
2018-03-14 14:29:53 +01:00
|
|
|
}
|
|
|
|
|
2019-02-26 16:25:52 +01:00
|
|
|
const subjects = resData.subjects;
|
|
|
|
const lengthSubjects = Array.isArray(subjects) ? subjects.length : 1;
|
2018-03-14 14:29:53 +01:00
|
|
|
|
2019-02-26 16:25:52 +01:00
|
|
|
for (let j = 0; j < lengthSubjects; j++) {
|
|
|
|
const subject = Array.isArray(subjects) ? subjects[i] : subjects;
|
|
|
|
result.subjects[j] = subject;
|
2018-03-14 14:29:53 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-26 16:25:52 +01:00
|
|
|
if (result['type'] === 'community' || result['type'] === 'ri') {
|
|
|
|
communities.push(result);
|
2018-05-29 16:46:14 +02:00
|
|
|
}
|
2018-03-14 14:29:53 +01:00
|
|
|
}
|
2021-11-12 11:36:16 +01:00
|
|
|
return CommunityInfo.checkIsUpload(communities);
|
2018-03-14 14:29:53 +01:00
|
|
|
}
|
|
|
|
}
|