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
import {Breadcrumb} from "../openaireLibrary/utils/breadcrumbs/breadcrumbs.component";
19

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

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

    
28
  public pageTitle = "OpenAIRE"
29
  public subscriberOfCommunities = [];
30
  public managerOfCommunities = [];
31
  public researchCommunities = [];
32
  public pageContents = null;
33
  public divContents = null;
34
  // Message variables
35
  public status: number;
36
  public loading: boolean = true;
37
  public subscriberErrorMessage: string = "";
38
  public errorCodes: ErrorCodes;
39
  private errorMessages: ErrorMessagesComponent;
40
  public breadcrumbs: Breadcrumb[] = [{name: 'home', route: '/'}, {name: 'my Communities'}];
41

    
42
  properties: EnvProperties;
43
  private user: User;
44

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

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

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

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

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

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

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

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

    
109
    this.subscriberOfCommunities = [];
110
    this.managerOfCommunities = [];
111
    this.researchCommunities = [];
112

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

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

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

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

    
154
          this.status = this.errorCodes.DONE;
155

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

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

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

    
214
  public quote(param: string): string {
215
    return StringUtils.quote(param);
216
  }
217

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

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

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

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