application.form.main.ts 11.5 KB
Newer Older
1
import { Component, OnInit, OnDestroy } from '@angular/core';
2
import { BehaviorSubject, Subscription, 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
12
import { VALID_NAMES_PATTERN, VALID_ADDRESS_PATTERN, VALID_ADDRESSTK_PATTERN, VALID_DIGITS_PATTERN,
    VALID_DATE_PATTERN, FIRST_SCHOOL_YEAR, VALID_YEAR_PATTERN, VALID_TELEPHONE_PATTERN } from '../../constants';
13
14
import { STUDENT_DATA_FIELDS_INITIAL_STATE } from '../../store/studentdatafields/studentdatafields.initial-state';
import { CRITERIA_INITIAL_STATE } from '../../store/criteria/criteria.initial-state';
15
16
17
import { ILoginInfo, ILoginInfoToken } from '../../store/logininfo/logininfo.types';
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';
import {IMyDpOptions} from 'mydatepicker';
18
import {Http, RequestOptions} from '@angular/http';
19
20
21
22
23

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

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

@Injectable() export default class StudentApplicationMain implements OnInit {

35
36
37
38
39
    private studentDataFields$: BehaviorSubject<IStudentDataFields>;
    private criteria$: BehaviorSubject<ICriter>;

    private studentDataFieldsSub: Subscription;
    private criteriaSub: Subscription;
40
41

    public studentDataGroup: FormGroup;
42
    public studentCriteriaGroup: FormGroup;
43

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

    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;
49
50
    private schoolyears: string[];
    private graduationyears: string[];
51

52
    private rss = new FormArray([]);
53

54
55
56
57
58
59
    private myDatePickerOptions: IMyDpOptions = {
        // other options...
        sunHighlight: false,
        editableDateField: false,
        dateFormat: 'dd/mm/yyyy',
    };
60

61
62
63
64
65
66
67
68
69
70
71
72
73
    private observableSource = (keyword: any): Observable<any[]> => {
    let url: string = 'https://mm.sch.gr/api/units?name='+keyword;
    if (keyword) {
      return this.http.get(url)
        .map(res => {
          let json = res.json();
          return json.data;
        })
    } else {
      return Observable.of([]);
    }
    }

74
75
    constructor(private fb: FormBuilder,
                private _sdfa: StudentDataFieldsActions,
76
                private _sdfb: CriteriaActions,
77
                private _ngRedux: NgRedux<IAppState>,
78
79
80
81
                private router: Router,
                private http: Http) {
        this.populateSchoolyears();
        this.populateGraduationyears();
82
83
84
85
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.modalTitle =  new BehaviorSubject("");
        this.modalText =  new BehaviorSubject("");
        this.modalHeader =  new BehaviorSubject("");
86
87
88

        this.studentDataFields$ = new BehaviorSubject(STUDENT_DATA_FIELDS_INITIAL_STATE);
        this.criteria$ = new BehaviorSubject(CRITERIA_INITIAL_STATE);
89
        this.studentDataGroup = this.fb.group({
90
91
92
93
//            epaluser_id: [,[]],

            name: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            studentsurname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
94
95
            studentbirthdate: ['', [Validators.required]],
            //studentbirthdate: ['', [Validators.pattern(VALID_DATE_PATTERN),Validators.required]],
96
97
98
99
100
101
102
            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],
103
104
105
106
107
            telnum:  ['', [Validators.pattern(VALID_TELEPHONE_PATTERN),Validators.required]],
            graduation_year: ['', this.checkChoice],
            lastschool_schoolname: ['', [Validators.pattern(VALID_ADDRESS_PATTERN),Validators.required]],
            lastschool_schoolyear: ['', this.checkChoice],
            lastschool_class: ['', this.checkChoice],
108
109
        });

110

111
/*        this.studentCriteriaGroup = this.fb.group({
112
            formArray: this.rss,
113
        }); */
114

115
116
117
    };

    ngOnInit() {
118
119
120
121
122
123
124
125
126
127
128
        (<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$);
129

130
        this.studentDataFieldsSub = this._ngRedux.select(state => {
131
132
            if (state.studentDataFields.size > 0) {
                state.studentDataFields.reduce(({}, studentDataField) => {
133

134
135
136
137
138
139
140
141
                    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);
142
143
144
145
                    this.studentDataGroup.controls['graduation_year'].setValue(studentDataField.graduation_year);
                    this.studentDataGroup.controls['lastschool_schoolname'].setValue(studentDataField.lastschool_schoolname);
                    this.studentDataGroup.controls['lastschool_schoolyear'].setValue(studentDataField.lastschool_schoolyear);
                    this.studentDataGroup.controls['lastschool_class'].setValue(studentDataField.lastschool_class);
146
                    this.studentDataGroup.controls['relationtostudent'].setValue(studentDataField.relationtostudent);
147
                    this.studentDataGroup.controls['telnum'].setValue(studentDataField.telnum);
148
                    this.studentDataGroup.controls['studentbirthdate'].setValue(this.populateDate(studentDataField.studentbirthdate));
149
150
151
152
                    return studentDataField;
                }, {});
            }
            return state.studentDataFields;
153
        }).subscribe(this.studentDataFields$);
154

155
156


157
/*        this._sdfb.getCriteria(true);
158
        this.criteriaSub = this._ngRedux.select(state => {
159
160
            if (state.criter.size > 0) {
                state.criter.reduce(({}, criteria) => {
161
                    //this.studentCriteriaGroup.setValue(criteria);
162
163
164
165

                      //if (criteria.selected === true && (criteria.name === "Εισόδημα" ))  {
                      //  this.selectionIncomeId = Number(criteria.id);
                      //}
166
                      this.rss.push( new FormControl(criteria.selected, []));
167
168
169
170
                    return criteria;
                }, {});
            }
            return state.criter;
171
        }).subscribe(this.criteria$); */
172

173
    };
174

175
    ngOnDestroy() {
176
        (<any>$('#applicationFormNotice')).remove();
177
        if (this.studentDataFieldsSub) this.studentDataFieldsSub.unsubscribe();
178
//        if (this.criteriaSub) this.criteriaSub.unsubscribe();
179
        if (this.studentDataFields$) this.studentDataFields$.unsubscribe();
180
//        if (this.criteria$) this.criteria$.unsubscribe();
181
        if (this.loginInfo$) this.loginInfo$.unsubscribe();
182
    }
183

184
    navigateBack() {
185
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
186
//        this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);
187
188
189
        this.router.navigate(['/schools-order-select']);
    }

190
    submitSelected() {
191
192
        // if (this.studentDataGroup.invalid || this.studentCriteriaGroup.invalid) {
        if (this.studentDataGroup.invalid) {
193
194
195
196
197
198
            this.modalHeader.next("modal-header-danger");
            this.modalTitle.next("Η αίτηση δεν είναι πλήρης");
            this.modalText.next("Πρέπει να συμπληρώσετε όλα τα πεδία που συνοδεύονται από (*)");
            this.showModal();
        } else {
            this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
199
//            this._sdfb.saveCriteria([this.studentCriteriaGroup.value.formArray]);
200
201
            this.router.navigate(['/application-submit']);
        }
202
203
    }

204
205
    checkcriteria(cb, mutual_disabled) {
      if (mutual_disabled !== "-1" && cb.checked === true) {
206
207
208
209
        //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);
210
        }
211

212
      }
213
214
    }

215
    checkChoice(c: FormControl) {
216
      return (c.value === "" ) ? {status: true} : null;
217
    }
218
219

    populateDate(d) {
220
221
222
223
224
225
226
227
228
229
230
231
232
        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
            };
        }
233
234
    }

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    private populateSchoolyears(): void {
        let endYear = new Date().getFullYear();
        this.schoolyears = new Array();
        for (let i=endYear; i>FIRST_SCHOOL_YEAR; i--) {
            this.schoolyears.push((i-1) + "-" + i);
        }
    };

    private populateGraduationyears(): void {
        let endYear = new Date().getFullYear();
        this.graduationyears = new Array();
        for (let i=endYear; i>FIRST_SCHOOL_YEAR; i--) {
            this.graduationyears.push(i + "");
        }
    };

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
    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');
    }

272
273
274
275
276
277
278
279
280
281
282
283
    lastSchoolListFormatter(data: any): string {
      return data.name;
    };

    lastSchoolValueFormatter(data: any): string {
      return data.name;
    };

    lastSchoolValueChanged(e: any): void {
//        console.log(this.studentDataGroup.controls['lastschool_schoolname'].value);
    };

284
}