course.fields.select.ts 2.42 KB
Newer Older
1
import { Component, OnInit } from '@angular/core';
2
import { Router } from '@angular/router';
3
import { Observable } from 'rxjs/Rx';
4
5
import { Injectable } from "@angular/core";
import { CourseFieldsActions } from '../../actions/coursefields.actions';
6
7
import { NgRedux, select } from 'ng2-redux';
import { ICourseFields } from '../../store/coursefields/coursefields.types';
8
import { IAppState } from '../../store/store';
9
10
11
12
13
14
15
16
17
18
19
20
21
22

import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'course-fields-select',
    template: `
     <form [formGroup]="formGroup">
        <div formArrayName="formArray">
23
            <div *ngFor="let courseField$ of courseFields$ | async; let i=index">
24
            <div class="row">
25
            <div class="col-md-1">
26
27
                <input type="checkbox" formControlName="{{i}}">
            </div>
28
            <div class="col-md-11 pull-left">
29
                {{courseField$.name}}
30
31
32
33
            </div>
            </div>
            </div>
        </div>
34
35
        <div class="row">
        <div class="col-md-2 col-md-offset-5">
36
            <button type="button" class="btn-primary btn-lg pull-center" (click)="saveSelected()">
37
38
            Συνέχεια<span class="glyphicon glyphicon-menu-right"></span>
            </button>
39
40
        </div>
        </div>
41
    </form>
42
<!--    <pre>{{formGroup.value | json}}</pre> -->
43
44
45
  `
})
@Injectable() export default class CourseFieldsSelect implements OnInit {
46
47
    private courseFields$: Observable<ICourseFields>;

48
49
50
    public formGroup: FormGroup;
    public cfs = new FormArray([]);

51
52
53
54
    constructor(private fb: FormBuilder,
                private _cfa: CourseFieldsActions,
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
55
56
57
58
59
60
        this.formGroup = this.fb.group({
            formArray: this.cfs
        });
    };

    ngOnInit() {
61

62
        this._cfa.getCourseFields();
63

64
        this.courseFields$ = this._ngRedux.select(state => {
65
66
67
68
            state.courseFields.reduce(({}, courseField) =>{
                this.cfs.push(new FormControl(courseField.selected, []));
                return courseField;
            }, {});
69
            return state.courseFields;
70
71
72
73
74
75
        });

    }

    saveSelected() {
        this._cfa.saveCourseFieldsSelected(this.formGroup.value.formArray);
76
        this.router.navigate(['/region-schools-select']);
77
    }
78
}