application.form.main.ts 5.55 KB
Newer Older
1
import { Component, OnInit } from '@angular/core';
2
import { Observable } 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
14
15
16

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

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

@Injectable() export default class StudentApplicationMain implements OnInit {

    private studentDataFields$: Observable<IStudentDataFields>;
29
    private criteria$: Observable<ICriter>;
30
    private showLoader$: Observable<boolean>;
31
32

    public studentDataGroup: FormGroup;
33
34
    public applicantDataGroup: FormGroup;
    public studentCriteriaGroup: FormGroup;
35
36
37

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

39
40
    constructor(private fb: FormBuilder,
                private _sdfa: StudentDataFieldsActions,
41
                private _sdfb: CriteriaActions,
42
43
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
44
        this.studentDataGroup = this.fb.group({
45
            epaluser_id: [,[]],
46
47
48
49
50
            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]],
51
52
            certificatetype: ['Απολυτήριο Λυκείου', this.checkChoice],
            relationtostudent: ['Μαθητής', this.checkChoice],
53
            telnum:  ['2610789789', [Validators.pattern(VALID_DIGITS_PATTERN),Validators.required]],
54
        });
55
56
57
58
59
60

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

61
62
        this.studentCriteriaGroup = this.fb.group({
            formArray: this.rss,
63
            income: ['', this.checkChoice ],
64
            //income: ['noincomecriterio', Validators.required ],
65
            //income: [this.selectionIncomeId, this.checkChoice ],
66
            //incometest: ['noincomecriterio', checkChoice ],
67
        });
68

69
70
71
    };

    ngOnInit() {
72

73
74
75
76
77
78
79
80
81
82
        this.studentDataFields$ = this._ngRedux.select(state => {
            if (state.studentDataFields.size > 0) {
                state.studentDataFields.reduce(({}, studentDataField) => {
                    this.studentDataGroup.setValue(studentDataField);
                    return studentDataField;
                }, {});
            }
            return state.studentDataFields;
        });

83
        this._sdfb.getCriteria(true);
84
85
86
        this.criteria$ = this._ngRedux.select(state => {
            if (state.criter.size > 0) {
                state.criter.reduce(({}, criteria) => {
87
                    //this.studentCriteriaGroup.setValue(criteria);
88
                      if (criteria.selected === true && (criteria.name === "Εισόδημα" ))  {
89
                        this.selectionIncomeId = Number(criteria.id);
90
                      }
91
                      this.rss.push( new FormControl(criteria.selected, []));
92
93
94
95
96
                    return criteria;
                }, {});
            }
            return state.criter;
        });
97
        this.showLoader$ = this.criteria$.map(criter => criter.size === 0);
98

99
    }
100

101
102
103
104
    navigateBack() {
        this.router.navigate(['/schools-order-select']);
    }

105
106
    submitSelected() {
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
107

108
109
        for (let i=7; i < 11; i++)
          this.studentCriteriaGroup.controls['formArray']['controls'][i].setValue(false);
110
        this.studentCriteriaGroup.controls['formArray']['controls'][this.selectionIncomeId-1].setValue(true);
111

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

114
        this.router.navigate(['/application-submit']);
115
116
    }

117
118
119
120
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
        this.studentCriteriaGroup.controls['formArray']['controls'][mutual_disabled-1].setValue(false);
      }
121
122
    }

123
124
125
126
127
128
129
130
131
    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;
132
    }
133

134
135
    //checkChoice(c: FormControl) {
    checkChoice(c: FormControl) {
136
      if (c.value === "noincomecriterio" ) {
137
138
139
140
141
        return {status: true}
      }
      else
      // Null means valid, believe it or not
        return null;
142
143
144
145
146


}


147
}