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