1
|
/**
|
2
|
* Created by stefania on 4/26/17.
|
3
|
*/
|
4
|
import { Component, ViewChild, OnInit } from '@angular/core';
|
5
|
import { FAQService } from "../../services/faq.service";
|
6
|
import { FormGroup } from "@angular/forms";
|
7
|
import { ModalFormComponent } from "../modal-form.component";
|
8
|
import { QuestionsFormComponent } from "./questions-form.component";
|
9
|
import { CheckQuestion, Question, QuestionFilterOptions } from "../../domain/question";
|
10
|
import { Topic } from "../../domain/topic";
|
11
|
import { DeleteConfirmationDialogComponent } from "../delete-confirmation-dialog.component";
|
12
|
|
13
|
@Component({
|
14
|
selector: 'questions',
|
15
|
templateUrl: './questions.component.html',
|
16
|
})
|
17
|
|
18
|
export class QuestionsComponent implements OnInit {
|
19
|
|
20
|
// @ViewChild(ModalFormComponent)
|
21
|
@ViewChild('saveModal')
|
22
|
public modal:ModalFormComponent;
|
23
|
|
24
|
@ViewChild('updateModal')
|
25
|
public updateModal:ModalFormComponent;
|
26
|
|
27
|
@ViewChild(QuestionsFormComponent)
|
28
|
public formComponent : QuestionsFormComponent;
|
29
|
|
30
|
@ViewChild('deleteConfirmationModal')
|
31
|
public deleteConfirmationModal : DeleteConfirmationDialogComponent;
|
32
|
|
33
|
public questionsCheckboxes : CheckQuestion[] = [];
|
34
|
|
35
|
public questions : Question[] = [];
|
36
|
|
37
|
public errorMessage: string = null;
|
38
|
|
39
|
public successMessage: string = null;
|
40
|
|
41
|
public formGroup : FormGroup;
|
42
|
|
43
|
public topics: Topic[];
|
44
|
|
45
|
public checkboxAll : boolean = false;
|
46
|
|
47
|
public filters : QuestionFilterOptions = {id : '', active : null, text : new RegExp('')};
|
48
|
|
49
|
public counter = {all : 0, active : 0, inactive : 0};
|
50
|
|
51
|
ngOnInit() {
|
52
|
this.getTopics();
|
53
|
this.getQuestions();
|
54
|
this.formGroup = this.formComponent.form;
|
55
|
}
|
56
|
|
57
|
constructor(private _faqService: FAQService) {}
|
58
|
|
59
|
getTopics() {
|
60
|
this._faqService.getTopics().subscribe(
|
61
|
topics => this.topics = topics,
|
62
|
error => this.errorMessage = <any>error);
|
63
|
}
|
64
|
|
65
|
public countQuestions() {
|
66
|
this.counter = {all : 0, active : 0, inactive : 0};
|
67
|
let filter = Object.assign({},this.filters);
|
68
|
filter.active = null;
|
69
|
this.questions.forEach(_ => {
|
70
|
if(this.filterQuestion(_,filter)){
|
71
|
if (_.isActive==true) this.counter.active++;
|
72
|
else this.counter.inactive++
|
73
|
}
|
74
|
});
|
75
|
this.counter.all = this.counter.active + this.counter.inactive;
|
76
|
}
|
77
|
|
78
|
getQuestions() {
|
79
|
let self = this;
|
80
|
this._faqService.getQuestions().subscribe(
|
81
|
questions => {
|
82
|
self.questions = questions;
|
83
|
self.counter.all = questions.length;
|
84
|
questions.forEach(_ => {
|
85
|
self.questionsCheckboxes.push(<CheckQuestion>{question : _, checked : false});
|
86
|
});
|
87
|
self.countQuestions();
|
88
|
},
|
89
|
error => this.errorMessage = <any>error);
|
90
|
}
|
91
|
|
92
|
public showModal():void {
|
93
|
this.modal.showModal();
|
94
|
}
|
95
|
|
96
|
public toggleCheckBoxes(event) {
|
97
|
this.questionsCheckboxes.forEach(_ => _.checked = event.target.checked);
|
98
|
this.checkboxAll = event.target.checked;
|
99
|
}
|
100
|
|
101
|
public applyCheck(flag : boolean) {
|
102
|
this.questionsCheckboxes.forEach(_ => _.checked = flag);
|
103
|
this.checkboxAll = false;
|
104
|
}
|
105
|
|
106
|
public getSelectedQuestions() : string[] {
|
107
|
return this.questionsCheckboxes.filter(question => question.checked == true)
|
108
|
.map(checkedQuestion => checkedQuestion.question).map(res => res._id);
|
109
|
}
|
110
|
|
111
|
public confirmDeleteQuestion(id : string) {
|
112
|
this.deleteConfirmationModal.ids = [id];
|
113
|
this.deleteConfirmationModal.showModal();
|
114
|
}
|
115
|
|
116
|
public confirmDeleteSelectedQuestions() {
|
117
|
this.deleteConfirmationModal.ids = this.getSelectedQuestions();
|
118
|
this.deleteConfirmationModal.showModal();
|
119
|
}
|
120
|
|
121
|
public confirmedDeleteQuestions(ids : string[]) {
|
122
|
this._faqService.deleteQuestions(ids).subscribe(
|
123
|
_ => this.deleteQuestionsFromArray(ids),
|
124
|
error => this.handleError(error)
|
125
|
);
|
126
|
}
|
127
|
|
128
|
private deleteQuestionsFromArray(ids : string[]) : void {
|
129
|
this.successMessage = `Successfully deleted Question(s)`;
|
130
|
for(let id of ids) {
|
131
|
let iqc = this.questionsCheckboxes.findIndex(_ => _.question._id == id);
|
132
|
let iq = this.questions.findIndex(_ => _._id == id);
|
133
|
this.questionsCheckboxes.splice(iqc, 1);
|
134
|
this.questions.splice(iqc, 1);
|
135
|
}
|
136
|
}
|
137
|
|
138
|
public editQuestion(i : number) {
|
139
|
let question : Question = Object.assign({}, this.questionsCheckboxes[i].question);
|
140
|
// question.topics = <Topic[]>Object.create(this.questionsCheckboxes[i].question.topics);
|
141
|
let topics : string[] = [];
|
142
|
for(let topic of <Topic[]>question.topics) {
|
143
|
topics.push(topic._id)
|
144
|
}
|
145
|
question.topics = topics;
|
146
|
console.log(question);
|
147
|
this.formGroup.patchValue(question);
|
148
|
this.updateModal.showModal();
|
149
|
}
|
150
|
|
151
|
public toggleQuestion(status : boolean, ids : string[]) {
|
152
|
this._faqService.toggleQuestion(ids,status).subscribe(
|
153
|
ret => {
|
154
|
for(let id of ret) {
|
155
|
let i = this.questionsCheckboxes.findIndex(_ => _.question._id == id);
|
156
|
this.questionsCheckboxes[i].question.isActive=status;
|
157
|
}
|
158
|
this.countQuestions();
|
159
|
this.applyCheck(false);
|
160
|
},
|
161
|
error => this.handleError(<any>error)
|
162
|
);
|
163
|
}
|
164
|
|
165
|
public saveQuestion(data : any):void {
|
166
|
console.log(data);
|
167
|
this._faqService.saveQuestion(data).subscribe(
|
168
|
question => this.questionSavedSuccessfully(question),
|
169
|
error => this.handleError(<any>error)
|
170
|
);
|
171
|
}
|
172
|
|
173
|
public questionSavedSuccessfully(question: Question) {
|
174
|
this.questionsCheckboxes.push(<CheckQuestion>{question : question, checked : false});
|
175
|
this.questions.push(question);
|
176
|
this.successMessage = `Question [${question.question}] saved successfully`;
|
177
|
this.applyCheck(false);
|
178
|
this.countQuestions();
|
179
|
}
|
180
|
|
181
|
public questionUpdatedSuccessfully(question : Question) {
|
182
|
this.questionsCheckboxes.find(checkItem => checkItem.question._id==question._id).question = question;
|
183
|
let index = this.questions.findIndex(checkItem => checkItem._id==question._id);
|
184
|
this.successMessage = `Question [${question.question}] updated successfully`;
|
185
|
this.questions[index] = question;
|
186
|
this.applyCheck(false);
|
187
|
this.countQuestions();
|
188
|
}
|
189
|
|
190
|
|
191
|
public filterQuestion(question : Question, filters : QuestionFilterOptions) : boolean {
|
192
|
|
193
|
let idFlag = filters.id == '' || (<Topic[]>question.topics).map(_ => _._id).includes(filters.id);
|
194
|
let activeFlag = filters.active == null || question.isActive == filters.active;
|
195
|
let textFlag = filters.text.toString() == '' || (question.question + ' ' +question.answer).match(filters.text) != null;
|
196
|
return idFlag && activeFlag && textFlag;
|
197
|
}
|
198
|
|
199
|
public applyFilter() {
|
200
|
this.questionsCheckboxes = [];
|
201
|
this.questions.filter(item => this.filterQuestion(item,this.filters)).forEach(
|
202
|
_ => this.questionsCheckboxes.push(<CheckQuestion>{question: _, checked: false})
|
203
|
);
|
204
|
this.countQuestions();
|
205
|
}
|
206
|
|
207
|
public filterByTopic(event: any) {
|
208
|
this.filters.id = event.target.value;
|
209
|
this.applyFilter();
|
210
|
}
|
211
|
|
212
|
public displayAllQuestions() {
|
213
|
this.filters.active = null;
|
214
|
this.applyFilter();
|
215
|
}
|
216
|
|
217
|
public displayActiveQuestions() {
|
218
|
this.filters.active = true;
|
219
|
this.applyFilter();
|
220
|
}
|
221
|
|
222
|
public filterBySearch(text : string) {
|
223
|
this.filters.text = new RegExp(text, "i");
|
224
|
this.applyFilter();
|
225
|
}
|
226
|
|
227
|
public displayInactiveQuestions() {
|
228
|
this.filters.active = false;
|
229
|
this.applyFilter();
|
230
|
}
|
231
|
|
232
|
public getNames(question : Question) : string[]{
|
233
|
return (<Topic[]>question.topics).map(_ => _.name);
|
234
|
}
|
235
|
|
236
|
handleError(error) {
|
237
|
if(error == null) {
|
238
|
this.formComponent.reset();
|
239
|
}else {
|
240
|
this.errorMessage = 'System error saving topic (Server responded: ' + error + ')';
|
241
|
}
|
242
|
}
|
243
|
}
|