1
|
import {Component, Input, OnInit, ViewChild} from '@angular/core';
|
2
|
import {FormBuilder, FormControl} from '@angular/forms';
|
3
|
import {ActivatedRoute, Router} from '@angular/router';
|
4
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
5
|
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
|
6
|
import {SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
|
7
|
import {ZenodoCommunitiesService} from '../../openaireLibrary/connect/zenodoCommunities/zenodo-communities.service';
|
8
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
9
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
10
|
import {ManageZenodoCommunitiesService} from '../../services/manageZenodoCommunities.service';
|
11
|
import {SearchInputComponent} from '../../openaireLibrary/sharedComponents/search-input/search-input.component';
|
12
|
import {Subject, Subscription} from 'rxjs';
|
13
|
import {debounceTime, distinctUntilChanged} from 'rxjs/operators';
|
14
|
|
15
|
declare var UIkit;
|
16
|
|
17
|
@Component({
|
18
|
selector: 'add-zenodo-communities',
|
19
|
templateUrl: './add-zenodo-communities.component.html',
|
20
|
})
|
21
|
export class AddZenodoCommunitiesComponent implements OnInit {
|
22
|
|
23
|
public zenodoCommunities = null; // zenodo search API results
|
24
|
public totalZenodoCommunities = null;
|
25
|
@Input() properties: EnvProperties = null;
|
26
|
@Input() communityId = null;
|
27
|
@ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
|
28
|
public filterForm: FormControl;
|
29
|
private subscriptions: any[] = [];
|
30
|
public subResults: any;
|
31
|
public zenodoCommunitySearchUtils: SearchUtilsClass = new SearchUtilsClass();
|
32
|
public searchTermStream = new Subject<string>();
|
33
|
errorCodes: ErrorCodes;
|
34
|
public rowsOnPage = 10;
|
35
|
@Input() masterCommunity = null;
|
36
|
@Input() selectedCommunities = [];
|
37
|
|
38
|
constructor(private route: ActivatedRoute,
|
39
|
private _router: Router,
|
40
|
public _fb: FormBuilder,
|
41
|
private _zenodoCommunitieService: ZenodoCommunitiesService,
|
42
|
private _manageZenodoCommunitiesService: ManageZenodoCommunitiesService) {
|
43
|
|
44
|
this.errorCodes = new ErrorCodes();
|
45
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.LOADING;
|
46
|
}
|
47
|
|
48
|
ngOnInit() {
|
49
|
this.filterForm = this._fb.control('');
|
50
|
this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
|
51
|
this.searchTermStream.next(value);
|
52
|
}));
|
53
|
this.subscriptions.push(this.searchTermStream
|
54
|
.pipe(debounceTime(1000), distinctUntilChanged())
|
55
|
.subscribe((term: string) => {
|
56
|
this.zenodoCommunitySearchUtils.keyword = term;
|
57
|
this.goTo(1);
|
58
|
}));
|
59
|
|
60
|
this.zenodoCommunitySearchUtils.keyword = "";
|
61
|
if (!Session.isLoggedIn()) {
|
62
|
this._router.navigate(['/user-info'], {
|
63
|
queryParams: {
|
64
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
65
|
"redirectUrl": this._router.url
|
66
|
}
|
67
|
});
|
68
|
} else {
|
69
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.LOADING;
|
70
|
if (this.communityId != null && this.communityId != '') {
|
71
|
this._zenodoCommunitieService.getZenodoCommunities(this.properties, this.properties.zenodoCommunities + "?page=" + this.zenodoCommunitySearchUtils.page + "&size=" + this.rowsOnPage).subscribe(
|
72
|
result => {
|
73
|
this.zenodoCommunities = result[0];
|
74
|
this.totalZenodoCommunities = result[1];
|
75
|
this.zenodoCommunitySearchUtils.totalResults = result[1];
|
76
|
this.zenodoCommunitySearchUtils.page = 1;
|
77
|
this.zenodoCommunitySearchUtils.size = this.rowsOnPage;
|
78
|
if (this.totalZenodoCommunities == 0) {
|
79
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.NONE;
|
80
|
} else {
|
81
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.DONE;
|
82
|
}
|
83
|
|
84
|
},
|
85
|
error => {
|
86
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.ERROR;
|
87
|
}
|
88
|
);
|
89
|
}
|
90
|
}
|
91
|
|
92
|
}
|
93
|
|
94
|
ngOnDestroy() {
|
95
|
this.subscriptions.forEach(subscription => {
|
96
|
if (subscription instanceof Subscription) {
|
97
|
subscription.unsubscribe();
|
98
|
}
|
99
|
});
|
100
|
if(this.subResults){
|
101
|
this.subResults.unsubscribe();
|
102
|
}
|
103
|
}
|
104
|
|
105
|
public goTo(page: number = 1) {
|
106
|
if (!Session.isLoggedIn()) {
|
107
|
this._router.navigate(['/user-info'], {
|
108
|
queryParams: {
|
109
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
110
|
"redirectUrl": this._router.url
|
111
|
}
|
112
|
});
|
113
|
} else {
|
114
|
this.zenodoCommunitySearchUtils.page = page;
|
115
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.LOADING;
|
116
|
if(this.subResults){
|
117
|
this.subResults.unsubscribe();
|
118
|
}
|
119
|
this.subResults = this._zenodoCommunitieService.getZenodoCommunities(this.properties,
|
120
|
this.properties.zenodoCommunities + "?page=" + this.zenodoCommunitySearchUtils.page
|
121
|
+ "&size=" + this.rowsOnPage
|
122
|
+ ((this.zenodoCommunitySearchUtils.keyword) ? ("&q=" + this.zenodoCommunitySearchUtils.keyword) : "")
|
123
|
).subscribe(
|
124
|
result => {
|
125
|
this.zenodoCommunities = result[0];
|
126
|
this.totalZenodoCommunities = result[1];
|
127
|
this.zenodoCommunitySearchUtils.totalResults = result[1];
|
128
|
this.zenodoCommunitySearchUtils.size = this.rowsOnPage;
|
129
|
if (this.totalZenodoCommunities == 0) {
|
130
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.NONE;
|
131
|
} else {
|
132
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.DONE;
|
133
|
}
|
134
|
|
135
|
},
|
136
|
error => {
|
137
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.ERROR;
|
138
|
}
|
139
|
);
|
140
|
}
|
141
|
}
|
142
|
|
143
|
totalPages(): number {
|
144
|
let totalPages: any = this.zenodoCommunitySearchUtils.totalResults / (this.rowsOnPage);
|
145
|
if (!(Number.isInteger(totalPages))) {
|
146
|
totalPages = (parseInt(totalPages, 10) + 1);
|
147
|
}
|
148
|
return totalPages;
|
149
|
}
|
150
|
|
151
|
|
152
|
public addCommunity(community) {
|
153
|
if (!Session.isLoggedIn()) {
|
154
|
this._router.navigate(['/user-info'], {
|
155
|
queryParams: {
|
156
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
157
|
"redirectUrl": this._router.url
|
158
|
}
|
159
|
});
|
160
|
} else {
|
161
|
this.subscriptions.push(this._manageZenodoCommunitiesService.addZCommunity(this.properties, this.communityId, community.id).subscribe(
|
162
|
data => {
|
163
|
community["openaireId"] = data.id;
|
164
|
this.selectedCommunities.push(community);
|
165
|
UIkit.notification('Community successfully added!', {
|
166
|
status: 'success',
|
167
|
timeout: 6000,
|
168
|
pos: 'bottom-right'
|
169
|
});
|
170
|
},
|
171
|
err => {
|
172
|
this.handleError('An error has been occurred. Try again later!');
|
173
|
console.log(err.status);
|
174
|
}/*,
|
175
|
() => {
|
176
|
console.info("completed ADD");
|
177
|
}*/
|
178
|
));
|
179
|
}
|
180
|
}
|
181
|
|
182
|
public removeCommunity(comm) {
|
183
|
if (!Session.isLoggedIn()) {
|
184
|
this._router.navigate(['/user-info'], {
|
185
|
queryParams: {
|
186
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
187
|
"redirectUrl": this._router.url
|
188
|
}
|
189
|
});
|
190
|
} else {
|
191
|
var openaireId = this.getOpenaireId(comm);
|
192
|
this.subscriptions.push(this._manageZenodoCommunitiesService.removeZCommunity(this.properties, this.communityId, openaireId,).subscribe(
|
193
|
data => {
|
194
|
var pos = -1;
|
195
|
for (var i = 0; i < this.selectedCommunities.length; i++) {
|
196
|
if (this.selectedCommunities[i].id == comm.id) {
|
197
|
pos = i;
|
198
|
break;
|
199
|
}
|
200
|
}
|
201
|
if (pos != -1) {
|
202
|
this.selectedCommunities.splice(pos, 1);
|
203
|
}
|
204
|
|
205
|
UIkit.notification('Community successfully removed!', {
|
206
|
status: 'success',
|
207
|
timeout: 6000,
|
208
|
pos: 'bottom-right'
|
209
|
});
|
210
|
},
|
211
|
err => {
|
212
|
this.handleError('An error has been occurred. Try again later!');
|
213
|
console.log(err.status);
|
214
|
}/*,
|
215
|
() => {
|
216
|
console.info("completed remove");
|
217
|
}*/
|
218
|
));
|
219
|
}
|
220
|
}
|
221
|
|
222
|
public inThelist(community: any, list): any {
|
223
|
for (let com of list) {
|
224
|
if (com.id == community.id) {
|
225
|
return true;
|
226
|
}
|
227
|
}
|
228
|
return false;
|
229
|
}
|
230
|
|
231
|
public getOpenaireId(community: any): string {
|
232
|
for (let com of this.selectedCommunities) {
|
233
|
if (com.id == community.id) {
|
234
|
return com.openaireId;
|
235
|
}
|
236
|
}
|
237
|
return null;
|
238
|
}
|
239
|
|
240
|
public onSearchClose() {
|
241
|
this.zenodoCommunitySearchUtils.keyword = this.filterForm.value;
|
242
|
}
|
243
|
|
244
|
public resetInput() {
|
245
|
this.zenodoCommunitySearchUtils.keyword = null;
|
246
|
this.searchInputComponent.reset()
|
247
|
}
|
248
|
|
249
|
handleError(message: string) {
|
250
|
UIkit.notification(message, {
|
251
|
status: 'danger',
|
252
|
timeout: 6000,
|
253
|
pos: 'bottom-right'
|
254
|
});
|
255
|
}
|
256
|
}
|