1
|
import {Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
|
2
|
import {ActivatedRoute, Router} from '@angular/router';
|
3
|
|
4
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
5
|
|
6
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
7
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
8
|
import {Affiliation} from '../../openaireLibrary/utils/entities/CuratorInfo';
|
9
|
import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class';
|
10
|
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
|
11
|
import {UtilitiesService} from '../../openaireLibrary/services/utilities.service';
|
12
|
import {AffiliationService} from "../../openaireLibrary/connect/affiliations/affiliation.service";
|
13
|
import {HelpContentService} from "../../services/help-content.service";
|
14
|
import {Title} from '@angular/platform-browser';
|
15
|
import {StringUtils} from "../../openaireLibrary/utils/string-utils.class";
|
16
|
|
17
|
@Component({
|
18
|
selector: 'affiliations',
|
19
|
templateUrl: './affiliations.component.html',
|
20
|
})
|
21
|
|
22
|
export class AffiliationsComponent implements OnInit {
|
23
|
|
24
|
@ViewChild('affiliationModal') affiliationModal: AlertModal;
|
25
|
@ViewChild('removeAffiliationModal') removeAffiliationModal: AlertModal;
|
26
|
public showLoading = false;
|
27
|
public message = '';
|
28
|
public messageType = '';
|
29
|
|
30
|
public affiliation: Affiliation = new Affiliation();
|
31
|
public properties: EnvProperties = null;
|
32
|
|
33
|
private index = 0;
|
34
|
private maxCharacters = 70;
|
35
|
|
36
|
@Input() hasChanged: boolean = false;
|
37
|
@Input() curatorAffiliations: boolean = false;
|
38
|
@Input() public affiliations: Affiliation[] = [];
|
39
|
@Output() affiliationsChange: EventEmitter<boolean> = new EventEmitter();
|
40
|
@Output() resetCuratorMessages: EventEmitter<boolean> = new EventEmitter();
|
41
|
public communityId: string;
|
42
|
public organizationsPageId: string;
|
43
|
public organizationsEnabled = false;
|
44
|
|
45
|
constructor(private element: ElementRef,
|
46
|
private route: ActivatedRoute,
|
47
|
private _router: Router,
|
48
|
private title: Title,
|
49
|
private affiliationService: AffiliationService,
|
50
|
private _helpContentService: HelpContentService) {
|
51
|
}
|
52
|
|
53
|
|
54
|
ngOnInit() {
|
55
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
56
|
this.properties = data.envSpecific;
|
57
|
if(!this.curatorAffiliations) {
|
58
|
this.title.setTitle('Administration Dashboard | Related Organizations');
|
59
|
}
|
60
|
if (!Session.isLoggedIn()) {
|
61
|
this._router.navigate(['/user-info'], {
|
62
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
63
|
});
|
64
|
} else {
|
65
|
this.showLoading = true;
|
66
|
this.message = '';
|
67
|
|
68
|
this.route.queryParams.subscribe(
|
69
|
communityId => {
|
70
|
this.communityId = communityId['communityId'];
|
71
|
|
72
|
if(!this.curatorAffiliations) {
|
73
|
this.getAffiliations();
|
74
|
}
|
75
|
this.organizationsPageStatus();
|
76
|
}
|
77
|
);
|
78
|
}
|
79
|
});
|
80
|
}
|
81
|
|
82
|
getAffiliations() {
|
83
|
this.affiliationService.initAffiliations(this.communityId);
|
84
|
this.affiliationService.affiliations.subscribe(
|
85
|
affiliations => {
|
86
|
this.affiliations = affiliations;
|
87
|
this.getOrganizationsPageId();
|
88
|
//this.showLoading = false;
|
89
|
},
|
90
|
error => {
|
91
|
console.error("Affiliations Component: Error getting affiliations for community with id: "+this.communityId, error);
|
92
|
this.showLoading = false;
|
93
|
}
|
94
|
);
|
95
|
}
|
96
|
|
97
|
getOrganizationsPageId() {
|
98
|
this._helpContentService.getCommunityPageByRoute("/organizations", this.properties.adminToolsAPIURL, this.communityId).subscribe(
|
99
|
pages => {
|
100
|
if(pages && pages[0]) {
|
101
|
this.organizationsPageId = pages[0]._id;
|
102
|
}
|
103
|
this.showLoading = false;
|
104
|
},
|
105
|
error => {
|
106
|
console.error("Affiliations Component: Error getting page with route '/organizations' for community with id: "+this.communityId, error);
|
107
|
this.showLoading = false;
|
108
|
}
|
109
|
);
|
110
|
}
|
111
|
|
112
|
initAffiliation(affiliation: Affiliation = null) {
|
113
|
this.resetMessages();
|
114
|
|
115
|
if (affiliation) {
|
116
|
this.affiliation = {...affiliation};
|
117
|
if(!this.curatorAffiliations) {
|
118
|
this.affiliation.communityId = affiliation.communityId;
|
119
|
this.affiliation.id = affiliation.id;
|
120
|
}
|
121
|
if(this.curatorAffiliations) {
|
122
|
this.affiliationModal.okButtonText = 'OK';
|
123
|
} else {
|
124
|
this.affiliationModal.okButtonText = 'Save Affiliation';
|
125
|
}
|
126
|
} else {
|
127
|
this.index = -1;
|
128
|
this.affiliation = new Affiliation();
|
129
|
if(!this.curatorAffiliations) {
|
130
|
this.affiliation.communityId = this.communityId;
|
131
|
}
|
132
|
}
|
133
|
this.affiliationModal.okButtonLeft = false;
|
134
|
if(this.curatorAffiliations) {
|
135
|
this.affiliationModal.okButtonText = 'OK';
|
136
|
} else {
|
137
|
this.affiliationModal.okButtonText = 'Save Affiliation';
|
138
|
}
|
139
|
this.affiliationModal.open();
|
140
|
}
|
141
|
|
142
|
public chooseAffiliation(index: number, action: string = 'delete') {
|
143
|
this.resetMessages();
|
144
|
this.index = index;
|
145
|
const affiliation: Affiliation = this.affiliations[index];
|
146
|
if (action === 'delete') {
|
147
|
this.removeAffiliationModal.message = 'Do you want to remove ' +
|
148
|
affiliation.name + ' from your Affiliations?';
|
149
|
this.removeAffiliationModal.okButtonText = 'Yes';
|
150
|
this.removeAffiliationModal.cancelButtonText = 'No';
|
151
|
this.removeAffiliationModal.open();
|
152
|
} else if (action === 'edit') {
|
153
|
this.initAffiliation(affiliation);
|
154
|
}
|
155
|
}
|
156
|
|
157
|
updateCommunityAffiliations(index: number) {
|
158
|
if (!Session.isLoggedIn()) {
|
159
|
this._router.navigate(['/user-info'], {
|
160
|
queryParams:
|
161
|
{'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
162
|
});
|
163
|
} else {
|
164
|
HelperFunctions.scroll();
|
165
|
|
166
|
this.showLoading = true;
|
167
|
|
168
|
this.affiliationService.updateAffiliation(this.properties.communityAPI + this.communityId + '/organizations',
|
169
|
this.affiliation).subscribe((affiliation) => {
|
170
|
if (affiliation) {
|
171
|
if (index === -1) {
|
172
|
this.affiliations.push(affiliation);
|
173
|
} else {
|
174
|
this.affiliations[index] = affiliation;
|
175
|
}
|
176
|
if(this.affiliation.id) {
|
177
|
this.handleUpdateSuccess('Your organization has been updated successfully!');
|
178
|
} else {
|
179
|
this.handleUpdateSuccess('Your organization has been saved successfully!');
|
180
|
}
|
181
|
}
|
182
|
},
|
183
|
error => {
|
184
|
if(this.affiliation.id) {
|
185
|
this.handleUpdateError('Your organization could not be updated. Try again later!', error);
|
186
|
} else {
|
187
|
this.handleUpdateError('Organization could not be saved. Try again later!', error);
|
188
|
}
|
189
|
});
|
190
|
}
|
191
|
}
|
192
|
|
193
|
addAffiliation() {
|
194
|
if (!this.isEmptyAffiliation()) {
|
195
|
if (!this.curatorAffiliations) {
|
196
|
this.updateCommunityAffiliations(this.index);
|
197
|
} else {
|
198
|
if (this.index === -1) {
|
199
|
this.affiliations.push(this.affiliation);
|
200
|
} else {
|
201
|
this.affiliations[this.index] = this.affiliation;
|
202
|
}
|
203
|
}
|
204
|
this.change();
|
205
|
}
|
206
|
}
|
207
|
|
208
|
removeAffiliation() {
|
209
|
if(!this.curatorAffiliations) {
|
210
|
HelperFunctions.scroll();
|
211
|
|
212
|
this.showLoading = true;
|
213
|
this.affiliationService.deleteAffiliation(this.properties.communityAPI + this.communityId + '/organizations',
|
214
|
this.affiliations[this.index].id).subscribe((deleteOK) => {
|
215
|
this.affiliations.splice(this.index, 1);
|
216
|
this.handleUpdateSuccess('Organization has been deleted');
|
217
|
},
|
218
|
error => {
|
219
|
this.handleUpdateError('Organization could not be deleted. Try again later!', error);
|
220
|
}
|
221
|
);
|
222
|
} else {
|
223
|
this.affiliations.splice(this.index, 1);
|
224
|
this.change();
|
225
|
}
|
226
|
}
|
227
|
|
228
|
private organizationsPageStatus() {
|
229
|
this._helpContentService.getCommunityFull(this.communityId, this.properties.adminToolsAPIURL).subscribe((community) => {
|
230
|
for(let page of community.pages) {
|
231
|
if(page['route'] === '/organizations') {
|
232
|
this.organizationsEnabled = page['isEnabled'];
|
233
|
return;
|
234
|
}
|
235
|
}
|
236
|
this.organizationsEnabled = false;
|
237
|
})
|
238
|
}
|
239
|
|
240
|
private change() {
|
241
|
this.hasChanged = true;
|
242
|
if(this.curatorAffiliations) {
|
243
|
this.affiliationsChange.emit(this.hasChanged);
|
244
|
}
|
245
|
}
|
246
|
|
247
|
private resetMessages() {
|
248
|
this.message = '';
|
249
|
if(this.curatorAffiliations) {
|
250
|
this.resetCuratorMessages.emit(true);
|
251
|
}
|
252
|
}
|
253
|
|
254
|
handleUpdateError(message: string, error) {
|
255
|
this.resetMessages();
|
256
|
this.message = message;
|
257
|
this.messageType = "warning";
|
258
|
console.log('Server responded: ', error);
|
259
|
|
260
|
this.showLoading = false;
|
261
|
}
|
262
|
|
263
|
handleUpdateSuccess(message) {
|
264
|
this.resetMessages();
|
265
|
this.message = message;
|
266
|
this.messageType = "success";
|
267
|
this.showLoading = false;
|
268
|
}
|
269
|
|
270
|
isEmptyAffiliation(): boolean {
|
271
|
return ((!this.affiliation.name || this.affiliation.name === '') ||
|
272
|
(!this.affiliation.logo_url || this.affiliation.logo_url === '') ||
|
273
|
(!this.affiliation.website_url || this.affiliation.website_url === ''));
|
274
|
}
|
275
|
|
276
|
_format(name: string){
|
277
|
if(name) {
|
278
|
return (((name).length > this.maxCharacters) ? (name.substring(0, (this.maxCharacters - ('...').length)) + '...') : name);
|
279
|
} else {
|
280
|
return null;
|
281
|
}
|
282
|
}
|
283
|
|
284
|
public urlPrefix(url: string): string {
|
285
|
return StringUtils.urlPrefix(url);
|
286
|
}
|
287
|
|
288
|
}
|