course-fields-select.ts 2.29 KB
Newer Older
1
import { Component, OnInit } from '@angular/core';
2
3
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
4
5
import { Injectable } from "@angular/core";
import { CourseFieldsActions } from '../../actions/coursefields.actions';
6
import { DevToolsExtension, NgRedux, select } from 'ng2-redux';
7
import { ICourseFields, ICourseField } 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
36
37
38
        <div class="row">
        <div class="col-md-2 col-md-offset-5">
            <button class="btn-primary btn-lg pull-center" (click)="saveSelected()">Συνέχεια</button>
        </div>
        </div>
39
    </form>
40
<!--    <pre>{{formGroup.value | json}}</pre> -->
41
42
43
  `
})
@Injectable() export default class CourseFieldsSelect implements OnInit {
44
45
    private courseFields$: Observable<ICourseFields>;

46
47
48
    public formGroup: FormGroup;
    public cfs = new FormArray([]);

49
    constructor(private http: Http, private fb: FormBuilder, private _cfa: CourseFieldsActions, private _ngRedux: NgRedux<IAppState>) {
50
51
52
53
54
55
        this.formGroup = this.fb.group({
            formArray: this.cfs
        });
    };

    ngOnInit() {
56

57
        this._cfa.getCourseFields();
58

59
        this.courseFields$ = this._ngRedux.select(state => {
60
61
62
63
            state.courseFields.reduce(({}, courseField) =>{
                this.cfs.push(new FormControl(courseField.selected, []));
                return courseField;
            }, {});
64
            return state.courseFields;
65
66
67
68
69
70
        });

    }

    saveSelected() {
        this._cfa.saveCourseFieldsSelected(this.formGroup.value.formArray);
71
    }
72
73


74
}