application.form.main.ts 2.87 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 { StudentDataFieldsActions } from '../../actions/studentdatafields.actions';
import { Router } from '@angular/router';
6

7
import { NgRedux, select } from 'ng2-redux';
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;

32
33
34
35
    constructor(private fb: FormBuilder,
                private _sdfa: StudentDataFieldsActions,
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
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
        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 => {
            state.courseFields.reduce(({}, courseField) =>{
                return courseField;
            }, {});
            return state.courseFields;
        });
    }

    saveSelected() {
        this._sdfa.saveStudentDataFields([this.studentDataGroup.value]);
71
        this.router.navigate(['/course-fields-select']);
72
73
    }
}