Project

General

Profile

1
import {Component} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {Meta, Title} from '@angular/platform-browser';
4
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
5
import {CommunitiesService} from '../openaireLibrary/connect/communities/communities.service';
6
import {SubscribeService} from '../openaireLibrary/utils/subscribe/subscribe.service';
7
import {CommunityInfo} from '../openaireLibrary/connect/community/communityInfo';
8

    
9
import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service';
10
import {Session, User} from '../openaireLibrary/login/utils/helper.class';
11
import {StringUtils} from '../openaireLibrary/utils/string-utils.class';
12

    
13
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
14
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
15
import {HelperService} from "../openaireLibrary/utils/helper/helper.service";
16
import {SEOService} from "../openaireLibrary/sharedComponents/SEO/SEO.service";
17
import {UserManagementService} from "../openaireLibrary/services/user-management.service";
18

    
19
@Component({
20
  selector: 'my-communities',
21
  templateUrl: 'my-communities.component.html',
22
})
23

    
24
export class MyCommunitiesComponent {
25
  public piwiksub: any;
26

    
27
  public pageTitle = "OpenAIRE"
28
  public subscriberOfCommunities = [];
29
  public managerOfCommunities = [];
30
  public researchCommunities = [];
31
  public pageContents = null;
32
  public divContents = null;
33
  // Message variables
34
  public status: number;
35
  public loading: boolean = true;
36
  public subscriberErrorMessage: string = "";
37
  public errorCodes: ErrorCodes;
38
  private errorMessages: ErrorMessagesComponent;
39

    
40
  properties: EnvProperties;
41
  private user: User;
42

    
43
  constructor(
44
    private route: ActivatedRoute,
45
    private _router: Router,
46
    private _meta: Meta,
47
    private _title: Title,
48
    private _piwikService: PiwikService,
49
    private _communitiesService: CommunitiesService,
50
    private _subscribeService: SubscribeService,
51
    private helper: HelperService,
52
    private seoService: SEOService,
53
    private userManagementService: UserManagementService) {
54

    
55
    var description = "OpenAIRE - Connect, Community Dashboard, research community " +
56
      "| My managing and subscribed to Communities";
57
    var title = "OpenAIRE - Connect | My Communities";
58

    
59
    this._meta.updateTag({content: description}, "name='description'");
60
    this._meta.updateTag({content: description}, "property='og:description'");
61
    this._meta.updateTag({content: title}, "property='og:title'");
62
    this._title.setTitle(title);
63

    
64
    this.errorCodes = new ErrorCodes();
65
    this.errorMessages = new ErrorMessagesComponent();
66
    this.status = this.errorCodes.LOADING;
67
  }
68

    
69
  public ngOnInit() {
70
    this.route.data
71
      .subscribe((data: { envSpecific: EnvProperties }) => {
72
        this.properties = data.envSpecific;
73
        var url = data.envSpecific.baseLink + this._router.url;
74
        this.seoService.createLinkForCanonicalURL(url, false);
75
        this._meta.updateTag({content: url}, "property='og:url'");
76
        if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
77
          this.piwiksub = this._piwikService.trackView(this.properties, "OpenAIRE Connect", this.properties.piwikSiteId).subscribe();
78
        }
79
        this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
80
          this.user = user;
81
          if(this.user) {
82
            this.getCommunities();
83
          }
84
          //this.getDivContents();
85
          //this.getPageContents();
86
        });
87
      });
88
  }
89

    
90
  private getPageContents() {
91
    this.helper.getPageHelpContents(this._router.url, this.properties, 'connect').subscribe(contents => {
92
      this.pageContents = contents;
93
    })
94
  }
95

    
96
  private getDivContents() {
97
    this.helper.getDivHelpContents(this._router.url, this.properties, 'connect').subscribe(contents => {
98
      this.divContents = contents;
99
    })
100
  }
101

    
102
  public getCommunities() {
103
    this.loading = true;
104
    this.status = this.errorCodes.LOADING;
105
    this.subscriberErrorMessage = "";
106

    
107
    this.subscriberOfCommunities = [];
108
    this.managerOfCommunities = [];
109
    this.researchCommunities = [];
110

    
111
    this._communitiesService.getCommunitiesState().subscribe(
112
      communitiesResults => {
113
        if (!communitiesResults) {
114
          return;
115
        }
116
        if (communitiesResults.length == 0) {
117
          this.status = this.errorCodes.DONE;
118
          return;
119
        }
120
        ;
121
        this.sort(communitiesResults);
122

    
123
        var subscribedLoading = communitiesResults.length;
124
        var mail = this.user.email;
125
        communitiesResults.forEach((community, index) => {
126
          let showCommunity: boolean = true;
127
          let isManager: boolean = false;
128
          let isSubscriber: boolean = false;
129

    
130
          if (community['status'] == "hidden") {
131
            showCommunity = false;
132
          } else {
133
            if (mail == null && community['status'] == "manager") { // no user
134
              showCommunity = false;
135
            } else if (Session.isCommunityCurator(this.user) || Session.isPortalAdministrator(this.user)) {
136
              isManager = true;
137
            } else if (community.managers.indexOf(mail) != -1) {
138
              isManager = true;
139
            } else if (community['status'] == "manager") {
140
              showCommunity = false;
141
            }
142
          }
143

    
144
          if (showCommunity) {
145
            this.researchCommunities.push(community);
146
            if (isManager) {
147
              community.isManager = true;
148
              this.managerOfCommunities.push(community);
149
            }
150
          }
151

    
152
          this.status = this.errorCodes.DONE;
153

    
154
          if (mail != null && showCommunity) {
155
            this._subscribeService.isSubscribedToCommunity(this.properties, community.communityId, mail).subscribe(
156
              res => {
157
                isSubscriber = res;
158
                if (isSubscriber) {
159
                  community.isSubscribed = true;
160
                  if (isManager) {
161
                    this.subscriberOfCommunities.push(community);
162
                  } else {
163
                    this.subscriberOfCommunities.unshift(community);
164
                  }
165
                }
166

    
167
                subscribedLoading--;
168
                if (subscribedLoading == 0) {
169
                  this.loading = false;
170
                }
171
              },
172
              error => {
173
                this.handleError("Error getting response if email: " + mail + " is subscribed to community with id: " + community.communityId, error);
174
                subscribedLoading--;
175
                if (subscribedLoading == 0) {
176
                  this.loading = false;
177
                }
178
              });
179
          } else {
180
            subscribedLoading--;
181
            if (subscribedLoading == 0) {
182
              this.loading = false;
183
            }
184
          }
185
        });
186
      },
187
      error => {
188
        this.status = this.handleError("Error getting communities", error);
189
        this.loading = false;
190
      }
191
    );
192
  }
193

    
194
  private sort(results: CommunityInfo[]) {
195
    results.sort((left, right): number => {
196
      if (!right.date || left.date > right.date) {
197
        return -1;
198
      } else if (!left.date || left.date < right.date) {
199
        return 1;
200
      } else {
201
        if (left.title > right.title) {
202
          return 1;
203
        } else if (left.title < right.title) {
204
          return -1;
205
        } else {
206
          return 0;
207
        }
208
      }
209
    })
210
  }
211

    
212
  public quote(param: string): string {
213
    return StringUtils.quote(param);
214
  }
215

    
216
  public ngOnDestroy() {
217
    if (this.piwiksub) {
218
      this.piwiksub.unsubscribe();
219
    }
220
  }
221

    
222
  private handleError(message: string, error): number {
223
    var code = "";
224
    if (!error.status) {
225
      var error = error.json();
226
      code = error.code;
227
    } else {
228
      code = error.status;
229
    }
230

    
231
    console.error("Communities (component): " + message, error);
232

    
233
    return this.errorMessages.getErrorCode(code);
234
  }
235
}
(3-3/4)