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

import {
    FormBuilder,
    FormGroup,
    FormControl,
24
    FormArray,
25
    Validators,
26
} from "@angular/forms";
27
28

@Component({
29
30
    selector: "application-form-main",
    templateUrl: "./application.form.main.html"
31
32
33
34
})

@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
    private myDatePickerOptions: IMyDpOptions = {
        // other options...
        sunHighlight: false,
        editableDateField: false,
57
        dateFormat: "dd/mm/yyyy",
58
    };
59

60
    private observableSource = (keyword: any): Observable<any[]> => {
61
        let url: string = "https://mm.sch.gr/api/units?name=" + keyword;
62
63
64
65
66
        if (keyword) {
            return this.http.get(url)
                .map(res => {
                    let json = res.json();
                    let retArr = <any>Array();
67
                    for (let i = 0; i < json.data.length; i++) {
68
69
70
71
72
73
                        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;
74
                });
75
76
77
78
        } 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
97
98
99
100
101
102
103
104
105
106
107
            name: ["", [Validators.pattern(VALID_UCASE_NAMES_PATTERN), Validators.required]],
            studentsurname: ["", [Validators.pattern(VALID_UCASE_NAMES_PATTERN), Validators.required]],
            studentbirthdate: ["", [Validators.required]],
            fatherfirstname: ["", [Validators.pattern(VALID_UCASE_NAMES_PATTERN), Validators.required]],
            motherfirstname: ["", [Validators.pattern(VALID_UCASE_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],
            telnum:  ["", [Validators.pattern(VALID_TELEPHONE_PATTERN), Validators.required]],
            lastschool_schoolname: ["", [Validators.required]],
            lastschool_schoolyear: ["", this.checkChoice],
            lastschool_class: ["", this.checkChoice],
108
        });
109
110
111
    };

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

        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
134
135
136
137
138
139
                    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["lastschool_schoolname"].setValue(studentDataField.lastschool_schoolname);
                    this.studentDataGroup.controls["lastschool_schoolyear"].setValue(studentDataField.lastschool_schoolyear);
                    this.studentDataGroup.controls["lastschool_class"].setValue(studentDataField.lastschool_class);
                    this.studentDataGroup.controls["relationtostudent"].setValue(studentDataField.relationtostudent);
                    this.studentDataGroup.controls["telnum"].setValue(studentDataField.telnum);
                    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
        this.router.navigate(["/schools-order-select"]);
158
159
    }

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
                this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*). Το σχολείο τελευταίας φοίτησης πρέπει να αναζητηθεί και να επιλεχθεί από τα αποτελέσματα της αναζήτησης.");
169
170
            else if (invalidFlag === 4)
                this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*). Το τηλέφωνο επικοινωνίας πρέπει να είναι σταθερό τηλέφωνο και να αποτελείται από 10 ψηφία.");
171
172
173
            else
                this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*)");

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

181
    private invalidFormData(): number {
182

183
        let d = this.studentDataGroup.controls["studentbirthdate"].value;
184
185
186
187
        if (!d || !d.date || !d.date.year)
            return 1;
        else if ((new Date().getFullYear()) - d.date.year < 15)
            return 2;
188
        if (!this.studentDataGroup.controls["lastschool_schoolname"].value.registry_no)
189
            return 3;
190
191
        if (this.studentDataGroup.controls["telnum"].value.length !== 10 )
            return 4;
192

193
        return 0;
194
195
    }

196
197
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
198
        // this.studentCriteriaGroup.controls["formArray"]["controls"][mutual_disabled-1].setValue(false);
199
        let  mutual_ids = mutual_disabled.split(",");
200
201
        for (let i = 0; i < mutual_ids.length; i++) {
          this.studentCriteriaGroup.controls["formArray"]["controls"][mutual_ids[i] - 1].setValue(false);
202
        }
203

204
      }
205
206
    }

207
    checkChoice(c: FormControl) {
208
      return (c.value === "" ) ? {status: true} : null;
209
    }
210
211

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

227
228
229
    private populateSchoolyears(): void {
        let endYear = new Date().getFullYear();
        this.schoolyears = new Array();
230
231
        for (let i = endYear; i > FIRST_SCHOOL_YEAR; i--) {
            this.schoolyears.push((i - 1) + "-" + i);
232
233
234
        }
    };

235
236
237
238
239
240
241
242
243
244
245
246
247
    setDate() {
        let date = new Date();
        return { date: {
            year: date.getFullYear() - 14,
            month: date.getMonth() + 1,
            day: date.getDate()}
        };
    }

    clearDate() {
        return null;
    }

248
249
    public showModal(): void {
        (<any>$("#applicationFormNotice")).modal("show");
250
251
    }

252
253
    public hideModal(): void {
        (<any>$("#applicationFormNotice")).modal("hide");
254
255
    }

256
257
258
259
260
261
262
263
264
265
266
    lastSchoolListFormatter(data: any): string {
      return data.name;
    };

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

    lastSchoolValueChanged(e: any): void {
    };

267
}