application.form.main.ts 11 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
    private schoolyears: string[];
50

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

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

60
    private observableSource = (keyword: any): Observable<any[]> => {
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        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([]);
        }
    };
79

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

        this.studentDataFields$ = new BehaviorSubject(STUDENT_DATA_FIELDS_INITIAL_STATE);
        this.criteria$ = new BehaviorSubject(CRITERIA_INITIAL_STATE);
94
        this.studentDataGroup = this.fb.group({
95
96
            name: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            studentsurname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
97
            studentbirthdate: ['', [Validators.required]],
98
99
100
101
102
103
            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]],
            relationtostudent: ['', this.checkChoice],
104
            telnum:  ['', [Validators.pattern(VALID_TELEPHONE_PATTERN),Validators.required]],
105
            lastschool_schoolname: ['', [Validators.required]],
106
107
            lastschool_schoolyear: ['', this.checkChoice],
            lastschool_class: ['', this.checkChoice],
108
        });
109
110
111
    };

    ngOnInit() {
112
113
114
115
116
117
118
119
120
121
        (<any>$('#applicationFormNotice')).appendTo("body");

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

123
        this.studentDataFieldsSub = this._ngRedux.select(state => {
124
125
            if (state.studentDataFields.size > 0) {
                state.studentDataFields.reduce(({}, studentDataField) => {
126

127
128
129
130
131
132
133
                    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);
134
135
136
                    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);
137
                    this.studentDataGroup.controls['relationtostudent'].setValue(studentDataField.relationtostudent);
138
                    this.studentDataGroup.controls['telnum'].setValue(studentDataField.telnum);
139
                    this.studentDataGroup.controls['studentbirthdate'].setValue(this.populateDate(studentDataField.studentbirthdate));
140
141
142
143
                    return studentDataField;
                }, {});
            }
            return state.studentDataFields;
144
        }).subscribe(this.studentDataFields$);
145

146
    };
147

148
    ngOnDestroy() {
149
        (<any>$('#applicationFormNotice')).remove();
150
        if (this.studentDataFieldsSub) this.studentDataFieldsSub.unsubscribe();
151
        if (this.studentDataFields$) this.studentDataFields$.unsubscribe();
152
        if (this.loginInfo$) this.loginInfo$.unsubscribe();
153
    }
154

155
    navigateBack() {
156
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
157
158
159
        this.router.navigate(['/schools-order-select']);
    }

160
    submitSelected() {
161
162
        let invalidFlag = 0;
        if (this.studentDataGroup.invalid || (invalidFlag = this.invalidFormData()) > 0) {
163
            this.modalHeader.next("modal-header-danger");
164
            this.modalTitle.next("Η δήλωση προτίμησης δεν είναι πλήρης");
165
166
167
            if (invalidFlag === 1 || invalidFlag === 2)
                this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*). Η ημερομηνία γέννησης του μαθητή δεν είναι επιτρεπόμενη για μαθητή ΕΠΑΛ.");
            else if (invalidFlag === 3)
168
169
170
171
                this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*). Το σχολείο τελευταίας φοίτησης πρέπει να αναζητηθεί και να επιλεχθεί από τα αποτελέσματα της αναζήτησης.");
            else
                this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*)");

172
173
174
175
176
            this.showModal();
        } else {
            this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
            this.router.navigate(['/application-submit']);
        }
177
178
    }

179
    private invalidFormData() : number {
180

181
182
183
184
185
        let d = this.studentDataGroup.controls['studentbirthdate'].value;
        if (!d || !d.date || !d.date.year)
            return 1;
        else if ((new Date().getFullYear()) - d.date.year < 15)
            return 2;
186
        if (!this.studentDataGroup.controls['lastschool_schoolname'].value.registry_no)
187
            return 3;
188

189
        return 0;
190
191
    }

192
193
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
194
195
196
197
        //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);
198
        }
199

200
      }
201
202
    }

203
    checkChoice(c: FormControl) {
204
      return (c.value === "" ) ? {status: true} : null;
205
    }
206
207

    populateDate(d) {
208
209
210
211
        if (d && d.length > 0) {
            return {
                date: {
                    year: d ? parseInt(d.substr(0,4)) : 0,
212
                    month: d ? parseInt(d.substr(5,7)) : 0,
213
214
215
216
217
218
219
220
                    day: d ? parseInt(d.substr(8,10)) : 0
                }
            };
        } else {
            return {
                date: null
            };
        }
221
222
    }

223
224
225
226
227
228
229
230
    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);
        }
    };

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    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');
    }

252
253
254
255
256
257
258
259
260
261
262
    lastSchoolListFormatter(data: any): string {
      return data.name;
    };

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

    lastSchoolValueChanged(e: any): void {
    };

263
}