1
|
import {Injectable} from '@angular/core';
|
2
|
import {Http} from '@angular/http';
|
3
|
import {HttpClient} from "@angular/common/http";
|
4
|
|
5
|
import {CommunityInfo} from '../community/communityInfo';
|
6
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
7
|
import {BehaviorSubject, Observable} from "rxjs";
|
8
|
import {map} from "rxjs/operators";
|
9
|
|
10
|
@Injectable()
|
11
|
export class CommunitiesService {
|
12
|
|
13
|
public communities: BehaviorSubject<CommunityInfo[]> = null;
|
14
|
|
15
|
constructor(private http: HttpClient) {
|
16
|
this.communities = new BehaviorSubject([]);
|
17
|
}
|
18
|
|
19
|
updateCommunities(properties: EnvProperties, url: string) {
|
20
|
this.getCommunities(properties, url).subscribe(res => {
|
21
|
this.communities.next(res);
|
22
|
})
|
23
|
}
|
24
|
|
25
|
getCommunitiesState() {
|
26
|
return this.communities.asObservable();
|
27
|
}
|
28
|
|
29
|
getCommunities(properties: EnvProperties, url: string) {
|
30
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
31
|
//.map(res => <any> res.json())
|
32
|
.pipe(map(res => this.parseCommunities(res)));
|
33
|
}
|
34
|
|
35
|
parseCommunities(data: any): CommunityInfo[] {
|
36
|
const communities: CommunityInfo[] = [];
|
37
|
|
38
|
const length = Array.isArray(data) ? data.length : 1;
|
39
|
|
40
|
for (let i = 0; i < length; i++) {
|
41
|
const resData = Array.isArray(data) ? data[i] : data;
|
42
|
const result: CommunityInfo = new CommunityInfo();
|
43
|
result['title'] = resData.name;
|
44
|
result['shortTitle'] = resData.shortName;
|
45
|
result['communityId'] = resData.id;
|
46
|
result['queryId'] = resData.queryId;
|
47
|
result['logoUrl'] = resData.logoUrl;
|
48
|
result['description'] = resData.description;
|
49
|
result['date'] = resData.creationDate;
|
50
|
result['status'] = 'all';
|
51
|
if (resData.hasOwnProperty('status')) {
|
52
|
result['status'] = resData.status;
|
53
|
const status = ['all', 'hidden', 'manager'];
|
54
|
if (status.indexOf(result['status']) === -1) {
|
55
|
result['status'] = 'hidden';
|
56
|
}
|
57
|
}
|
58
|
if (resData.type != null) {
|
59
|
result['type'] = resData.type;
|
60
|
}
|
61
|
|
62
|
if (resData.managers != null) {
|
63
|
if (result['managers'] === undefined) {
|
64
|
result['managers'] = new Array<string>();
|
65
|
}
|
66
|
|
67
|
const managers = resData.managers;
|
68
|
const lengthManagers = Array.isArray(managers) ? managers.length : 1;
|
69
|
|
70
|
for (let j = 0; j < lengthManagers; j++) {
|
71
|
const manager = Array.isArray(managers) ? managers[j] : managers;
|
72
|
result.managers[j] = manager;
|
73
|
}
|
74
|
}
|
75
|
|
76
|
if (resData.subjects != null) {
|
77
|
if (result['subjects'] === undefined) {
|
78
|
result['subjects'] = new Array<string>();
|
79
|
}
|
80
|
|
81
|
const subjects = resData.subjects;
|
82
|
const lengthSubjects = Array.isArray(subjects) ? subjects.length : 1;
|
83
|
|
84
|
for (let j = 0; j < lengthSubjects; j++) {
|
85
|
const subject = Array.isArray(subjects) ? subjects[i] : subjects;
|
86
|
result.subjects[j] = subject;
|
87
|
}
|
88
|
}
|
89
|
if (result['type'] === 'community' || result['type'] === 'ri') {
|
90
|
communities.push(result);
|
91
|
}
|
92
|
}
|
93
|
return communities;
|
94
|
}
|
95
|
}
|