application.form.main.ts 7.38 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
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

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

43
    //private  sdate;// = new Date(2013,7,29); // = Date.now();
44
45
    //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
            studentbirthdate: ['', [Validators.required]],
            //studentbirthdate: ['', [Validators.pattern(VALID_DATE_PATTERN),Validators.required]],
60
61
62
63
            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:  ['6978333235', [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
105
106
107

                      //if (criteria.selected === true && (criteria.name === "Εισόδημα" ))  {
                      //  this.selectionIncomeId = Number(criteria.id);
                      //}
108
                      this.rss.push( new FormControl(criteria.selected, []));
109
110
111
112
                    return criteria;
                }, {});
            }
            return state.criter;
113
114
115
        }).subscribe(this.criteria$);

    }
116

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

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

128
129
    submitSelected() {
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
130
        this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);
131

132
        this.router.navigate(['/application-submit']);
133
134
    }

135
136
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
137
138
139
140
        //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);
141
142
143
144
          //console.log("Hi!");
          //console.log(mutual_ids);
          console.log(this.studentCriteriaGroup.controls['formArray']['controls']);
          console.log(this.studentCriteriaGroup.controls['formArray']['controls'][mutual_ids[i]-1]);        }
145

146
      }
147
148
149

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

150
151
    }

152
/*
153
    checkstatus(cb) {
154

155
156
157
158
159
160
161
162
        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;
163
    }
164
    */
165

166
167
    //checkChoice(c: FormControl) {
    checkChoice(c: FormControl) {
168
      if (c.value === "noincomecriterio" ) {
169
170
171
172
173
        return {status: true}
      }
      else
      // Null means valid, believe it or not
        return null;
174
    }
175
}