Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient} from "@angular/common/http";
3

    
4
import {CommunityInfo} from '../community/communityInfo';
5
import {EnvProperties} from '../../utils/properties/env-properties';
6
import {BehaviorSubject} from "rxjs";
7
import {map} from "rxjs/operators";
8

    
9
@Injectable()
10
export class CommunitiesService {
11

    
12
    public communities: BehaviorSubject<CommunityInfo[]> = null;
13

    
14
    constructor(private http: HttpClient) {
15
        this.communities = new BehaviorSubject(null);
16
    }
17

    
18
    updateCommunities(properties: EnvProperties, url: string) {
19
        this.getCommunities(properties, url).subscribe(res => {
20
                this.communities.next(res);
21
        })
22
    }
23

    
24
    getCommunitiesState() {
25
        return this.communities.asObservable();
26
    }
27

    
28
    getCommunities(properties: EnvProperties, url: string) {
29
        return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
30
                        //.map(res => <any> res.json())
31
                        .pipe(map(res => this.parseCommunities(res)));
32
    }
33

    
34
    parseCommunities(data: any): CommunityInfo[] {
35
        const communities: CommunityInfo[] = [];
36

    
37
        const length = Array.isArray(data) ? data.length : 1;
38

    
39
        for (let i = 0; i < length; i++) {
40
            const resData = Array.isArray(data) ? data[i] : data;
41
            const result: CommunityInfo = new CommunityInfo();
42
            result['title'] = resData.name;
43
            result['shortTitle'] = resData.shortName;
44
            result['communityId'] = resData.id;
45
            result['queryId'] = resData.queryId;
46
            result['logoUrl'] = resData.logoUrl;
47
            result['description'] = resData.description;
48
            result['date'] = resData.creationDate;
49
            result['status'] = 'all';
50
            if (resData.hasOwnProperty('status')) {
51
                result['status'] = resData.status;
52
                const status = ['all', 'hidden', 'manager'];
53
                if (status.indexOf(result['status']) === -1) {
54
                    result['status'] = 'hidden';
55
                }
56
            }
57
            if (resData.type != null) {
58
                result['type'] = resData.type;
59
            }
60

    
61
            if (resData.managers != null) {
62
                if (result['managers'] === undefined) {
63
                    result['managers'] = new Array<string>();
64
                }
65

    
66
                const managers = resData.managers;
67
                const lengthManagers = Array.isArray(managers) ? managers.length : 1;
68

    
69
                for (let j = 0; j < lengthManagers; j++) {
70
                    const manager = Array.isArray(managers) ? managers[j] : managers;
71
                    result.managers[j] = manager;
72
                }
73
            }
74

    
75
            if (resData.subjects != null) {
76
                if (result['subjects'] === undefined) {
77
                    result['subjects'] = new Array<string>();
78
                }
79

    
80
                const subjects = resData.subjects;
81
                const lengthSubjects = Array.isArray(subjects) ? subjects.length : 1;
82

    
83
                for (let j = 0; j < lengthSubjects; j++) {
84
                    const subject = Array.isArray(subjects) ? subjects[i] : subjects;
85
                    result.subjects[j] = subject;
86
                }
87
            }
88
            if (result['type'] === 'community' || result['type'] === 'ri') {
89
                communities.push(result);
90
            }
91
        }
92
        return communities;
93
    }
94
}
    (1-1/1)