application.form.main.ts 5.98 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
17
18
19
20
21
22
23
24
25
26
27

import {
    FormBuilder,
    FormGroup,
    FormControl,
    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>;
28
    private criteria$: Observable<ICriter>;
29
30

    public studentDataGroup: FormGroup;
31
32
33
34
    public applicantDataGroup: FormGroup;
    public studentCriteriaGroup: FormGroup;
    //public orphanmode = <number>0;
    //public childrenmode = <number>0;
35

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

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

        this.studentCriteriaGroup = this.fb.group({
          orphanmono: false,
          orphantwice: false,
          threechildren: false,
          manychildren: false,
          twins: false,
          disability: false,
          studies: false,
66
          income: ['noincomecriterio', this.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
84
85
86
87
88
89
90
91
        this.criteria$ = this._ngRedux.select(state => {
            if (state.criter.size > 0) {
                state.criter.reduce(({}, criteria) => {
                    this.studentCriteriaGroup.setValue(criteria);
                    return criteria;
                }, {});
            }
            return state.criter;
        });
92

93
    }
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    navigateBack() {
/*        this._ngRedux.select(state => {
            state.epalclasses.reduce(({}, epalclass) =>{
              if (epalclass.name === "Α' Λυκείου")
                this.router.navigate(['/region-schools-select']);
              else if (epalclass.name === "Β' Λυκείου")
                  this.router.navigate(['/region-schools-select']);
              else if (epalclass.name === "Γ' Λυκείου")
                    this.router.navigate(['/region-schools-select']);
              return epalclass;
            }, {});
            return state.epalclasses;
        }); */
        this.router.navigate(['/schools-order-select']);

    }

112
113
    submitSelected() {
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
114

115
        this.router.navigate(['/application-submit']);
116
    }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

    checkorphan(orphanModeName,cb) {
      if (orphanModeName === "mono" && cb.checked === true)
        this.studentCriteriaGroup.controls['orphantwice'].setValue(false);
      else if (orphanModeName === "twice" && cb.checked === true)
        this.studentCriteriaGroup.controls['orphanmono'].setValue(false);

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

      //console.log("after check");
      //console.log(this.studentCriteriaGroup.value);
    }

    checkchildren(childrenModeName, cb) {
      if (childrenModeName === "three" && cb.checked === true)
        this.studentCriteriaGroup.controls['manychildren'].setValue(false);
      else if (childrenModeName === "many" && cb.checked === true)
        this.studentCriteriaGroup.controls['threechildren'].setValue(false);

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

      //console.log("after check");
      //console.log(this.studentCriteriaGroup.value);
    }

    checkstatus() {
        //this.studentCriteriaGroup.controls[name].setValue(cb.checked);
        //console.log("after check");
        //console.log(this.studentCriteriaGroup.value);
        this._sdfb.saveCriteria([this.studentCriteriaGroup.value]);
    }

149
150
151
152
153
154
155
156
     checkChoice(c: FormControl) {
      console.log(c.value);
      if (c.value === "noincomecriterio")
        return {status: true}
      else
      // Null means valid, believe it or not
        return null;
    }
157
158

}