1
|
/**
|
2
|
* Created by stefania on 7/13/17.
|
3
|
*/
|
4
|
import { Component, ViewChild, OnInit } from '@angular/core';
|
5
|
import { ActivatedRoute } from "@angular/router";
|
6
|
import { HelpContentService } from "../../services/help-content.service";
|
7
|
import { FormGroup } from "@angular/forms";
|
8
|
import { ModalFormComponent } from "../modal-form.component";
|
9
|
import { DeleteConfirmationDialogComponent } from "../delete-confirmation-dialog.component";
|
10
|
import { CommunityFormComponent } from "./community-form.component";
|
11
|
import { CheckCommunity, Community } from "../../domain/community";
|
12
|
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
|
13
|
|
14
|
@Component({
|
15
|
selector: 'communities',
|
16
|
templateUrl: './communities.component.html',
|
17
|
})
|
18
|
|
19
|
export class CommunitiesComponent implements OnInit {
|
20
|
|
21
|
// @ViewChild(ModalFormComponent)
|
22
|
// @ViewChild('saveModal')
|
23
|
// public modal:ModalFormComponent;
|
24
|
//
|
25
|
// @ViewChild('updateModal')
|
26
|
// public updateModal:ModalFormComponent;
|
27
|
|
28
|
// @ViewChild('deleteConfirmationModal')
|
29
|
// public deleteConfirmationModal : DeleteConfirmationDialogComponent;
|
30
|
@ViewChild('AlertModalSaveCommunity') alertModalSaveCommunity;
|
31
|
@ViewChild('AlertModalUpdateCommunity') alertModalUpdateCommunity;
|
32
|
@ViewChild('AlertModalDeleteCommunities') alertModalDeleteCommunities;
|
33
|
private selectedCommunities: string[] = [];
|
34
|
|
35
|
@ViewChild(CommunityFormComponent)
|
36
|
public formComponent : CommunityFormComponent;
|
37
|
|
38
|
public checkboxes : CheckCommunity[] = [];
|
39
|
|
40
|
public communities : Community[] = [];
|
41
|
|
42
|
//public errorMessage: string;
|
43
|
|
44
|
public formGroup : FormGroup;
|
45
|
|
46
|
private searchText : RegExp = new RegExp('');
|
47
|
public properties:EnvProperties = null;
|
48
|
|
49
|
public showLoading: boolean = true;
|
50
|
public errorMessage: string = '';
|
51
|
public updateErrorMessage: string = '';
|
52
|
public modalErrorMessage: string = '';
|
53
|
|
54
|
ngOnInit() {
|
55
|
this.formGroup = this.formComponent.form;
|
56
|
|
57
|
this.route.data
|
58
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
59
|
this.properties = data.envSpecific;
|
60
|
this.getCommunities();
|
61
|
|
62
|
});
|
63
|
}
|
64
|
|
65
|
constructor(private route: ActivatedRoute, private _helpContentService: HelpContentService) {}
|
66
|
|
67
|
|
68
|
getCommunities() {
|
69
|
this.showLoading = true;
|
70
|
this.updateErrorMessage = "";
|
71
|
this.errorMessage = "";
|
72
|
|
73
|
|
74
|
this._helpContentService.getCommunitiesFull( this.properties.adminToolsAPIURL).subscribe(
|
75
|
communities => {
|
76
|
this.communities = communities;
|
77
|
communities.forEach(_ => {
|
78
|
this.checkboxes.push(<CheckCommunity>{community : _, checked : false});
|
79
|
});
|
80
|
this.showLoading = false;
|
81
|
},
|
82
|
error => this.handleError('System error retrieving communities', error));
|
83
|
}
|
84
|
|
85
|
// public showModal():void {
|
86
|
// this.modal.showModal();
|
87
|
// }
|
88
|
|
89
|
public toggleCheckBoxes(event) {
|
90
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
91
|
}
|
92
|
|
93
|
public applyCheck(flag : boolean) {
|
94
|
console.info("applyCheck "+flag);
|
95
|
this.checkboxes.forEach(_ => _.checked = flag);
|
96
|
}
|
97
|
|
98
|
public getSelectedCommunities() : string[] {
|
99
|
return this.checkboxes.filter(community => community.checked == true).map(checkedCommunity => checkedCommunity.community).map(res => res._id);
|
100
|
}
|
101
|
|
102
|
private deleteCommunitiesFromArray(ids : string[]) : void {
|
103
|
for(let id of ids) {
|
104
|
let i = this.checkboxes.findIndex(_ => _.community._id == id);
|
105
|
this.checkboxes.splice(i, 1);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
public confirmDeleteCommunity(id : string) {
|
110
|
//this.deleteConfirmationModal.ids = [id];
|
111
|
//this.deleteConfirmationModal.showModal();
|
112
|
this.selectedCommunities = [id];
|
113
|
this.confirmModalOpen();
|
114
|
}
|
115
|
|
116
|
public confirmDeleteSelectedCommunities() {
|
117
|
//this.deleteConfirmationModal.ids = this.getSelectedCommunities();
|
118
|
//this.deleteConfirmationModal.showModal();
|
119
|
this.selectedCommunities = this.getSelectedCommunities();
|
120
|
this.confirmModalOpen();
|
121
|
}
|
122
|
|
123
|
private confirmModalOpen() {
|
124
|
this.alertModalDeleteCommunities.cancelButton = true;
|
125
|
this.alertModalDeleteCommunities.okButton = true;
|
126
|
this.alertModalDeleteCommunities.alertTitle = "Delete Confirmation";
|
127
|
this.alertModalDeleteCommunities.message = "Are you sure you want to delete the selected community(-ies)?";
|
128
|
this.alertModalDeleteCommunities.okButtonText = "Yes";
|
129
|
this.alertModalDeleteCommunities.open();
|
130
|
}
|
131
|
|
132
|
public confirmedDeleteCommunities(data: any) {
|
133
|
this.showLoading = true;
|
134
|
this.updateErrorMessage = "";
|
135
|
|
136
|
this._helpContentService.deleteCommunities(this.selectedCommunities, this.properties.adminToolsAPIURL).subscribe(
|
137
|
_ => {
|
138
|
this.deleteCommunitiesFromArray(this.selectedCommunities);
|
139
|
this.showLoading = false;
|
140
|
},
|
141
|
error => this.handleUpdateError('System error deleting the selected communities', error)
|
142
|
);
|
143
|
}
|
144
|
|
145
|
public editCommunity(i : number) {
|
146
|
let community : Community = this.checkboxes[i].community;
|
147
|
this.formGroup.patchValue(community);
|
148
|
//this.updateModal.showModal();
|
149
|
this.modalErrorMessage = "";
|
150
|
this.communitiesModalOpen(this.alertModalUpdateCommunity, "Update", "Update Community");
|
151
|
}
|
152
|
|
153
|
public newCommunity() {
|
154
|
this.formComponent.reset();
|
155
|
this.modalErrorMessage = "";
|
156
|
this.communitiesModalOpen(this.alertModalSaveCommunity, "Save", "Add a new Community");
|
157
|
}
|
158
|
|
159
|
private communitiesModalOpen(modal: any, title: string, yesBtn: string) {
|
160
|
modal.cancelButton = true;
|
161
|
modal.okButton = true;
|
162
|
modal.alertTitle = title;
|
163
|
modal.okButtonText = yesBtn;
|
164
|
modal.open();
|
165
|
}
|
166
|
|
167
|
public communitySaveConfirmed(data: any) {
|
168
|
if(!this.formGroup.valid) {
|
169
|
this.communitiesModalOpen(this.alertModalSaveCommunity, "Save", "Add a new Community");
|
170
|
this.modalErrorMessage = "Please fill in all required fields marked with *";
|
171
|
} else {
|
172
|
this.modalErrorMessage = "";
|
173
|
this._helpContentService.saveCommunity(<Community> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
|
174
|
community => {
|
175
|
this.communitySavedSuccessfully(community);
|
176
|
},
|
177
|
error => this.handleUpdateError("System error creating community", error)
|
178
|
);
|
179
|
}
|
180
|
}
|
181
|
|
182
|
public communityUpdateConfirmed(data: any) {
|
183
|
if(!this.formGroup.valid) {
|
184
|
this.communitiesModalOpen(this.alertModalUpdateCommunity, "Update", "Update Community");
|
185
|
this.modalErrorMessage = "Please fill in all required fields marked with *";
|
186
|
} else {
|
187
|
this._helpContentService.updateCommunity(<Community> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
|
188
|
community => {
|
189
|
this.communityUpdatedSuccessfully(community);
|
190
|
},
|
191
|
error => this.handleUpdateError("System error updating community", error)
|
192
|
);
|
193
|
}
|
194
|
}
|
195
|
|
196
|
public communitySavedSuccessfully(community: Community) {
|
197
|
this.checkboxes.push(<CheckCommunity>{community : community, checked : false});
|
198
|
console.info("checkboxes length: "+this.checkboxes.length);
|
199
|
this.applyCheck(false);
|
200
|
}
|
201
|
|
202
|
public communityUpdatedSuccessfully(community : Community) {
|
203
|
this.checkboxes.find(checkItem => checkItem.community._id==community._id).community = community;
|
204
|
this.applyCheck(false);
|
205
|
}
|
206
|
|
207
|
public filterBySearch(text : string) {
|
208
|
this.searchText = new RegExp(text,'i');
|
209
|
this.applyFilter();
|
210
|
}
|
211
|
|
212
|
public applyFilter() {
|
213
|
this.checkboxes = [];
|
214
|
this.communities.filter(item => this.filterCommunities(item)).forEach(
|
215
|
_ => this.checkboxes.push(<CheckCommunity>{community: _, checked: false})
|
216
|
);
|
217
|
}
|
218
|
|
219
|
public filterCommunities(community : Community) : boolean {
|
220
|
let textFlag = this.searchText.toString() == '' || (community.name).match(this.searchText) != null;
|
221
|
return textFlag;
|
222
|
}
|
223
|
|
224
|
handleUpdateError(message: string, error) {
|
225
|
if(error == null) {
|
226
|
this.formComponent.reset();
|
227
|
} else {
|
228
|
this.updateErrorMessage = message;
|
229
|
console.log('Server responded: ' +error);
|
230
|
}
|
231
|
|
232
|
this.showLoading = false;
|
233
|
}
|
234
|
|
235
|
handleError(message: string, error) {
|
236
|
this.errorMessage = message;
|
237
|
console.log('Server responded: ' + error);
|
238
|
|
239
|
this.showLoading = false;
|
240
|
}
|
241
|
}
|