application.form.main.ts 9.46 KB
Newer Older
1 2
import { Component, OnInit, OnDestroy } from '@angular/core';
import { BehaviorSubject, Subscription } 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
import { VALID_NAMES_PATTERN, VALID_ADDRESS_PATTERN, VALID_ADDRESSTK_PATTERN, VALID_DIGITS_PATTERN,  VALID_DATE_PATTERN } from '../../constants';
12 13
import { STUDENT_DATA_FIELDS_INITIAL_STATE } from '../../store/studentdatafields/studentdatafields.initial-state';
import { CRITERIA_INITIAL_STATE } from '../../store/criteria/criteria.initial-state';
14 15 16
import { ILoginInfo, ILoginInfoToken } from '../../store/logininfo/logininfo.types';
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';
import {IMyDpOptions} from 'mydatepicker';
17 18 19 20 21

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

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

@Injectable() export default class StudentApplicationMain implements OnInit {

33 34 35 36 37
    private studentDataFields$: BehaviorSubject<IStudentDataFields>;
    private criteria$: BehaviorSubject<ICriter>;

    private studentDataFieldsSub: Subscription;
    private criteriaSub: Subscription;
38 39

    public studentDataGroup: FormGroup;
40
    public studentCriteriaGroup: FormGroup;
41

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

    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;

48
    private rss = new FormArray([]);
49

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

57 58
    constructor(private fb: FormBuilder,
                private _sdfa: StudentDataFieldsActions,
59
                private _sdfb: CriteriaActions,
60 61
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
62 63 64 65
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.modalTitle =  new BehaviorSubject("");
        this.modalText =  new BehaviorSubject("");
        this.modalHeader =  new BehaviorSubject("");
66 67 68

        this.studentDataFields$ = new BehaviorSubject(STUDENT_DATA_FIELDS_INITIAL_STATE);
        this.criteria$ = new BehaviorSubject(CRITERIA_INITIAL_STATE);
69
        this.studentDataGroup = this.fb.group({
70 71 72 73
//            epaluser_id: [,[]],

            name: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            studentsurname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
74 75
            studentbirthdate: ['', [Validators.required]],
            //studentbirthdate: ['', [Validators.pattern(VALID_DATE_PATTERN),Validators.required]],
76 77 78 79 80 81 82 83 84 85
            fatherfirstname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            fathersurname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            motherfirstname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            mothersurname: ['', [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],
            telnum:  ['', [Validators.pattern(VALID_DIGITS_PATTERN),Validators.required]],
86 87
        });

88 89
        this.studentCriteriaGroup = this.fb.group({
            formArray: this.rss,
90
        });
91

92 93 94
    };

    ngOnInit() {
95 96 97 98 99 100 101 102 103 104 105
        (<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$);
106

107
        this.studentDataFieldsSub = this._ngRedux.select(state => {
108 109
            if (state.studentDataFields.size > 0) {
                state.studentDataFields.reduce(({}, studentDataField) => {
110 111 112 113 114 115 116 117 118 119 120 121
                    this.studentDataGroup.controls['name'].setValue(studentDataField.name);
                    this.studentDataGroup.controls['studentsurname'].setValue(studentDataField.studentsurname);
                    this.studentDataGroup.controls['fatherfirstname'].setValue(studentDataField.fatherfirstname);
                    this.studentDataGroup.controls['fathersurname'].setValue(studentDataField.fathersurname);
                    this.studentDataGroup.controls['motherfirstname'].setValue(studentDataField.motherfirstname);
                    this.studentDataGroup.controls['mothersurname'].setValue(studentDataField.mothersurname);
                    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);
                    this.studentDataGroup.controls['telnum'].setValue(studentDataField.telnum);
                    this.studentDataGroup.setValue({studentbirthdate: this.populateDate(studentDataField.studentbirthdate)});
122 123 124 125
                    return studentDataField;
                }, {});
            }
            return state.studentDataFields;
126
        }).subscribe(this.studentDataFields$);
127

128
        this._sdfb.getCriteria(true);
129
        this.criteriaSub = this._ngRedux.select(state => {
130 131
            if (state.criter.size > 0) {
                state.criter.reduce(({}, criteria) => {
132
                    //this.studentCriteriaGroup.setValue(criteria);
133 134 135 136

                      //if (criteria.selected === true && (criteria.name === "Εισόδημα" ))  {
                      //  this.selectionIncomeId = Number(criteria.id);
                      //}
137
                      this.rss.push( new FormControl(criteria.selected, []));
138 139 140 141
                    return criteria;
                }, {});
            }
            return state.criter;
142 143 144
        }).subscribe(this.criteria$);

    }
145

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

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

161
    submitSelected() {
162 163 164 165 166 167 168 169 170 171
        if (this.studentDataGroup.invalid || this.studentCriteriaGroup.invalid) {
            this.modalHeader.next("modal-header-danger");
            this.modalTitle.next("Η αίτηση δεν είναι πλήρης");
            this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*)");
            this.showModal();
        } else {
            this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
            this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);
            this.router.navigate(['/application-submit']);
        }
172 173
    }

174 175
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
176 177 178 179
        //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);
180
        }
181

182
      }
183 184
    }

185
    checkChoice(c: FormControl) {
186
      if (c.value === "" ) {
187 188 189 190 191
        return {status: true}
      }
      else
      // Null means valid, believe it or not
        return null;
192
    }
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

    populateDate(d) {
        return {
            date: {
                year: d ? parseInt(d.substr(0,4)) : 0,
                month: d ? parseInt(d.substr(6,8)) : 0,
                day: d ? parseInt(d.substr(8,10)) : 0
            }
        };
    }

    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');
    }

225
}