application.form.main.ts 11.9 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
import { Router } from "@angular/router";
5
import { NgRedux, select } from "@angular-redux/store";
6 7 8 9 10 11 12 13 14 15 16 17 18
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
    private loginInfo$: BehaviorSubject<ILoginInfo>;
36 37 38 39
    private studentDataFields$: BehaviorSubject<IStudentDataFields>;
    private criteria$: BehaviorSubject<ICriter>;

    private studentDataFieldsSub: Subscription;
40
    private loginInfoSub: Subscription;
41
    private criteriaSub: Subscription;
42 43

    public studentDataGroup: FormGroup;
44
    public studentCriteriaGroup: FormGroup;
45

46 47 48
    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
        this.modalTitle =  new BehaviorSubject("");
        this.modalText =  new BehaviorSubject("");
        this.modalHeader =  new BehaviorSubject("");
90

91
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        this.loginInfoSub = this._ngRedux.select('loginInfo')
            .subscribe(loginInfo => {
                this.loginInfo$.next(<ILoginInfo>loginInfo);
            }, error => {console.log("error selecting loginInfo");});

        this.studentDataFieldsSub = this._ngRedux.select('studentDataFields')
            .subscribe(studentDataFields => {
                let sdfds = <IStudentDataFields>studentDataFields;
                if (sdfds.size > 0) {
                    sdfds.reduce(({}, studentDataField) => {

                        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));
                        return studentDataField;
                    }, {});
                }
                this.studentDataFields$.next(sdfds);
            }, error => {console.log("error selecting studentDataFields");});
143

144
    };
145

146
    ngOnDestroy() {
147
        (<any>$("#applicationFormNotice")).remove();
148
        if (this.studentDataFieldsSub) this.studentDataFieldsSub.unsubscribe();
149
    }
150

151
    navigateBack() {
152
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
153
        this.router.navigate(["/schools-order-select"]);
154 155
    }

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

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

177
    private invalidFormData(): number {
178

179
        let d = this.studentDataGroup.controls["studentbirthdate"].value;
180 181
        if (!d || !d.date || !d.date.year)
            return 1;
182
        else if ((new Date().getFullYear()) - d.date.year < 14)
183
            return 2;
184 185
        if (!this.studentDataGroup.controls["lastschool_schoolname"].value.registry_no &&
            this.studentDataGroup.controls["lastschool_schoolname"].value.unit_type_id!==38)
186
            return 3;
187 188
        else if (this.studentDataGroup.controls["lastschool_schoolname"].value.unit_type_id===38)
            this.studentDataGroup.controls["lastschool_schoolname"].value.registry_no='0000000'
189 190
        if (this.studentDataGroup.controls["telnum"].value.length !== 10 )
            return 4;
191

192
        return 0;
193 194
    }

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

203
      }
204 205
    }

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

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

226 227 228
    private populateSchoolyears(): void {
        let endYear = new Date().getFullYear();
        this.schoolyears = new Array();
229 230
        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
    setDate() {
        let date = new Date();
        return { date: {
            year: date.getFullYear() - 14,
            month: date.getMonth() + 1,
            day: date.getDate()}
        };
    }

    clearDate() {
        return null;
    }

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

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

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

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

    lastSchoolValueChanged(e: any): void {
    };

266
}