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, Subscriber} 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([]);
16
    }
17
    sub;
18
    ngOnDestroy() {
19
        this.clearSubscriptions();
20
    }
21
    clearSubscriptions(){
22
        if (this.sub instanceof Subscriber) {
23
            this.sub.unsubscribe();
24
        }
25
    }
26
    updateCommunities(properties: EnvProperties, url: string) {
27
        this.sub = this.getCommunities(properties, url).subscribe(res => {
28
            this.communities.next(res);
29
        }, error => {
30
            this.communities.error(error);
31
        })
32
    }
33

    
34
    getCommunitiesState() {
35
        return this.communities.asObservable();
36
    }
37

    
38
    getCommunities(properties: EnvProperties, url: string) {
39
        return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
40
                        .pipe(map(res => this.parseCommunities(res)));
41
    }
42

    
43
    parseCommunities(data: any): CommunityInfo[] {
44
        const communities: CommunityInfo[] = [];
45

    
46
        const length = Array.isArray(data) ? data.length : 1;
47

    
48
        for (let i = 0; i < length; i++) {
49
            const resData = Array.isArray(data) ? data[i] : data;
50
            const result: CommunityInfo = new CommunityInfo();
51
            result['title'] = resData.name;
52
            result['shortTitle'] = resData.shortName;
53
            result['communityId'] = resData.id;
54
            result['queryId'] = resData.queryId;
55
            result['logoUrl'] = resData.logoUrl;
56
            result['description'] = resData.description;
57
            result['date'] = resData.creationDate;
58
            result['status'] = 'all';
59
            if (resData.hasOwnProperty('status')) {
60
                result['status'] = resData.status;
61
                const status = ['all', 'hidden', 'manager'];
62
                if (status.indexOf(result['status']) === -1) {
63
                    result['status'] = 'hidden';
64
                }
65
            }
66
            if (resData.type != null) {
67
                result['type'] = resData.type;
68
            }
69

    
70
            if (resData.managers != null) {
71
                if (result['managers'] === undefined) {
72
                    result['managers'] = new Array<string>();
73
                }
74

    
75
                const managers = resData.managers;
76
                const lengthManagers = Array.isArray(managers) ? managers.length : 1;
77

    
78
                for (let j = 0; j < lengthManagers; j++) {
79
                    const manager = Array.isArray(managers) ? managers[j] : managers;
80
                    result.managers[j] = manager;
81
                }
82
            }
83

    
84
            if (resData.subjects != null) {
85
                if (result['subjects'] === undefined) {
86
                    result['subjects'] = new Array<string>();
87
                }
88

    
89
                const subjects = resData.subjects;
90
                const lengthSubjects = Array.isArray(subjects) ? subjects.length : 1;
91

    
92
                for (let j = 0; j < lengthSubjects; j++) {
93
                    const subject = Array.isArray(subjects) ? subjects[i] : subjects;
94
                    result.subjects[j] = subject;
95
                }
96
            }
97
            if (result['type'] === 'community' || result['type'] === 'ri') {
98
                communities.push(result);
99
            }
100
        }
101
        return communities;
102
    }
103
}
    (1-1/1)