application.form.main.ts 6.14 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
    constructor(private fb: FormBuilder,
                private _sdfa: StudentDataFieldsActions,
45
                private _sdfb: CriteriaActions,
46
47
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
48
49
50

        this.studentDataFields$ = new BehaviorSubject(STUDENT_DATA_FIELDS_INITIAL_STATE);
        this.criteria$ = new BehaviorSubject(CRITERIA_INITIAL_STATE);
51
        this.studentDataGroup = this.fb.group({
52
            epaluser_id: [,[]],
53
54
55
56
57
            name: ['ΝΙΚΟΣ', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            studentsurname: ['ΚΑΤΣΑΟΥΝΟΣ', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            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]],
58
59
            certificatetype: ['Απολυτήριο Λυκείου', this.checkChoice],
            relationtostudent: ['Μαθητής', this.checkChoice],
60
            telnum:  ['2610789789', [Validators.pattern(VALID_DIGITS_PATTERN),Validators.required]],
61
        });
62
63
64
65
66
67

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

68
69
        this.studentCriteriaGroup = this.fb.group({
            formArray: this.rss,
70
            income: ['', this.checkChoice ],
71
            //income: ['noincomecriterio', Validators.required ],
72
            //income: [this.selectionIncomeId, this.checkChoice ],
73
            //incometest: ['noincomecriterio', checkChoice ],
74
        });
75

76
77
78
    };

    ngOnInit() {
79

80
        this.studentDataFieldsSub = this._ngRedux.select(state => {
81
82
83
84
85
86
87
            if (state.studentDataFields.size > 0) {
                state.studentDataFields.reduce(({}, studentDataField) => {
                    this.studentDataGroup.setValue(studentDataField);
                    return studentDataField;
                }, {});
            }
            return state.studentDataFields;
88
        }).subscribe(this.studentDataFields$);
89

90
        this._sdfb.getCriteria(true);
91
        this.criteriaSub = this._ngRedux.select(state => {
92
93
            if (state.criter.size > 0) {
                state.criter.reduce(({}, criteria) => {
94
                    //this.studentCriteriaGroup.setValue(criteria);
95
                      if (criteria.selected === true && (criteria.name === "Εισόδημα" ))  {
96
                        this.selectionIncomeId = Number(criteria.id);
97
                      }
98
                      this.rss.push( new FormControl(criteria.selected, []));
99
100
101
102
                    return criteria;
                }, {});
            }
            return state.criter;
103
104
105
        }).subscribe(this.criteria$);

    }
106

107
108
109
    ngOnDestroy() {
        if (this.studentDataFieldsSub) this.studentDataFieldsSub.unsubscribe();
        if (this.criteriaSub) this.criteriaSub.unsubscribe();
110
    }
111

112
113
114
115
    navigateBack() {
        this.router.navigate(['/schools-order-select']);
    }

116
117
    submitSelected() {
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
118

119
120
        for (let i=7; i < 11; i++)
          this.studentCriteriaGroup.controls['formArray']['controls'][i].setValue(false);
121
        this.studentCriteriaGroup.controls['formArray']['controls'][this.selectionIncomeId-1].setValue(true);
122

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

125
        this.router.navigate(['/application-submit']);
126
127
    }

128
129
130
131
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
        this.studentCriteriaGroup.controls['formArray']['controls'][mutual_disabled-1].setValue(false);
      }
132
133
    }

134
135
136
137
138
139
140
141
142
    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;
143
    }
144

145
146
    //checkChoice(c: FormControl) {
    checkChoice(c: FormControl) {
147
      if (c.value === "noincomecriterio" ) {
148
149
150
151
152
        return {status: true}
      }
      else
      // Null means valid, believe it or not
        return null;
153
    }
154
}