application.form.main.ts 7.02 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 } 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 17 18

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

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

@Injectable() export default class StudentApplicationMain implements OnInit {

30 31 32 33 34
    private studentDataFields$: BehaviorSubject<IStudentDataFields>;
    private criteria$: BehaviorSubject<ICriter>;

    private studentDataFieldsSub: Subscription;
    private criteriaSub: Subscription;
35 36

    public studentDataGroup: FormGroup;
37 38
    public applicantDataGroup: FormGroup;
    public studentCriteriaGroup: FormGroup;
39 40 41

    private rss = new FormArray([]);
    private selectionIncomeId = <number>0;
42

43 44 45
    private  sdate; // = Date.now();
    //date: [datePipe.transform(this.event.date, 'yyyy-MM-dd'), [Validators.required]]

46 47
    constructor(private fb: FormBuilder,
                private _sdfa: StudentDataFieldsActions,
48
                private _sdfb: CriteriaActions,
49 50
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
51 52 53

        this.studentDataFields$ = new BehaviorSubject(STUDENT_DATA_FIELDS_INITIAL_STATE);
        this.criteria$ = new BehaviorSubject(CRITERIA_INITIAL_STATE);
54
        this.studentDataGroup = this.fb.group({
55
            epaluser_id: [,[]],
56 57
            name: ['ΝΙΚΟΣ', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            studentsurname: ['ΚΑΤΣΑΟΥΝΟΣ', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
58 59 60 61 62 63
            studentbirthdate: [this.sdate, [Validators.required]],
            //studentbirthdate: [datePipe.transform(this.event.date, 'yyyy-MM-dd'), [Validators.required]],
            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]],
64 65 66
            regionaddress: ['ΓΙΑΝΝΙΤΣΩΝ 5', [Validators.pattern(VALID_ADDRESS_PATTERN),Validators.required]],
            regiontk: ['26334', [Validators.pattern(VALID_ADDRESSTK_PATTERN),Validators.required]],
            regionarea: ['ΠΑΤΡΑ', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
67 68
            certificatetype: ['Απολυτήριο Λυκείου', this.checkChoice],
            relationtostudent: ['Μαθητής', this.checkChoice],
69
            telnum:  ['2610789789', [Validators.pattern(VALID_DIGITS_PATTERN),Validators.required]],
70
        });
71 72 73 74 75 76

        this.applicantDataGroup = this.fb.group({
          guardianfirstname: ['ΑΝΑΣΤΑΣΙΟΣ', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
          guardiansurname: ['ΚΑΤΣΑΟΥΝΟΣ', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
        });

77 78
        this.studentCriteriaGroup = this.fb.group({
            formArray: this.rss,
79
            income: ['', this.checkChoice ],
80
            //income: ['noincomecriterio', Validators.required ],
81
            //income: [this.selectionIncomeId, this.checkChoice ],
82
            incometest: ['noincomecriterio', this. checkChoice ],
83
        });
84

85 86 87
    };

    ngOnInit() {
88

89
        this.studentDataFieldsSub = this._ngRedux.select(state => {
90 91 92 93 94 95 96
            if (state.studentDataFields.size > 0) {
                state.studentDataFields.reduce(({}, studentDataField) => {
                    this.studentDataGroup.setValue(studentDataField);
                    return studentDataField;
                }, {});
            }
            return state.studentDataFields;
97
        }).subscribe(this.studentDataFields$);
98

99
        this._sdfb.getCriteria(true);
100
        this.criteriaSub = this._ngRedux.select(state => {
101 102
            if (state.criter.size > 0) {
                state.criter.reduce(({}, criteria) => {
103
                    //this.studentCriteriaGroup.setValue(criteria);
104
                      if (criteria.selected === true && (criteria.name === "Εισόδημα" ))  {
105
                        this.selectionIncomeId = Number(criteria.id);
106
                      }
107
                      this.rss.push( new FormControl(criteria.selected, []));
108 109 110 111
                    return criteria;
                }, {});
            }
            return state.criter;
112 113 114
        }).subscribe(this.criteria$);

    }
115

116 117 118
    ngOnDestroy() {
        if (this.studentDataFieldsSub) this.studentDataFieldsSub.unsubscribe();
        if (this.criteriaSub) this.criteriaSub.unsubscribe();
119 120
        if (this.studentDataFields$) this.studentDataFields$.unsubscribe();
        if (this.criteria$) this.criteria$.unsubscribe();
121
    }
122

123 124 125 126
    navigateBack() {
        this.router.navigate(['/schools-order-select']);
    }

127 128
    submitSelected() {
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
129

130 131
        for (let i=7; i < 11; i++)
          this.studentCriteriaGroup.controls['formArray']['controls'][i].setValue(false);
132
        this.studentCriteriaGroup.controls['formArray']['controls'][this.selectionIncomeId-1].setValue(true);
133

134
        this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);
135

136
        this.router.navigate(['/application-submit']);
137 138
    }

139 140 141 142
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
        this.studentCriteriaGroup.controls['formArray']['controls'][mutual_disabled-1].setValue(false);
      }
143 144
    }

145 146 147 148 149 150 151 152 153
    checkstatus(cb) {
        if (cb.value === "<= 3000 Ευρώ")
          this.selectionIncomeId = 8;
        else if (cb.value === "<= 6000 Ευρώ")
          this.selectionIncomeId = 9;
        else if (cb.value === "<= 9000 Ευρώ")
          this.selectionIncomeId = 10;
        else if (cb.value === "> 9000 Ευρώ")
          this.selectionIncomeId = 11;
154
    }
155

156 157
    //checkChoice(c: FormControl) {
    checkChoice(c: FormControl) {
158
      if (c.value === "noincomecriterio" ) {
159 160 161 162 163
        return {status: true}
      }
      else
      // Null means valid, believe it or not
        return null;
164
    }
165
}