application.form.main.ts 2.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {Component, OnInit} from '@angular/core';
import { Observable } from 'rxjs/Rx';
import {Injectable, ChangeDetectionStrategy} from "@angular/core";
import {StudentDataFieldsActions} from '../../actions/studentdatafields.actions';

import { DevToolsExtension, NgRedux, select } from 'ng2-redux';
import { IStudentDataFields } from '../../store/studentdatafields/studentdatafields.types';
import { ICourseFields } from '../../store/coursefields/coursefields.types';
import { IAppState } from '../../store/store';
import { VALID_NAMES_PATTERN } from '../../constants';

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>;
    private courseFields$: Observable<ICourseFields>;

    public studentDataGroup: FormGroup;

    constructor(private fb: FormBuilder, private _sdfa: StudentDataFieldsActions, private _ngRedux: NgRedux<IAppState>) {

        this.studentDataGroup = this.fb.group({
            studentAmka: ['12345', Validators.required],
            studentFirstname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            studentSurname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            guardianFirstname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            guardianSurname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            regionAddress: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            regionTK: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            regionArea: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
            certificateType: ['', Validators.required],
            relationToStudent: ['', Validators.required],
        });

    };

    ngOnInit() {
        this.studentDataFields$ = this._ngRedux.select(state => {
            if (state.studentDataFields.size > 0) {
                state.studentDataFields.reduce(({}, studentDataField) => {
                    this.studentDataGroup.setValue(studentDataField);
                    return studentDataField;
                }, {});
            }
            return state.studentDataFields;
        });

        this.courseFields$ = this._ngRedux.select(state => {
           //console.log("test2");
            state.courseFields.reduce(({}, courseField) =>{
//                this.cfs.push(new FormControl(courseField.selected, []));
                //console.log(courseField.selected);
                return courseField;
            }, {});
            return state.courseFields;
        });
    }

    saveSelected() {
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
    }
}