]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/survey/survey-edit.component.ts
LP 2061136 follow-up: ng lint --fix
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / survey / survey-edit.component.ts
1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {ActivatedRoute} from '@angular/router';
3 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
4 import {StringComponent} from '@eg/share/string/string.component';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {NetService} from '@eg/core/net.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {IdlObject, IdlService } from '@eg/core/idl.service';
9 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
10
11 @Component({
12     templateUrl: './survey-edit.component.html'
13 })
14
15 export class SurveyEditComponent implements OnInit {
16     surveyId: number;
17     surveyObj: IdlObject;
18     localArray: any;
19     newAnswerArray: object[];
20     newQuestionText: string;
21     surveyTab: string;
22
23     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
24
25     @ViewChild('createAnswerString', { static: true })
26         createAnswerString: StringComponent;
27     @ViewChild('createAnswerErrString', { static: true })
28         createAnswerErrString: StringComponent;
29     @ViewChild('createQuestionString', { static: true })
30         createQuestionString: StringComponent;
31     @ViewChild('createQuestionErrString', { static: true })
32         createQuestionErrString: StringComponent;
33
34     @ViewChild('updateQuestionSuccessStr', { static: true })
35         updateQuestionSuccessStr: StringComponent;
36     @ViewChild('updateQuestionFailStr', { static: true })
37         updateQuestionFailStr: StringComponent;
38     @ViewChild('updateAnswerSuccessStr', { static: true })
39         updateAnswerSuccessStr: StringComponent;
40     @ViewChild('updateAnswerFailStr', { static: true })
41         updateAnswerFailStr: StringComponent;
42
43     @ViewChild('delAnswerSuccessStr', { static: true })
44         delAnswerSuccessStr: StringComponent;
45     @ViewChild('delAnswerFailStr', { static: true })
46         delAnswerFailStr: StringComponent;
47     @ViewChild('delQuestionSuccessStr', { static: true })
48         delQuestionSuccessStr: StringComponent;
49     @ViewChild('delQuestionFailStr', { static: true })
50         delQuestionFailStr: StringComponent;
51
52     @ViewChild('endSurveyFailedString', { static: true })
53         endSurveyFailedString: StringComponent;
54     @ViewChild('endSurveySuccessString', { static: true })
55         endSurveySuccessString: StringComponent;
56     @ViewChild('questionAlreadyStartedErrString', { static: true })
57         questionAlreadyStartedErrString: StringComponent;
58
59     constructor(
60         private auth: AuthService,
61         private net: NetService,
62         private route: ActivatedRoute,
63         private toast: ToastService,
64         private idl: IdlService,
65     ) {
66     }
67
68     ngOnInit() {
69         this.surveyId = parseInt(this.route.snapshot.paramMap.get('id'), 10);
70         this.updateData();
71     }
72
73     updateData() {
74         this.newQuestionText = '';
75         this.net.request(
76             'open-ils.circ',
77             'open-ils.circ.survey.fleshed.retrieve',
78             this.surveyId
79         ).subscribe(res => {
80             this.surveyObj = res;
81             this.buildLocalArray(res);
82             return res;
83         });
84     }
85
86     onNavChange(event: NgbNavChangeEvent) {
87         this.surveyTab = event.nextId;
88     }
89
90     buildLocalArray(res) {
91         this.localArray = [];
92         this.newAnswerArray = [];
93         const allQuestions = res.questions();
94         allQuestions.forEach((question, index) => {
95             this.newAnswerArray.push({inputText: ''});
96             question.words = question.question();
97             question.answers = question.answers();
98             this.localArray.push(question);
99             question.answers.forEach(answer => {
100                 answer.words = answer.answer();
101             });
102             this.sortAnswers(index);
103         });
104         this.sortQuestions();
105     }
106
107     sortQuestions() {
108         this.localArray.sort(function(a, b) {
109             const q1 = a.question().toUpperCase();
110             const q2 = b.question().toUpperCase();
111             return (q1 < q2) ? -1 : (q1 > q2) ? 1 : 0;
112         });
113     }
114
115     sortAnswers(questionIndex) {
116         this.localArray[questionIndex].answers.sort(function(a, b) {
117             const a1 = a.answer().toUpperCase();
118             const a2 = b.answer().toUpperCase();
119             return (a1 < a2) ? -1 : (a1 > a2) ? 1 : 0;
120         });
121     }
122
123     updateQuestion(questionToChange) {
124         if (this.surveyHasBegun()) {
125             return;
126         }
127         questionToChange.question(questionToChange.words);
128         questionToChange.ischanged(true);
129         this.net.request(
130             'open-ils.circ',
131             'open-ils.circ.survey.update',
132             this.auth.token(), this.surveyObj
133         ).subscribe(res => {
134             if (res.debug) {
135                 this.updateQuestionFailStr.current().then(msg => this.toast.warning(msg));
136                 return res;
137             } else {
138                 this.surveyObj = res;
139                 this.buildLocalArray(this.surveyObj);
140                 this.updateQuestionSuccessStr.current().then(msg => this.toast.success(msg));
141                 return res;
142             }
143         });
144     }
145
146     deleteQuestion(questionToDelete) {
147         if (this.surveyHasBegun()) {
148             return;
149         }
150         questionToDelete.isdeleted(true);
151         this.net.request(
152             'open-ils.circ',
153             'open-ils.circ.survey.update',
154             this.auth.token(), this.surveyObj
155         ).subscribe(res => {
156             if (res.debug) {
157                 this.delQuestionFailStr.current().then(msg => this.toast.warning(msg));
158                 return res;
159             } else {
160                 this.surveyObj = res;
161                 this.buildLocalArray(this.surveyObj);
162                 this.delQuestionSuccessStr.current().then(msg => this.toast.success(msg));
163                 return res;
164             }
165
166         });
167     }
168
169     createQuestion(newQuestionText) {
170         if (this.surveyHasBegun()) {
171             return;
172         }
173         const newQuestion = this.idl.create('asvq');
174         newQuestion.question(newQuestionText);
175         newQuestion.isnew(true);
176         let questionObjects = [];
177         questionObjects = this.surveyObj.questions();
178         questionObjects.push(newQuestion);
179         this.surveyObj.questions(questionObjects);
180         this.net.request(
181             'open-ils.circ',
182             'open-ils.circ.survey.update',
183             this.auth.token(), this.surveyObj
184         ).subscribe(res => {
185             if (res.debug) {
186                 this.newQuestionText = '';
187                 this.createQuestionErrString.current().then(msg => this.toast.warning(msg));
188                 return res;
189             } else {
190                 this.surveyObj = res;
191                 this.buildLocalArray(this.surveyObj);
192                 this.newQuestionText = '';
193                 this.createQuestionString.current().then(msg => this.toast.success(msg));
194                 return res;
195             }
196
197         });
198     }
199
200     deleteAnswer(answerObj) {
201         if (this.surveyHasBegun()) {
202             return;
203         }
204         answerObj.isdeleted(true);
205         this.net.request(
206             'open-ils.circ',
207             'open-ils.circ.survey.update',
208             this.auth.token(), this.surveyObj
209         ).subscribe(res => {
210             if (res.debug) {
211                 this.delAnswerFailStr.current().then(msg => this.toast.warning(msg));
212                 return res;
213             } else {
214                 this.surveyObj = res;
215                 this.buildLocalArray(this.surveyObj);
216                 this.delAnswerSuccessStr.current().then(msg => this.toast.success(msg));
217                 return res;
218             }
219         });
220     }
221
222     updateAnswer(answerObj) {
223         if (this.surveyHasBegun()) {
224             return;
225         }
226         answerObj.answer(answerObj.words);
227         answerObj.ischanged(true);
228         this.net.request(
229             'open-ils.circ',
230             'open-ils.circ.survey.update',
231             this.auth.token(), this.surveyObj
232         ).subscribe(res => {
233             if (res.debug) {
234                 this.updateAnswerFailStr.current().then(msg => this.toast.warning(msg));
235                 return res;
236             } else {
237                 this.surveyObj = res;
238                 this.buildLocalArray(this.surveyObj);
239                 this.updateAnswerSuccessStr.current().then(msg => this.toast.success(msg));
240                 return res;
241             }
242         });
243     }
244
245     createAnswer(newAnswerText, questionObj) {
246         // Create answer *is* allowed if survey has already begun
247         const questionId = questionObj.id();
248         const newAnswer = this.idl.create('asva');
249         newAnswer.answer(newAnswerText);
250         newAnswer.question(questionId);
251         newAnswer.isnew(true);
252         questionObj.answers.push(newAnswer);
253         this.net.request(
254             'open-ils.circ',
255             'open-ils.circ.survey.update',
256             this.auth.token(), this.surveyObj
257         ).subscribe(res => {
258             if (res.debug) {
259                 this.createAnswerErrString.current().then(msg => this.toast.warning(msg));
260                 return res;
261             } else {
262                 this.surveyObj = res;
263                 this.buildLocalArray(this.surveyObj);
264                 this.createAnswerString.current().then(msg => this.toast.success(msg));
265                 return res;
266             }
267         });
268     }
269
270     endSurvey() {
271         const today = new Date().toISOString();
272         this.surveyObj.end_date(today);
273         this.surveyObj.ischanged(true);
274         // to get fm-editor to display changed date we need to set
275         // this.surveyObj to null temporarily
276         const surveyClone = this.idl.clone(this.surveyObj);
277         this.surveyObj = null;
278         this.net.request(
279             'open-ils.circ',
280             'open-ils.circ.survey.update',
281             this.auth.token(), surveyClone
282         ).subscribe(res => {
283             if (res.debug) {
284                 this.endSurveyFailedString.current().then(msg => this.toast.warning(msg));
285                 return res;
286             } else {
287                 this.surveyObj = res;
288                 this.surveyObj.ischanged(false);
289                 this.buildLocalArray(this.surveyObj);
290                 this.endSurveySuccessString.current().then(msg => this.toast.success(msg));
291                 return res;
292             }
293         });
294     }
295
296     surveyHasBegun() {
297         const surveyStartDate = new Date(this.surveyObj.start_date());
298         const now = new Date();
299         if (surveyStartDate <= now) {
300             this.questionAlreadyStartedErrString.current().then(msg =>
301                 this.toast.warning(msg));
302             return true;
303         }
304         return false;
305     }
306 }
307