application.form.main.ts 9.4 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
import { ILoginInfo, ILoginInfoToken } from '../../store/logininfo/logininfo.types';
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';
import {IMyDpOptions} from 'mydatepicker';
17
18
19
20
21

import {
    FormBuilder,
    FormGroup,
    FormControl,
22
    FormArray,
23
24
25
26
27
28
29
30
31
32
    Validators,
} from '@angular/forms';

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

@Injectable() export default class StudentApplicationMain implements OnInit {

33
34
35
36
37
    private studentDataFields$: BehaviorSubject<IStudentDataFields>;
    private criteria$: BehaviorSubject<ICriter>;

    private studentDataFieldsSub: Subscription;
    private criteriaSub: Subscription;
38
39

    public studentDataGroup: FormGroup;
40
    public studentCriteriaGroup: FormGroup;
41

42
43
44
45
46
47
    private loginInfo$: BehaviorSubject<ILoginInfo>;

    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;

48
    private rss = new FormArray([]);
49

50
51
52
53
54
55
    private myDatePickerOptions: IMyDpOptions = {
        // other options...
        sunHighlight: false,
        editableDateField: false,
        dateFormat: 'dd/mm/yyyy',
    };
56

57
58
    constructor(private fb: FormBuilder,
                private _sdfa: StudentDataFieldsActions,
59
                private _sdfb: CriteriaActions,
60
61
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
62
63
64
65
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.modalTitle =  new BehaviorSubject("");
        this.modalText =  new BehaviorSubject("");
        this.modalHeader =  new BehaviorSubject("");
66
67
68

        this.studentDataFields$ = new BehaviorSubject(STUDENT_DATA_FIELDS_INITIAL_STATE);
        this.criteria$ = new BehaviorSubject(CRITERIA_INITIAL_STATE);
69
        this.studentDataGroup = this.fb.group({
70
71
72
73
//            epaluser_id: [,[]],

            name: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            studentsurname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
74
75
            studentbirthdate: ['', [Validators.required]],
            //studentbirthdate: ['', [Validators.pattern(VALID_DATE_PATTERN),Validators.required]],
76
77
78
79
80
81
82
83
            fatherfirstname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            motherfirstname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            regionaddress: ['', [Validators.pattern(VALID_ADDRESS_PATTERN),Validators.required]],
            regiontk: ['', [Validators.pattern(VALID_ADDRESSTK_PATTERN),Validators.required]],
            regionarea: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            certificatetype: ['', this.checkChoice],
            relationtostudent: ['', this.checkChoice],
            telnum:  ['', [Validators.pattern(VALID_DIGITS_PATTERN),Validators.required]],
84
85
        });

86
/*        this.studentCriteriaGroup = this.fb.group({
87
            formArray: this.rss,
88
        }); */
89

90
91
92
    };

    ngOnInit() {
93
94
95
96
97
98
99
100
101
102
103
        (<any>$('#applicationFormNotice')).appendTo("body");

        this._ngRedux.select(state => {
            if (state.loginInfo.size > 0) {
                state.loginInfo.reduce(({}, loginInfoToken) => {
                    console.log(loginInfoToken.cu_name);
                    return loginInfoToken;
                }, {});
            }
            return state.loginInfo;
        }).subscribe(this.loginInfo$);
104

105
        this.studentDataFieldsSub = this._ngRedux.select(state => {
106
107
            if (state.studentDataFields.size > 0) {
                state.studentDataFields.reduce(({}, studentDataField) => {
108
109
110
111
112
113
114
115
                    this.studentDataGroup.controls['name'].setValue(studentDataField.name);
                    this.studentDataGroup.controls['studentsurname'].setValue(studentDataField.studentsurname);
                    this.studentDataGroup.controls['fatherfirstname'].setValue(studentDataField.fatherfirstname);
                    this.studentDataGroup.controls['motherfirstname'].setValue(studentDataField.motherfirstname);
                    this.studentDataGroup.controls['regionaddress'].setValue(studentDataField.regionaddress);
                    this.studentDataGroup.controls['regiontk'].setValue(studentDataField.regiontk);
                    this.studentDataGroup.controls['regionarea'].setValue(studentDataField.regionarea);
                    this.studentDataGroup.controls['certificatetype'].setValue(studentDataField.certificatetype);
116
                    this.studentDataGroup.controls['relationtostudent'].setValue(studentDataField.relationtostudent);
117
                    this.studentDataGroup.controls['telnum'].setValue(studentDataField.telnum);
118
                    this.studentDataGroup.controls['studentbirthdate'].setValue(this.populateDate(studentDataField.studentbirthdate));
119
120
121
122
                    return studentDataField;
                }, {});
            }
            return state.studentDataFields;
123
        }).subscribe(this.studentDataFields$);
124

125
/*        this._sdfb.getCriteria(true);
126
        this.criteriaSub = this._ngRedux.select(state => {
127
128
            if (state.criter.size > 0) {
                state.criter.reduce(({}, criteria) => {
129
                    //this.studentCriteriaGroup.setValue(criteria);
130
131
132
133

                      //if (criteria.selected === true && (criteria.name === "Εισόδημα" ))  {
                      //  this.selectionIncomeId = Number(criteria.id);
                      //}
134
                      this.rss.push( new FormControl(criteria.selected, []));
135
136
137
138
                    return criteria;
                }, {});
            }
            return state.criter;
139
        }).subscribe(this.criteria$); */
140
141

    }
142

143
    ngOnDestroy() {
144
        (<any>$('#applicationFormNotice')).remove();
145
        if (this.studentDataFieldsSub) this.studentDataFieldsSub.unsubscribe();
146
//        if (this.criteriaSub) this.criteriaSub.unsubscribe();
147
        if (this.studentDataFields$) this.studentDataFields$.unsubscribe();
148
//        if (this.criteria$) this.criteria$.unsubscribe();
149
        if (this.loginInfo$) this.loginInfo$.unsubscribe();
150
    }
151

152
    navigateBack() {
153
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
154
//        this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);
155
156
157
        this.router.navigate(['/schools-order-select']);
    }

158
    submitSelected() {
159
160
        // if (this.studentDataGroup.invalid || this.studentCriteriaGroup.invalid) {
        if (this.studentDataGroup.invalid) {
161
162
163
164
165
166
            this.modalHeader.next("modal-header-danger");
            this.modalTitle.next("Η αίτηση δεν είναι πλήρης");
            this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*)");
            this.showModal();
        } else {
            this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
167
//            this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);
168
169
            this.router.navigate(['/application-submit']);
        }
170
171
    }

172
173
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
174
175
176
177
        //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);
178
        }
179

180
      }
181
182
    }

183
    checkChoice(c: FormControl) {
184
      if (c.value === "" ) {
185
186
187
188
189
        return {status: true}
      }
      else
      // Null means valid, believe it or not
        return null;
190
    }
191
192

    populateDate(d) {
193
194
195
196
197
198
199
200
201
202
203
204
205
        if (d && d.length > 0) {
            return {
                date: {
                    year: d ? parseInt(d.substr(0,4)) : 0,
                    month: d ? parseInt(d.substr(6,8)) : 0,
                    day: d ? parseInt(d.substr(8,10)) : 0
                }
            };
        } else {
            return {
                date: null
            };
        }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    }

    setDate() {
        let date = new Date();
        return { date: {
            year: date.getFullYear() - 14,
            month: date.getMonth() + 1,
            day: date.getDate()}
        };
    }

    clearDate() {
        return null;
    }

    public showModal():void {
        (<any>$('#applicationFormNotice')).modal('show');
    }

    public hideModal():void {
        (<any>$('#applicationFormNotice')).modal('hide');
    }

229
}