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
|
this.getCommunities();
|
82
|
//this.getDivContents();
|
83
|
//this.getPageContents();
|
84
|
});
|
85
|
});
|
86
|
}
|
87
|
|
88
|
private getPageContents() {
|
89
|
this.helper.getPageHelpContents(this._router.url, this.properties, 'connect').subscribe(contents => {
|
90
|
this.pageContents = contents;
|
91
|
})
|
92
|
}
|
93
|
|
94
|
private getDivContents() {
|
95
|
this.helper.getDivHelpContents(this._router.url, this.properties, 'connect').subscribe(contents => {
|
96
|
this.divContents = contents;
|
97
|
})
|
98
|
}
|
99
|
|
100
|
public getCommunities() {
|
101
|
this.loading = true;
|
102
|
this.status = this.errorCodes.LOADING;
|
103
|
this.subscriberErrorMessage = "";
|
104
|
|
105
|
this.subscriberOfCommunities = [];
|
106
|
this.managerOfCommunities = [];
|
107
|
this.researchCommunities = [];
|
108
|
|
109
|
this._communitiesService.getCommunitiesState().subscribe(
|
110
|
communitiesResults => {
|
111
|
if(!communitiesResults){
|
112
|
return;
|
113
|
}
|
114
|
if(communitiesResults.length == 0) {
|
115
|
this.status = this.errorCodes.DONE;
|
116
|
return;
|
117
|
};
|
118
|
this.sort(communitiesResults);
|
119
|
|
120
|
var subscribedLoading = communitiesResults.length;
|
121
|
var mail = this.user.email;
|
122
|
communitiesResults.forEach((community, index) => {
|
123
|
let showCommunity: boolean = true;
|
124
|
let isManager: boolean = false;
|
125
|
let isSubscriber: boolean = false;
|
126
|
|
127
|
if (community['status'] == "hidden") {
|
128
|
showCommunity = false;
|
129
|
} else {
|
130
|
if (mail == null && community['status'] == "manager") { // no user
|
131
|
showCommunity = false;
|
132
|
} else if (Session.isCommunityCurator(this.user) || Session.isPortalAdministrator(this.user)) {
|
133
|
isManager = true;
|
134
|
} else if (community.managers.indexOf(mail) != -1) {
|
135
|
isManager = true;
|
136
|
} else if (community['status'] == "manager") {
|
137
|
showCommunity = false;
|
138
|
}
|
139
|
}
|
140
|
|
141
|
if (showCommunity) {
|
142
|
this.researchCommunities.push(community);
|
143
|
if (isManager) {
|
144
|
community.isManager = true;
|
145
|
this.managerOfCommunities.push(community);
|
146
|
}
|
147
|
}
|
148
|
|
149
|
this.status = this.errorCodes.DONE;
|
150
|
|
151
|
if (mail != null && showCommunity) {
|
152
|
this._subscribeService.isSubscribedToCommunity(this.properties, community.communityId, mail).subscribe(
|
153
|
res => {
|
154
|
isSubscriber = res;
|
155
|
if (isSubscriber) {
|
156
|
community.isSubscribed = true;
|
157
|
if (isManager) {
|
158
|
this.subscriberOfCommunities.push(community);
|
159
|
} else {
|
160
|
this.subscriberOfCommunities.unshift(community);
|
161
|
}
|
162
|
}
|
163
|
|
164
|
subscribedLoading--;
|
165
|
if (subscribedLoading == 0) {
|
166
|
this.loading = false;
|
167
|
}
|
168
|
},
|
169
|
error => {
|
170
|
this.handleError("Error getting response if email: " + mail + " is subscribed to community with id: " + community.communityId, error);
|
171
|
subscribedLoading--;
|
172
|
if (subscribedLoading == 0) {
|
173
|
this.loading = false;
|
174
|
}
|
175
|
});
|
176
|
} else {
|
177
|
subscribedLoading--;
|
178
|
if (subscribedLoading == 0) {
|
179
|
this.loading = false;
|
180
|
}
|
181
|
}
|
182
|
});
|
183
|
},
|
184
|
error => {
|
185
|
this.status = this.handleError("Error getting communities", error);
|
186
|
this.loading = false;
|
187
|
}
|
188
|
);
|
189
|
}
|
190
|
|
191
|
private sort(results: CommunityInfo[]) {
|
192
|
results.sort((left, right): number => {
|
193
|
if (!right.date || left.date > right.date) {
|
194
|
return -1;
|
195
|
} else if (!left.date || left.date < right.date) {
|
196
|
return 1;
|
197
|
} else {
|
198
|
if (left.title > right.title) {
|
199
|
return 1;
|
200
|
} else if (left.title < right.title) {
|
201
|
return -1;
|
202
|
} else {
|
203
|
return 0;
|
204
|
}
|
205
|
}
|
206
|
})
|
207
|
}
|
208
|
|
209
|
public quote(param: string): string {
|
210
|
return StringUtils.quote(param);
|
211
|
}
|
212
|
|
213
|
public ngOnDestroy() {
|
214
|
if (this.piwiksub) {
|
215
|
this.piwiksub.unsubscribe();
|
216
|
}
|
217
|
}
|
218
|
|
219
|
private handleError(message: string, error): number {
|
220
|
var code = "";
|
221
|
if (!error.status) {
|
222
|
var error = error.json();
|
223
|
code = error.code;
|
224
|
} else {
|
225
|
code = error.status;
|
226
|
}
|
227
|
|
228
|
console.error("Communities (component): " + message, error);
|
229
|
|
230
|
return this.errorMessages.getErrorCode(code);
|
231
|
}
|
232
|
}
|