1
|
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
|
2
|
import {FormArray, FormBuilder, FormControl} from '@angular/forms';
|
3
|
import {ActivatedRoute, Router} from '@angular/router';
|
4
|
import {CommunityService} from '../../../openaireLibrary/connect/community/community.service';
|
5
|
import {SubjectsService} from '../subjects.service';
|
6
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
7
|
import {Session} from '../../../openaireLibrary/login/utils/helper.class';
|
8
|
import {LoginErrorCodes} from '../../../openaireLibrary/login/utils/guardHelper.class';
|
9
|
import {Title} from '@angular/platform-browser';
|
10
|
import {properties} from '../../../../environments/environment';
|
11
|
import {AlertModal} from '../../../openaireLibrary/utils/modal/alert';
|
12
|
import {SearchInputComponent} from '../../../openaireLibrary/sharedComponents/search-input/search-input.component';
|
13
|
import {Subscription, zip} from 'rxjs';
|
14
|
import {CommunityInfo} from '../../../openaireLibrary/connect/community/communityInfo';
|
15
|
import {concat} from 'rxjs/operators';
|
16
|
|
17
|
declare var UIkit;
|
18
|
|
19
|
@Component({
|
20
|
selector: 'subjects-edit-form',
|
21
|
templateUrl: './subjects-edit-form.component.html',
|
22
|
})
|
23
|
|
24
|
export class SubjectsEditFormComponent implements OnInit {
|
25
|
myForm = new FormArray([new FormControl({subject: ''})]);
|
26
|
public showLoading = true;
|
27
|
public res = [];
|
28
|
params: any;
|
29
|
public communityId: string = null;
|
30
|
public community: CommunityInfo = null;
|
31
|
public properties: EnvProperties = properties;
|
32
|
public edit = null;
|
33
|
public originalSubjects = [];
|
34
|
@ViewChild('editModal') editModal: AlertModal;
|
35
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
36
|
page = 1;
|
37
|
size = 10;
|
38
|
selectedSubjects = [];
|
39
|
isEditModal: boolean = false;
|
40
|
public selectedKeyword: string;
|
41
|
@ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
|
42
|
public filterForm: FormControl;
|
43
|
private subscriptions: any[] = [];
|
44
|
/*//Check again functionality to enable page
|
45
|
subjectsPage;
|
46
|
@ViewChild('enablePageModal') enablePageModal: AlertModal;*/
|
47
|
constructor(private element: ElementRef,
|
48
|
private route: ActivatedRoute,
|
49
|
private _router: Router,
|
50
|
public _fb: FormBuilder,
|
51
|
private title: Title,
|
52
|
// private _helpContentService: HelpContentService,
|
53
|
private _communityService: CommunityService,
|
54
|
private _subjectsService: SubjectsService,
|
55
|
) {
|
56
|
}
|
57
|
|
58
|
|
59
|
ngOnInit() {
|
60
|
this.filterForm = this._fb.control('');
|
61
|
this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
|
62
|
this.page = 1;
|
63
|
this.community.subjects = this.originalSubjects.filter(subject => {
|
64
|
return subject.toLowerCase().indexOf(value.toLowerCase()) != -1
|
65
|
});
|
66
|
}));
|
67
|
this.subscriptions.push(this.route.params.subscribe(
|
68
|
params => {
|
69
|
this.communityId = params['community'];
|
70
|
this.title.setTitle(this.communityId.toUpperCase() + ' | Subjects');
|
71
|
this.showLoading = true;
|
72
|
this.subscriptions.push(this._communityService.getCommunityAsObservable().subscribe(
|
73
|
community => {
|
74
|
this.community = community;
|
75
|
this.params = {
|
76
|
community: encodeURIComponent(
|
77
|
'"' + community.queryId + '"')
|
78
|
};
|
79
|
this.community.subjects.sort((n1,n2)=> {
|
80
|
if (n1.toLowerCase() > n2.toLowerCase()) {
|
81
|
return 1;
|
82
|
}
|
83
|
if (n1.toLowerCase() < n2.toLowerCase()) {
|
84
|
return -1;
|
85
|
}
|
86
|
return 0;
|
87
|
});
|
88
|
|
89
|
this.originalSubjects = [];
|
90
|
for (let i = 0; i < this.community.subjects.length; i++) {
|
91
|
this.originalSubjects.push(this.community.subjects[i]);
|
92
|
}
|
93
|
if (this.community.subjects.length === 0) {
|
94
|
this.community.subjects.push('');
|
95
|
}
|
96
|
this.showLoading = false;
|
97
|
},
|
98
|
error => this.handleUpdateError('System error retrieving community profile', error)
|
99
|
));
|
100
|
// this.getSubjectsPageStatus();
|
101
|
}));
|
102
|
|
103
|
}
|
104
|
|
105
|
ngOnDestroy() {
|
106
|
this.subscriptions.forEach(subscription => {
|
107
|
if (subscription instanceof Subscription) {
|
108
|
subscription.unsubscribe();
|
109
|
}
|
110
|
});
|
111
|
}
|
112
|
|
113
|
private removeModalOpen(subject: string, i: any) {
|
114
|
if (!Session.isLoggedIn()) {
|
115
|
this._router.navigate(['/user-info'], {
|
116
|
queryParams: {
|
117
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
118
|
"redirectUrl": this._router.url
|
119
|
}
|
120
|
});
|
121
|
} else {
|
122
|
this.selectedSubjects = [subject];
|
123
|
this.reset(['']);
|
124
|
this.deleteModal.cancelButton = true;
|
125
|
this.deleteModal.okButton = true;
|
126
|
this.deleteModal.alertTitle = 'Delete Confirmation';
|
127
|
this.deleteModal.message = 'Are you sure you want to delete this subject?';
|
128
|
this.deleteModal.okButtonText = 'Yes';
|
129
|
this.deleteModal.cancelButtonText = 'No';
|
130
|
this.deleteModal.open();
|
131
|
}
|
132
|
}
|
133
|
|
134
|
|
135
|
public getSubjectsExistOnlyInFirst(firstArray: string[], secondArray: string[]): string[] {
|
136
|
const difference = [];
|
137
|
for (let i = 0; i < firstArray.length; i++) {
|
138
|
if (secondArray.indexOf(firstArray[i]) === -1) {
|
139
|
difference.push(firstArray[i]);
|
140
|
}
|
141
|
|
142
|
}
|
143
|
return difference;
|
144
|
}
|
145
|
|
146
|
public saveSubjects() {
|
147
|
if (!Session.isLoggedIn()) {
|
148
|
this._router.navigate(['/user-info'],
|
149
|
{queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
150
|
} else {
|
151
|
if (this.communityId != null && this.communityId !== '') {
|
152
|
this.showLoading = true;
|
153
|
const subjectsToDeleteAr = this.getSubjectsExistOnlyInFirst(this.selectedSubjects, this.myForm.getRawValue());
|
154
|
const subjectsToAddAr = this.getSubjectsExistOnlyInFirst(this.myForm.getRawValue(), this.selectedSubjects);
|
155
|
const subjectsToDelete = this.getNonEmptyItems(subjectsToDeleteAr);
|
156
|
const subjectsToAdd = this.getNonEmptyItems(subjectsToAddAr);
|
157
|
if (subjectsToAdd.length > 0 && subjectsToDelete.length > 0) {
|
158
|
this.subscriptions.push(this._subjectsService.removeSubjects(
|
159
|
this.properties.communityAPI + this.communityId + '/subjects', subjectsToDelete).subscribe(res =>{
|
160
|
this.subscriptions.push(this._subjectsService.addSubjects(
|
161
|
this.properties.communityAPI + this.communityId + '/subjects', subjectsToAdd).subscribe(res => {
|
162
|
this.afterUpdateActions(res, "added");
|
163
|
}));
|
164
|
}));
|
165
|
} else if (subjectsToAdd.length > 0) {
|
166
|
this.subscriptions.push(this._subjectsService.addSubjects(
|
167
|
this.properties.communityAPI + this.communityId + '/subjects', subjectsToAdd).subscribe(res => {
|
168
|
this.afterUpdateActions(res, "added");
|
169
|
},
|
170
|
error => this.handleUpdateError('System error updating subjects', error)
|
171
|
));
|
172
|
} else if (subjectsToDelete.length > 0) {
|
173
|
this.subscriptions.push(this._subjectsService.removeSubjects(
|
174
|
this.properties.communityAPI + this.communityId + '/subjects', subjectsToDelete).subscribe(res => {
|
175
|
this.afterUpdateActions(res, "deleted");
|
176
|
},
|
177
|
error => this.handleUpdateError('System error updating subjects', error)
|
178
|
));
|
179
|
}
|
180
|
/* if(!this.subjectsEnabled) {
|
181
|
this.curatorsEnabledOpen();
|
182
|
}*/
|
183
|
}
|
184
|
}
|
185
|
}
|
186
|
|
187
|
handleUpdateError(message: string, error) {
|
188
|
UIkit.notification(message, {
|
189
|
status: 'danger',
|
190
|
timeout: 6000,
|
191
|
pos: 'bottom-right'
|
192
|
});
|
193
|
this.showLoading = false;
|
194
|
}
|
195
|
|
196
|
afterUpdateActions(res, message: string) {
|
197
|
this._communityService.setCommunity(this._communityService.parseCommunity(res));
|
198
|
this.community.subjects = res['subjects'];
|
199
|
this.originalSubjects = [];
|
200
|
for (let i = 0; i < this.community.subjects.length; i++) {
|
201
|
this.originalSubjects.push(this.community.subjects[i]);
|
202
|
}
|
203
|
this.originalSubjects.sort((n1,n2)=> {
|
204
|
if (n1.toLowerCase() > n2.toLowerCase()) {
|
205
|
return 1;
|
206
|
}
|
207
|
if (n1.toLowerCase() < n2.toLowerCase()) {
|
208
|
return -1;
|
209
|
}
|
210
|
return 0;
|
211
|
});
|
212
|
this.page = 1;
|
213
|
this.community.subjects = this.originalSubjects.filter(subject => {
|
214
|
return subject.toLowerCase().indexOf(this.filterForm.value.toLowerCase()) != -1
|
215
|
}).sort();
|
216
|
UIkit.notification('Subjects successfully ' + message + '!', {
|
217
|
status: 'success',
|
218
|
timeout: 6000,
|
219
|
pos: 'bottom-right'
|
220
|
});
|
221
|
this.showLoading = false;
|
222
|
}
|
223
|
|
224
|
|
225
|
private getNonEmptyItems(data: string[]): string[] {
|
226
|
const length = data.length;
|
227
|
const arrayNonEmpty = new Array<string>();
|
228
|
let j = 0;
|
229
|
for (let i = 0; i < length; i++) {
|
230
|
if (this.isEmpty(data[i])) {
|
231
|
} else if (this.isNonEmpty(data[i])) {
|
232
|
arrayNonEmpty[j] = data[i];
|
233
|
j++;
|
234
|
}
|
235
|
}
|
236
|
return arrayNonEmpty;
|
237
|
}
|
238
|
|
239
|
|
240
|
private isEmpty(data: string): boolean {
|
241
|
if (data !== undefined && !data.replace(/\s/g, '').length) {
|
242
|
return true;
|
243
|
} else {
|
244
|
return false;
|
245
|
}
|
246
|
}
|
247
|
|
248
|
private isNonEmpty(data: string): boolean {
|
249
|
if (data !== undefined && data != null) {
|
250
|
return true;
|
251
|
} else {
|
252
|
return false;
|
253
|
}
|
254
|
}
|
255
|
|
256
|
public addSubjectInForm() {
|
257
|
this.myForm.push(new FormControl(''));
|
258
|
}
|
259
|
|
260
|
public removeSubjectInForm(i: number) {
|
261
|
this.myForm.removeAt(i);
|
262
|
}
|
263
|
|
264
|
public reset(subjects: string[]) {
|
265
|
this.myForm = new FormArray([]);
|
266
|
for (let subject in subjects) {
|
267
|
this.myForm.push(new FormControl(''));
|
268
|
}
|
269
|
this.myForm.patchValue(subjects);
|
270
|
}
|
271
|
|
272
|
newSubject() {
|
273
|
this.isEditModal = false;
|
274
|
this.selectedSubjects = [];
|
275
|
this.reset([""]);
|
276
|
this.formModalOpen('Add Subject', 'Save');
|
277
|
}
|
278
|
|
279
|
public editSubject(subject: string) {
|
280
|
this.isEditModal = true;
|
281
|
this.selectedSubjects = [subject];
|
282
|
this.reset([subject]);
|
283
|
this.formModalOpen('Edit Subject', 'Save');
|
284
|
}
|
285
|
|
286
|
|
287
|
private formModalOpen(title: string, yesBtn: string) {
|
288
|
if (!Session.isLoggedIn()) {
|
289
|
this._router.navigate(['/user-info'], {
|
290
|
queryParams: {
|
291
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
292
|
"redirectUrl": this._router.url
|
293
|
}
|
294
|
});
|
295
|
} else {
|
296
|
this.editModal.okButtonLeft = false;
|
297
|
this.editModal.cancelButton = true;
|
298
|
this.editModal.okButton = true;
|
299
|
this.editModal.alertTitle = title;
|
300
|
this.editModal.okButtonText = yesBtn;
|
301
|
this.editModal.open();
|
302
|
}
|
303
|
}
|
304
|
|
305
|
public onSearchClose() {
|
306
|
this.selectedKeyword = this.filterForm.value;
|
307
|
}
|
308
|
|
309
|
public resetInput() {
|
310
|
this.selectedKeyword = null;
|
311
|
this.searchInputComponent.reset()
|
312
|
}
|
313
|
/*
|
314
|
//Check again functionality to enable page
|
315
|
private getSubjectsPageStatus() {
|
316
|
this._helpContentService.getCommunityPagesByRoute(this.communityId, '/subjects', this.properties.adminToolsAPIURL).subscribe((page) => {
|
317
|
this.subjectsPage = page;
|
318
|
});
|
319
|
}
|
320
|
|
321
|
public get subjectsEnabled(): boolean {
|
322
|
return !this.subjectsPage || this.subjectsPage.isEnabled;
|
323
|
}
|
324
|
enablePage() {
|
325
|
this._helpContentService.togglePages(this.communityId, [this.subjectsPage._id], true, this.properties.adminToolsAPIURL).subscribe(() => {
|
326
|
this.subjectsPage.isEnabled = true;
|
327
|
UIkit.notification('Curators Page has been <b>enabled successfully</b>', {
|
328
|
status: 'success',
|
329
|
timeout: 6000,
|
330
|
pos: 'bottom-right'
|
331
|
});
|
332
|
},error => {
|
333
|
this.subjectsPage.isEnabled = false;
|
334
|
UIkit.notification('An error occured', {
|
335
|
status: 'danger',
|
336
|
timeout: 6000,
|
337
|
pos: 'bottom-right'
|
338
|
});
|
339
|
});
|
340
|
}
|
341
|
|
342
|
private curatorsEnabledOpen() {
|
343
|
this.enablePageModal.okButtonLeft = false;
|
344
|
this.enablePageModal.alertTitle = 'Enable Subjects Page';
|
345
|
this.enablePageModal.okButtonText = 'Yes';
|
346
|
this.enablePageModal.cancelButtonText = 'No';
|
347
|
this.enablePageModal.open();
|
348
|
}*/
|
349
|
|
350
|
}
|