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

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

    private rss = new FormArray([]);
    private selectionIncomeId = <number>0;
37
38
    //public orphanmode = <number>0;
    //public childrenmode = <number>0;
39

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

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

62
        /*
63
64
65
66
67
68
69
70
        this.studentCriteriaGroup = this.fb.group({
          orphanmono: false,
          orphantwice: false,
          threechildren: false,
          manychildren: false,
          twins: false,
          disability: false,
          studies: false,
71
72
73
74
75
76
77
          income: ['noincomecriterio', checkChoice ],
        });
        */
        this.studentCriteriaGroup = this.fb.group({
            formArray: this.rss,
            income: ['noincomecriterio', checkChoice ],
            incometest: ['noincomecriterio', checkChoice ],
78
        });
79

80
81
82
    };

    ngOnInit() {
83

84
85
86
87
88
89
90
91
92
93
        this.studentDataFields$ = this._ngRedux.select(state => {
            if (state.studentDataFields.size > 0) {
                state.studentDataFields.reduce(({}, studentDataField) => {
                    this.studentDataGroup.setValue(studentDataField);
                    return studentDataField;
                }, {});
            }
            return state.studentDataFields;
        });

94
        this._sdfb.getCriteria(true);
95
96
97
        this.criteria$ = this._ngRedux.select(state => {
            if (state.criter.size > 0) {
                state.criter.reduce(({}, criteria) => {
98
99
100
101
102
103
                    //this.studentCriteriaGroup.setValue(criteria);
                      if (criteria.selected === true && (criteria.id === "8" || criteria.id === "9" || criteria.id === "10" || criteria.id === "11" ))
                        this.selectionIncomeId = Number(criteria.id);
                      console.log("Yes");
                      console.log(this.selectionIncomeId);
                      this.rss.push( new FormControl(criteria.selected, []));
104
105
106
107
108
                    return criteria;
                }, {});
            }
            return state.criter;
        });
109

110
    }
111

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    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']);

    }

129
130
    submitSelected() {
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
131

132
133
134
135
136
137
138
        this.studentCriteriaGroup.controls['formArray']['controls'][7].setValue(false);
        this.studentCriteriaGroup.controls['formArray']['controls'][8].setValue(false);
        this.studentCriteriaGroup.controls['formArray']['controls'][9].setValue(false);
        this.studentCriteriaGroup.controls['formArray']['controls'][10].setValue(false);
        this.studentCriteriaGroup.controls['formArray']['controls'][this.selectionIncomeId-1].setValue(true);
        this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);

139
        this.router.navigate(['/application-submit']);
140
    }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

    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);
    }

166
167
168
169
170
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
        this.studentCriteriaGroup.controls['formArray']['controls'][mutual_disabled-1].setValue(false);
      }
      //this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);
171
172
    }

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199


    checkstatus(cb) {
        console.log("Hey");
        console.log(this.studentCriteriaGroup.value.income);
        //this._sdfb.saveCriteria([this.studentCriteriaGroup.value.income]);


        console.log("OK");
        console.log(cb.value);

        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;


          //this.studentCriteriaGroup.value[9] = true;
          //this.studentCriteriaGroup.value[9] = false;
        console.log(this.studentCriteriaGroup.value.formArray);
        console.log(this.studentCriteriaGroup.value);
        //this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);

200
    }
201

202
203
204
205
206
207
208
209
210
211
212
213


}


function checkChoice(c: FormControl) {
  console.log(c.value);
  if (c.value === "noincomecriterio")
    return {status: true}
  else
  // Null means valid, believe it or not
    return null;
214
}