application.form.main.ts 12.5 KB
Newer Older
1
import { Component, OnInit, OnDestroy } from '@angular/core';
2
import { BehaviorSubject, Subscription, Observable } from 'rxjs/Rx';
3
4
5
import { Injectable } from "@angular/core";
import { Router } from '@angular/router';
import { NgRedux, select } from 'ng2-redux';
6
import { StudentDataFieldsActions } from '../../actions/studentdatafields.actions';
7
import { IStudentDataFields } from '../../store/studentdatafields/studentdatafields.types';
8
9
import { CriteriaActions } from '../../actions/criteria.actions';
import { ICriter } from '../../store/criteria/criteria.types';
10
import { IAppState } from '../../store/store';
11
12
import { VALID_NAMES_PATTERN, VALID_ADDRESS_PATTERN, VALID_ADDRESSTK_PATTERN, VALID_DIGITS_PATTERN,
    VALID_DATE_PATTERN, FIRST_SCHOOL_YEAR, VALID_YEAR_PATTERN, VALID_TELEPHONE_PATTERN } from '../../constants';
13
14
import { STUDENT_DATA_FIELDS_INITIAL_STATE } from '../../store/studentdatafields/studentdatafields.initial-state';
import { CRITERIA_INITIAL_STATE } from '../../store/criteria/criteria.initial-state';
15
16
17
import { ILoginInfo, ILoginInfoToken } from '../../store/logininfo/logininfo.types';
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';
import {IMyDpOptions} from 'mydatepicker';
18
import {Http, RequestOptions} from '@angular/http';
19
20
21
22
23

import {
    FormBuilder,
    FormGroup,
    FormControl,
24
    FormArray,
25
26
27
28
29
30
31
32
33
34
    Validators,
} from '@angular/forms';

@Component({
    selector: 'application-form-main',
    templateUrl: './application.form.main.html'
})

@Injectable() export default class StudentApplicationMain implements OnInit {

35
36
37
38
39
    private studentDataFields$: BehaviorSubject<IStudentDataFields>;
    private criteria$: BehaviorSubject<ICriter>;

    private studentDataFieldsSub: Subscription;
    private criteriaSub: Subscription;
40
41

    public studentDataGroup: FormGroup;
42
    public studentCriteriaGroup: FormGroup;
43

44
45
46
47
48
    private loginInfo$: BehaviorSubject<ILoginInfo>;

    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;
49
50
    private schoolyears: string[];
    private graduationyears: string[];
51

52
    private rss = new FormArray([]);
53

54
55
56
57
58
59
    private myDatePickerOptions: IMyDpOptions = {
        // other options...
        sunHighlight: false,
        editableDateField: false,
        dateFormat: 'dd/mm/yyyy',
    };
60

61
    private observableSource = (keyword: any): Observable<any[]> => {
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        let url: string = 'https://mm.sch.gr/api/units?name='+keyword;
        if (keyword) {
            return this.http.get(url)
                .map(res => {
                    let json = res.json();
                    let retArr = <any>Array();
                    for (var i=0; i<json.data.length; i++) {
                        retArr[i] = {};
                        retArr[i].registry_no = json.data[i].registry_no;
                        retArr[i].name = json.data[i].name;
                        retArr[i].unit_type_id = json.data[i].unit_type_id;
                    }
                    return retArr;
                })
        } else {
            return Observable.of([]);
        }
    };
80

81
82
    constructor(private fb: FormBuilder,
                private _sdfa: StudentDataFieldsActions,
83
                private _sdfb: CriteriaActions,
84
                private _ngRedux: NgRedux<IAppState>,
85
86
87
88
                private router: Router,
                private http: Http) {
        this.populateSchoolyears();
        this.populateGraduationyears();
89
90
91
92
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.modalTitle =  new BehaviorSubject("");
        this.modalText =  new BehaviorSubject("");
        this.modalHeader =  new BehaviorSubject("");
93
94
95

        this.studentDataFields$ = new BehaviorSubject(STUDENT_DATA_FIELDS_INITIAL_STATE);
        this.criteria$ = new BehaviorSubject(CRITERIA_INITIAL_STATE);
96
        this.studentDataGroup = this.fb.group({
97
98
99
100
//            epaluser_id: [,[]],

            name: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            studentsurname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
101
102
            studentbirthdate: ['', [Validators.required]],
            //studentbirthdate: ['', [Validators.pattern(VALID_DATE_PATTERN),Validators.required]],
103
104
105
106
107
108
109
            fatherfirstname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            motherfirstname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            regionaddress: ['', [Validators.pattern(VALID_ADDRESS_PATTERN),Validators.required]],
            regiontk: ['', [Validators.pattern(VALID_ADDRESSTK_PATTERN),Validators.required]],
            regionarea: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            certificatetype: ['', this.checkChoice],
            relationtostudent: ['', this.checkChoice],
110
111
            telnum:  ['', [Validators.pattern(VALID_TELEPHONE_PATTERN),Validators.required]],
            graduation_year: ['', this.checkChoice],
112
            lastschool_schoolname: ['', [Validators.required]],
113
114
            lastschool_schoolyear: ['', this.checkChoice],
            lastschool_class: ['', this.checkChoice],
115
116
        });

117

118
/*        this.studentCriteriaGroup = this.fb.group({
119
            formArray: this.rss,
120
        }); */
121

122
123
124
    };

    ngOnInit() {
125
126
127
128
129
130
131
132
133
134
135
        (<any>$('#applicationFormNotice')).appendTo("body");

        this._ngRedux.select(state => {
            if (state.loginInfo.size > 0) {
                state.loginInfo.reduce(({}, loginInfoToken) => {
                    console.log(loginInfoToken.cu_name);
                    return loginInfoToken;
                }, {});
            }
            return state.loginInfo;
        }).subscribe(this.loginInfo$);
136

137
        this.studentDataFieldsSub = this._ngRedux.select(state => {
138
139
            if (state.studentDataFields.size > 0) {
                state.studentDataFields.reduce(({}, studentDataField) => {
140

141
142
143
144
145
146
147
148
                    this.studentDataGroup.controls['name'].setValue(studentDataField.name);
                    this.studentDataGroup.controls['studentsurname'].setValue(studentDataField.studentsurname);
                    this.studentDataGroup.controls['fatherfirstname'].setValue(studentDataField.fatherfirstname);
                    this.studentDataGroup.controls['motherfirstname'].setValue(studentDataField.motherfirstname);
                    this.studentDataGroup.controls['regionaddress'].setValue(studentDataField.regionaddress);
                    this.studentDataGroup.controls['regiontk'].setValue(studentDataField.regiontk);
                    this.studentDataGroup.controls['regionarea'].setValue(studentDataField.regionarea);
                    this.studentDataGroup.controls['certificatetype'].setValue(studentDataField.certificatetype);
149
150
151
152
                    this.studentDataGroup.controls['graduation_year'].setValue(studentDataField.graduation_year);
                    this.studentDataGroup.controls['lastschool_schoolname'].setValue(studentDataField.lastschool_schoolname);
                    this.studentDataGroup.controls['lastschool_schoolyear'].setValue(studentDataField.lastschool_schoolyear);
                    this.studentDataGroup.controls['lastschool_class'].setValue(studentDataField.lastschool_class);
153
                    this.studentDataGroup.controls['relationtostudent'].setValue(studentDataField.relationtostudent);
154
                    this.studentDataGroup.controls['telnum'].setValue(studentDataField.telnum);
155
                    this.studentDataGroup.controls['studentbirthdate'].setValue(this.populateDate(studentDataField.studentbirthdate));
156
157
158
159
                    return studentDataField;
                }, {});
            }
            return state.studentDataFields;
160
        }).subscribe(this.studentDataFields$);
161

162
163


164
/*        this._sdfb.getCriteria(true);
165
        this.criteriaSub = this._ngRedux.select(state => {
166
167
            if (state.criter.size > 0) {
                state.criter.reduce(({}, criteria) => {
168
                    //this.studentCriteriaGroup.setValue(criteria);
169
170
171
172

                      //if (criteria.selected === true && (criteria.name === "Εισόδημα" ))  {
                      //  this.selectionIncomeId = Number(criteria.id);
                      //}
173
                      this.rss.push( new FormControl(criteria.selected, []));
174
175
176
177
                    return criteria;
                }, {});
            }
            return state.criter;
178
        }).subscribe(this.criteria$); */
179

180
    };
181

182
    ngOnDestroy() {
183
        (<any>$('#applicationFormNotice')).remove();
184
        if (this.studentDataFieldsSub) this.studentDataFieldsSub.unsubscribe();
185
//        if (this.criteriaSub) this.criteriaSub.unsubscribe();
186
        if (this.studentDataFields$) this.studentDataFields$.unsubscribe();
187
//        if (this.criteria$) this.criteria$.unsubscribe();
188
        if (this.loginInfo$) this.loginInfo$.unsubscribe();
189
    }
190

191
    navigateBack() {
192
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
193
//        this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);
194
195
196
        this.router.navigate(['/schools-order-select']);
    }

197
    submitSelected() {
198
        // if (this.studentDataGroup.invalid || this.studentCriteriaGroup.invalid) {
199
        if (this.studentDataGroup.invalid || this.invalidFormData()) {
200
            this.modalHeader.next("modal-header-danger");
201
            this.modalTitle.next("Η δήλωση προτίμησης δεν είναι πλήρης");
202
203
204
205
206
            if (this.invalidFormData())
                this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*). Το σχολείο τελευταίας φοίτησης πρέπει να αναζητηθεί και να επιλεχθεί από τα αποτελέσματα της αναζήτησης.");
            else
                this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*)");

207
208
209
            this.showModal();
        } else {
            this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
210
//            this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);
211
212
            this.router.navigate(['/application-submit']);
        }
213
214
    }

215
216
217
218
219
220
221
222
    private invalidFormData() : boolean {

        if (!this.studentDataGroup.controls['lastschool_schoolname'].value.registry_no)
            return true;

        return false;
    }

223
224
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
225
226
227
228
        //this.studentCriteriaGroup.controls['formArray']['controls'][mutual_disabled-1].setValue(false);
        let  mutual_ids = mutual_disabled.split(",");
        for (let i=0; i<mutual_ids.length; i++) {
          this.studentCriteriaGroup.controls['formArray']['controls'][mutual_ids[i]-1].setValue(false);
229
        }
230

231
      }
232
233
    }

234
    checkChoice(c: FormControl) {
235
      return (c.value === "" ) ? {status: true} : null;
236
    }
237
238

    populateDate(d) {
239
240
241
242
        if (d && d.length > 0) {
            return {
                date: {
                    year: d ? parseInt(d.substr(0,4)) : 0,
243
                    month: d ? parseInt(d.substr(5,7)) : 0,
244
245
246
247
248
249
250
251
                    day: d ? parseInt(d.substr(8,10)) : 0
                }
            };
        } else {
            return {
                date: null
            };
        }
252
253
    }

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
    private populateSchoolyears(): void {
        let endYear = new Date().getFullYear();
        this.schoolyears = new Array();
        for (let i=endYear; i>FIRST_SCHOOL_YEAR; i--) {
            this.schoolyears.push((i-1) + "-" + i);
        }
    };

    private populateGraduationyears(): void {
        let endYear = new Date().getFullYear();
        this.graduationyears = new Array();
        for (let i=endYear; i>FIRST_SCHOOL_YEAR; i--) {
            this.graduationyears.push(i + "");
        }
    };

270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    setDate() {
        let date = new Date();
        return { date: {
            year: date.getFullYear() - 14,
            month: date.getMonth() + 1,
            day: date.getDate()}
        };
    }

    clearDate() {
        return null;
    }

    public showModal():void {
        (<any>$('#applicationFormNotice')).modal('show');
    }

    public hideModal():void {
        (<any>$('#applicationFormNotice')).modal('hide');
    }

291
292
293
294
295
296
297
298
299
300
301
302
    lastSchoolListFormatter(data: any): string {
      return data.name;
    };

    lastSchoolValueFormatter(data: any): string {
      return data.name;
    };

    lastSchoolValueChanged(e: any): void {
//        console.log(this.studentDataGroup.controls['lastschool_schoolname'].value);
    };

303
}